|
1 |
| -import React, { useState } from 'react'; |
2 |
| -import { toast } from 'react-toastify'; |
3 |
| -import Cookies from 'js-cookie'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
4 | 2 | import { logStore } from '~/lib/stores/logs';
|
| 3 | +import { lookupSavedPassword, saveGitAuth, ensureEncryption } from '~/lib/hooks/useCredentials'; |
5 | 4 |
|
6 | 5 | export default function ConnectionsTab() {
|
7 |
| - const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || ''); |
8 |
| - const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || ''); |
9 |
| - |
10 |
| - const handleSaveConnection = () => { |
11 |
| - Cookies.set('githubUsername', githubUsername); |
12 |
| - Cookies.set('githubToken', githubToken); |
13 |
| - logStore.logSystem('GitHub connection settings updated', { |
14 |
| - username: githubUsername, |
15 |
| - hasToken: !!githubToken, |
| 6 | + const [credentials, setCredentials] = useState({ |
| 7 | + github: { username: '', token: '' }, |
| 8 | + gitlab: { username: '', token: '' }, |
| 9 | + }); |
| 10 | + const [expandedProviders, setExpandedProviders] = useState<Record<string, boolean>>({}); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + initializeEncryption(); |
| 14 | + }, []); |
| 15 | + |
| 16 | + const initializeEncryption = async () => { |
| 17 | + const success = await ensureEncryption(); |
| 18 | + |
| 19 | + if (success) { |
| 20 | + loadSavedCredentials(); |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + const loadSavedCredentials = async () => { |
| 25 | + for (const [, config] of Object.entries(providers)) { |
| 26 | + const auth = await lookupSavedPassword(config.url); |
| 27 | + |
| 28 | + if (auth?.username && auth?.password) { |
| 29 | + config.setCredentials(auth.username, auth.password); |
| 30 | + } |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const toggleProvider = (provider: string) => { |
| 35 | + setExpandedProviders((prev) => ({ |
| 36 | + ...prev, |
| 37 | + [provider]: !prev[provider], |
| 38 | + })); |
| 39 | + }; |
| 40 | + |
| 41 | + const providers = { |
| 42 | + github: { |
| 43 | + url: 'github.com', |
| 44 | + username: credentials.github.username, |
| 45 | + token: credentials.github.token, |
| 46 | + title: 'GitHub', |
| 47 | + instructions: 'Enter your GitHub username and personal access token.', |
| 48 | + tokenSetupSteps: [ |
| 49 | + '1. Go to GitHub.com → Settings → Developer settings → Personal access tokens → Tokens (classic)', |
| 50 | + '2. Generate new token (classic) with these scopes:', |
| 51 | + ' • repo (Full control of private repositories)', |
| 52 | + ' • workflow (Optional: Update GitHub Action workflows)', |
| 53 | + '3. Copy the generated token and paste it here', |
| 54 | + ], |
| 55 | + setCredentials: (username: string, token: string) => |
| 56 | + setCredentials((prev) => ({ |
| 57 | + ...prev, |
| 58 | + github: { username, token }, |
| 59 | + })), |
| 60 | + }, |
| 61 | + gitlab: { |
| 62 | + url: 'gitlab.com', |
| 63 | + username: credentials.gitlab.username, |
| 64 | + token: credentials.gitlab.token, |
| 65 | + title: 'GitLab', |
| 66 | + instructions: 'To set up GitLab access:', |
| 67 | + tokenSetupSteps: [ |
| 68 | + '1. Go to GitLab.com → Profile Settings → Access Tokens', |
| 69 | + '2. Create a new token with these scopes:', |
| 70 | + ' • api (Full API access)', |
| 71 | + ' • write_repository (Read/write access)', |
| 72 | + '3. Copy the generated token and paste it here', |
| 73 | + ], |
| 74 | + setCredentials: (username: string, token: string) => |
| 75 | + setCredentials((prev) => ({ |
| 76 | + ...prev, |
| 77 | + gitlab: { username, token }, |
| 78 | + })), |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + const handleSaveConnection = async (provider: keyof typeof providers) => { |
| 83 | + const { url, username, token, title } = providers[provider]; |
| 84 | + |
| 85 | + await saveGitAuth(url, { |
| 86 | + username, |
| 87 | + password: token, |
| 88 | + }); |
| 89 | + |
| 90 | + logStore.logSystem(`${title} connection settings updated`, { |
| 91 | + username, |
| 92 | + hasToken: !!token, |
16 | 93 | });
|
17 |
| - toast.success('GitHub credentials saved successfully!'); |
18 |
| - Cookies.set('git:github.com', JSON.stringify({ username: githubToken, password: 'x-oauth-basic' })); |
19 | 94 | };
|
20 | 95 |
|
21 | 96 | return (
|
22 |
| - <div className="p-4 mb-4 border border-bolt-elements-borderColor rounded-lg bg-bolt-elements-background-depth-3"> |
23 |
| - <h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">GitHub Connection</h3> |
24 |
| - <div className="flex mb-4"> |
25 |
| - <div className="flex-1 mr-2"> |
26 |
| - <label className="block text-sm text-bolt-elements-textSecondary mb-1">GitHub Username:</label> |
27 |
| - <input |
28 |
| - type="text" |
29 |
| - value={githubUsername} |
30 |
| - onChange={(e) => setGithubUsername(e.target.value)} |
31 |
| - 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" |
32 |
| - /> |
33 |
| - </div> |
34 |
| - <div className="flex-1"> |
35 |
| - <label className="block text-sm text-bolt-elements-textSecondary mb-1">Personal Access Token:</label> |
36 |
| - <input |
37 |
| - type="password" |
38 |
| - value={githubToken} |
39 |
| - onChange={(e) => setGithubToken(e.target.value)} |
40 |
| - 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" |
41 |
| - /> |
42 |
| - </div> |
43 |
| - </div> |
44 |
| - <div className="flex mb-4"> |
45 |
| - <button |
46 |
| - onClick={handleSaveConnection} |
47 |
| - 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" |
| 97 | + <div className="space-y-4"> |
| 98 | + {/* Encryption status section remains the same */} |
| 99 | + |
| 100 | + {Object.entries(providers).map(([key, provider]) => ( |
| 101 | + <div |
| 102 | + key={key} |
| 103 | + className="p-4 border border-bolt-elements-borderColor rounded-lg bg-bolt-elements-background-depth-3" |
48 | 104 | >
|
49 |
| - Save Connection |
50 |
| - </button> |
51 |
| - </div> |
| 105 | + <div className="flex items-center justify-between cursor-pointer" onClick={() => toggleProvider(key)}> |
| 106 | + <div className="flex items-center"> |
| 107 | + <h3 className="text-lg font-medium text-bolt-elements-textPrimary">{provider.title} Connection</h3> |
| 108 | + {provider.username && ( |
| 109 | + <span className="ml-2 text-sm text-bolt-elements-textSecondary">({provider.username})</span> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + <div className="flex items-center"> |
| 113 | + {provider.username && provider.token && ( |
| 114 | + <div className="flex items-center mr-3"> |
| 115 | + <div className="w-2 h-2 rounded-full bg-green-500 mr-2" /> |
| 116 | + <span className="text-sm text-bolt-elements-textSecondary">Connected</span> |
| 117 | + </div> |
| 118 | + )} |
| 119 | + <div className={`transform transition-transform ${expandedProviders[key] ? 'rotate-180' : ''}`}> |
| 120 | + <div className="i-ph:caret-down text-bolt-elements-textSecondary" /> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + |
| 125 | + {expandedProviders[key] && ( |
| 126 | + <div className="mt-4"> |
| 127 | + <div className="mb-4 p-3 bg-bolt-elements-background-depth-4 rounded border border-bolt-elements-borderColor"> |
| 128 | + <p className="text-sm text-bolt-elements-textSecondary mb-2">{provider.instructions}</p> |
| 129 | + <ul className="text-sm text-bolt-elements-textSecondary space-y-1"> |
| 130 | + {provider.tokenSetupSteps.map((step, index) => ( |
| 131 | + <li key={index}>{step}</li> |
| 132 | + ))} |
| 133 | + </ul> |
| 134 | + </div> |
| 135 | + |
| 136 | + <div className="flex mb-4"> |
| 137 | + <div className="flex-1 mr-2"> |
| 138 | + <label className="block text-sm text-bolt-elements-textSecondary mb-1"> |
| 139 | + {provider.title} Username: |
| 140 | + </label> |
| 141 | + <input |
| 142 | + type="text" |
| 143 | + value={provider.username} |
| 144 | + onChange={(e) => provider.setCredentials(e.target.value, provider.token)} |
| 145 | + 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" |
| 146 | + /> |
| 147 | + </div> |
| 148 | + <div className="flex-1"> |
| 149 | + <label className="block text-sm text-bolt-elements-textSecondary mb-1">Personal Access Token:</label> |
| 150 | + <input |
| 151 | + type="password" |
| 152 | + value={provider.token} |
| 153 | + onChange={(e) => provider.setCredentials(provider.username, e.target.value)} |
| 154 | + 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" |
| 155 | + /> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + <div className="flex"> |
| 159 | + <button |
| 160 | + onClick={() => handleSaveConnection(key as keyof typeof providers)} |
| 161 | + 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" |
| 162 | + > |
| 163 | + Save {provider.title} Connection |
| 164 | + </button> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + ))} |
52 | 170 | </div>
|
53 | 171 | );
|
54 | 172 | }
|
0 commit comments