mirror of
https://github.com/stackblitz/bolt.new
synced 2025-02-05 20:46:43 +00:00
Merge pull request #757 from dustinwloring1988/feat/enhanced-github-connection
feat: Improved GitHub connection
This commit is contained in:
commit
c257129a61
@ -1,21 +1,87 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { toast } from 'react-toastify';
|
import { toast } from 'react-toastify';
|
||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
import { logStore } from '~/lib/stores/logs';
|
import { logStore } from '~/lib/stores/logs';
|
||||||
|
|
||||||
|
interface GitHubUserResponse {
|
||||||
|
login: string;
|
||||||
|
id: number;
|
||||||
|
[key: string]: any; // for other properties we don't explicitly need
|
||||||
|
}
|
||||||
|
|
||||||
export default function ConnectionsTab() {
|
export default function ConnectionsTab() {
|
||||||
const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || '');
|
const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || '');
|
||||||
const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || '');
|
const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || '');
|
||||||
|
const [isConnected, setIsConnected] = useState(false);
|
||||||
|
const [isVerifying, setIsVerifying] = useState(false);
|
||||||
|
|
||||||
const handleSaveConnection = () => {
|
useEffect(() => {
|
||||||
Cookies.set('githubUsername', githubUsername);
|
// Check if credentials exist and verify them
|
||||||
Cookies.set('githubToken', githubToken);
|
if (githubUsername && githubToken) {
|
||||||
logStore.logSystem('GitHub connection settings updated', {
|
verifyGitHubCredentials();
|
||||||
username: githubUsername,
|
}
|
||||||
hasToken: !!githubToken,
|
}, []);
|
||||||
});
|
|
||||||
toast.success('GitHub credentials saved successfully!');
|
const verifyGitHubCredentials = async () => {
|
||||||
Cookies.set('git:github.com', JSON.stringify({ username: githubToken, password: 'x-oauth-basic' }));
|
setIsVerifying(true);
|
||||||
|
try {
|
||||||
|
const response = await fetch('https://api.github.com/user', {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${githubToken}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const data = (await response.json()) as GitHubUserResponse;
|
||||||
|
if (data.login === githubUsername) {
|
||||||
|
setIsConnected(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setIsConnected(false);
|
||||||
|
return false;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error verifying GitHub credentials:', error);
|
||||||
|
setIsConnected(false);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
setIsVerifying(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSaveConnection = async () => {
|
||||||
|
if (!githubUsername || !githubToken) {
|
||||||
|
toast.error('Please provide both GitHub username and token');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsVerifying(true);
|
||||||
|
const isValid = await verifyGitHubCredentials();
|
||||||
|
|
||||||
|
if (isValid) {
|
||||||
|
Cookies.set('githubUsername', githubUsername);
|
||||||
|
Cookies.set('githubToken', githubToken);
|
||||||
|
logStore.logSystem('GitHub connection settings updated', {
|
||||||
|
username: githubUsername,
|
||||||
|
hasToken: !!githubToken,
|
||||||
|
});
|
||||||
|
toast.success('GitHub credentials verified and saved successfully!');
|
||||||
|
Cookies.set('git:github.com', JSON.stringify({ username: githubToken, password: 'x-oauth-basic' }));
|
||||||
|
setIsConnected(true);
|
||||||
|
} else {
|
||||||
|
toast.error('Invalid GitHub credentials. Please check your username and token.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDisconnect = () => {
|
||||||
|
Cookies.remove('githubUsername');
|
||||||
|
Cookies.remove('githubToken');
|
||||||
|
Cookies.remove('git:github.com');
|
||||||
|
setGithubUsername('');
|
||||||
|
setGithubToken('');
|
||||||
|
setIsConnected(false);
|
||||||
|
logStore.logSystem('GitHub connection removed');
|
||||||
|
toast.success('GitHub connection removed successfully!');
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -28,7 +94,8 @@ export default function ConnectionsTab() {
|
|||||||
type="text"
|
type="text"
|
||||||
value={githubUsername}
|
value={githubUsername}
|
||||||
onChange={(e) => setGithubUsername(e.target.value)}
|
onChange={(e) => setGithubUsername(e.target.value)}
|
||||||
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
|
disabled={isVerifying}
|
||||||
|
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor disabled:opacity-50"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-1">
|
<div className="flex-1">
|
||||||
@ -37,17 +104,41 @@ export default function ConnectionsTab() {
|
|||||||
type="password"
|
type="password"
|
||||||
value={githubToken}
|
value={githubToken}
|
||||||
onChange={(e) => setGithubToken(e.target.value)}
|
onChange={(e) => setGithubToken(e.target.value)}
|
||||||
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
|
disabled={isVerifying}
|
||||||
|
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor disabled:opacity-50"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex mb-4">
|
<div className="flex mb-4 items-center">
|
||||||
<button
|
{!isConnected ? (
|
||||||
onClick={handleSaveConnection}
|
<button
|
||||||
className="bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-primary-backgroundHover text-bolt-elements-button-primary-text"
|
onClick={handleSaveConnection}
|
||||||
>
|
disabled={isVerifying || !githubUsername || !githubToken}
|
||||||
Save Connection
|
className="bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-primary-backgroundHover text-bolt-elements-button-primary-text disabled:opacity-50 disabled:cursor-not-allowed flex items-center"
|
||||||
</button>
|
>
|
||||||
|
{isVerifying ? (
|
||||||
|
<>
|
||||||
|
<div className="i-ph:spinner animate-spin mr-2" />
|
||||||
|
Verifying...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Connect'
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={handleDisconnect}
|
||||||
|
className="bg-bolt-elements-button-danger-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-danger-backgroundHover text-bolt-elements-button-danger-text"
|
||||||
|
>
|
||||||
|
Disconnect
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{isConnected && (
|
||||||
|
<span className="text-sm text-green-600 flex items-center">
|
||||||
|
<div className="i-ph:check-circle mr-1" />
|
||||||
|
Connected to GitHub
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user