mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add id and name to GitCredentials
This commit is contained in:
8 files changed
+204
-193
No files matched your search
@@ -14,10 +14,12 @@ vi.mock('@/lib/configUtils', () => ({
|
||||
|
||||
// Test component that uses the hook
|
||||
function TestComponent() {
|
||||
const { settings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings();
|
||||
const { settings, addCredential, removeCredential, setCredentials, isConfigured } = useGitSettings();
|
||||
|
||||
const handleAdd = () => {
|
||||
const credential: GitCredential = {
|
||||
id: crypto.randomUUID(),
|
||||
name: 'GitHub',
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'git',
|
||||
@@ -27,11 +29,20 @@ function TestComponent() {
|
||||
};
|
||||
|
||||
const handleUpdate = () => {
|
||||
updateCredential(0, { password: 'updated-token' });
|
||||
const githubCred = settings.credentials.find(c => c.host === 'github.com');
|
||||
if (githubCred) {
|
||||
const updatedCredentials = settings.credentials.map((cred) =>
|
||||
cred.id === githubCred.id ? { ...cred, password: 'updated-token' } : cred
|
||||
);
|
||||
setCredentials(updatedCredentials);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemove = () => {
|
||||
removeCredential(0);
|
||||
const githubCred = settings.credentials.find(c => c.host === 'github.com');
|
||||
if (githubCred) {
|
||||
removeCredential(githubCred.id);
|
||||
}
|
||||
};
|
||||
|
||||
const githubCred = settings.credentials.find(c => c.host === 'github.com');
|
||||
@@ -168,14 +179,14 @@ describe('GitSettingsProvider', () => {
|
||||
expect(writeGitSettings).toHaveBeenCalledWith(
|
||||
expect.anything(), // fs instance
|
||||
expect.objectContaining({
|
||||
credentials: [
|
||||
{
|
||||
credentials: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'git',
|
||||
password: 'test-token',
|
||||
}
|
||||
]
|
||||
})
|
||||
])
|
||||
}),
|
||||
'/config' // configPath
|
||||
);
|
||||
@@ -186,6 +197,8 @@ describe('GitSettingsProvider', () => {
|
||||
const settings = {
|
||||
credentials: [
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
name: 'GitLab',
|
||||
protocol: 'https',
|
||||
host: 'gitlab.com',
|
||||
username: 'git',
|
||||
|
||||
@@ -61,27 +61,13 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) {
|
||||
}));
|
||||
};
|
||||
|
||||
const removeCredential = (index: number) => {
|
||||
const removeCredential = (id: string) => {
|
||||
setSettings(prev => ({
|
||||
...prev,
|
||||
credentials: prev.credentials.filter((_, i) => i !== index),
|
||||
credentials: prev.credentials.filter((cred) => cred.id !== id),
|
||||
}));
|
||||
};
|
||||
|
||||
const updateCredential = (index: number, credential: Partial<GitCredential>) => {
|
||||
setSettings(prev => {
|
||||
const newCredentials = [...prev.credentials];
|
||||
newCredentials[index] = {
|
||||
...newCredentials[index],
|
||||
...credential,
|
||||
};
|
||||
return {
|
||||
...prev,
|
||||
credentials: newCredentials,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const setCredentials = (credentials: GitCredential[]) => {
|
||||
setSettings(prev => ({
|
||||
...prev,
|
||||
@@ -96,7 +82,6 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) {
|
||||
updateSettings,
|
||||
addCredential,
|
||||
removeCredential,
|
||||
updateCredential,
|
||||
setCredentials,
|
||||
isConfigured,
|
||||
isInitialized,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
export interface GitCredential {
|
||||
id: string; // UUID generated with crypto.randomUUID()
|
||||
name: string; // Display name for the credential
|
||||
protocol: string;
|
||||
host: string; // Includes port if non-standard (e.g., "github.com:8080")
|
||||
username: string;
|
||||
@@ -18,8 +20,7 @@ export interface GitSettingsContextType {
|
||||
settings: GitSettings;
|
||||
updateSettings: (settings: Partial<GitSettings>) => void;
|
||||
addCredential: (credential: GitCredential) => void;
|
||||
removeCredential: (index: number) => void;
|
||||
updateCredential: (index: number, credential: Partial<GitCredential>) => void;
|
||||
removeCredential: (id: string) => void;
|
||||
setCredentials: (credentials: GitCredential[]) => void;
|
||||
isConfigured: boolean;
|
||||
isInitialized: boolean;
|
||||
|
||||
@@ -72,6 +72,8 @@ export function useGitHubOAuth() {
|
||||
|
||||
// Store the credentials
|
||||
addCredential({
|
||||
id: crypto.randomUUID(),
|
||||
name: 'GitHub',
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: result.username || 'git',
|
||||
|
||||
+20
-18
@@ -157,6 +157,8 @@ describe('configUtils', () => {
|
||||
const sampleGitSettingsNew: GitSettings = {
|
||||
credentials: [
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
name: 'GitHub',
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
@@ -190,16 +192,16 @@ describe('configUtils', () => {
|
||||
const result = await readGitSettings(mockFS);
|
||||
|
||||
expect(mockFS.readFile).toHaveBeenCalledWith('/config/git.json', 'utf8');
|
||||
expect(result).toEqual({
|
||||
credentials: [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'token123',
|
||||
},
|
||||
],
|
||||
expect(result.credentials).toHaveLength(1);
|
||||
expect(result.credentials[0]).toMatchObject({
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'token123',
|
||||
name: 'GitHub',
|
||||
});
|
||||
expect(result.credentials[0].id).toBeDefined();
|
||||
expect(typeof result.credentials[0].id).toBe('string');
|
||||
});
|
||||
|
||||
it('should handle old format with custom ports', async () => {
|
||||
@@ -216,16 +218,16 @@ describe('configUtils', () => {
|
||||
|
||||
const result = await readGitSettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
credentials: [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'git.example.com:8080',
|
||||
username: 'user',
|
||||
password: 'token456',
|
||||
},
|
||||
],
|
||||
expect(result.credentials).toHaveLength(1);
|
||||
expect(result.credentials[0]).toMatchObject({
|
||||
protocol: 'https',
|
||||
host: 'git.example.com:8080',
|
||||
username: 'user',
|
||||
password: 'token456',
|
||||
name: 'git.example.com',
|
||||
});
|
||||
expect(result.credentials[0].id).toBeDefined();
|
||||
expect(typeof result.credentials[0].id).toBe('string');
|
||||
});
|
||||
|
||||
it('should return default settings if file does not exist and no localStorage data', async () => {
|
||||
|
||||
+26
-1
@@ -59,6 +59,8 @@ const aiSettingsSchema = z.object({
|
||||
|
||||
// New format for git credentials
|
||||
const gitCredentialSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
protocol: z.string(),
|
||||
host: z.string(),
|
||||
username: z.string(),
|
||||
@@ -217,17 +219,40 @@ export async function writeAISettings(fs: JSRuntimeFS, settings: AISettings, con
|
||||
await fs.writeFile(aiConfigPath, JSON.stringify(settings, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a display name for a credential based on its hostname
|
||||
*/
|
||||
function generateCredentialName(host: string): string {
|
||||
const hostname = host.split(':')[0]; // Remove port if present
|
||||
|
||||
if (hostname === 'github.com') {
|
||||
return 'GitHub';
|
||||
}
|
||||
|
||||
if (hostname === 'gitlab.com') {
|
||||
return 'GitLab';
|
||||
}
|
||||
|
||||
// For other hosts, use the hostname as the name
|
||||
return hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert old format credentials (map) to new format (array)
|
||||
* This is only used for migrating from the old map-based format
|
||||
*/
|
||||
function convertCredentialsToNewFormat(
|
||||
credentials: Record<string, { username: string; password: string }>
|
||||
): GitCredential[] {
|
||||
return Object.entries(credentials).map(([origin, cred]) => {
|
||||
const url = new URL(origin);
|
||||
const host = url.host; // Includes port if non-standard
|
||||
|
||||
return {
|
||||
id: crypto.randomUUID(),
|
||||
name: generateCredentialName(host),
|
||||
protocol: url.protocol.replace(/:$/, ''),
|
||||
host: url.host, // Includes port if non-standard
|
||||
host,
|
||||
username: cred.username,
|
||||
password: cred.password,
|
||||
};
|
||||
|
||||
+64
-116
@@ -2,6 +2,19 @@ import { describe, it, expect } from 'vitest';
|
||||
import { findCredentialsForRepo } from './gitCredentials';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
|
||||
// Helper to create test credentials with required fields
|
||||
function createTestCredential(overrides: Partial<GitCredential>): GitCredential {
|
||||
return {
|
||||
id: crypto.randomUUID(),
|
||||
name: 'Test Credential',
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('gitCredentials', () => {
|
||||
describe('findCredentialsForRepo', () => {
|
||||
it('returns undefined when no credentials are provided', () => {
|
||||
@@ -11,12 +24,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('returns undefined when no credentials match', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'gitlab.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
@@ -24,12 +34,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('matches credential with exact protocol, hostname, and default port', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -37,12 +44,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('matches credential with explicit port 443 for https', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'github.com:443',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -50,12 +54,10 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('matches credential with explicit port 80 for http', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
createTestCredential({
|
||||
protocol: 'http',
|
||||
host: 'example.com:80',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('http://example.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -63,12 +65,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('matches credential with custom port when URL has same custom port', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'gitlab.local:8443',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://gitlab.local:8443/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -76,12 +75,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('does not match when ports differ', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'gitlab.local:8443',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://gitlab.local:9443/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
@@ -89,12 +85,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('does not match when protocol differs', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
createTestCredential({
|
||||
protocol: 'http',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
@@ -102,12 +95,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('does not match when hostname differs', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://gitlab.com/user/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
@@ -115,18 +105,14 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('prefers credential with matching username over credential without username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: '',
|
||||
password: 'pass1',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
}),
|
||||
createTestCredential({
|
||||
username: 'alice',
|
||||
password: 'pass2',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBe(credentials[1]);
|
||||
@@ -134,18 +120,14 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('returns first match when no username preference exists', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: '',
|
||||
password: 'pass1',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
}),
|
||||
createTestCredential({
|
||||
username: '',
|
||||
password: 'pass2',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -153,12 +135,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('does not match when usernames differ', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: 'alice',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
@@ -166,12 +145,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('matches when credential has username but URL does not', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: 'alice',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -179,12 +155,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('matches when URL has username but credential does not', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: '',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -192,24 +165,18 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles multiple matching credentials and returns the one with matching username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: 'alice',
|
||||
password: 'pass1',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
}),
|
||||
createTestCredential({
|
||||
username: 'bob',
|
||||
password: 'pass2',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
}),
|
||||
createTestCredential({
|
||||
username: '',
|
||||
password: 'pass3',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBe(credentials[1]);
|
||||
@@ -217,12 +184,7 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles complex URLs with paths and query parameters', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
createTestCredential({}),
|
||||
];
|
||||
const result = findCredentialsForRepo(
|
||||
'https://github.com/org/repo.git?foo=bar#fragment',
|
||||
@@ -233,12 +195,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles URLs with encoded characters', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: 'user%40email.com',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo(
|
||||
'https://user%[email protected]/repo.git',
|
||||
@@ -249,12 +208,9 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles subdomains correctly', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'api.github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
// Should not match github.com
|
||||
const result1 = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
@@ -267,12 +223,11 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles credentials with empty username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
createTestCredential({
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'token',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -280,12 +235,10 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('returns credential when both URL and credential have no username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
createTestCredential({
|
||||
username: '',
|
||||
password: 'token',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -293,12 +246,10 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles IPv4 addresses as hostnames', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
createTestCredential({
|
||||
protocol: 'http',
|
||||
host: '192.168.1.100:8080',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('http://192.168.1.100:8080/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
@@ -306,12 +257,10 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('does not match IPv4 addresses with different ports', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
createTestCredential({
|
||||
protocol: 'http',
|
||||
host: '192.168.1.100:8080',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('http://192.168.1.100:9090/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
@@ -319,12 +268,11 @@ describe('gitCredentials', () => {
|
||||
|
||||
it('handles localhost URLs', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
createTestCredential({
|
||||
protocol: 'http',
|
||||
host: 'localhost:3000',
|
||||
username: 'dev',
|
||||
password: 'pass',
|
||||
},
|
||||
}),
|
||||
];
|
||||
const result = findCredentialsForRepo('http://localhost:3000/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
|
||||
+67
-32
@@ -70,13 +70,12 @@ const PRESET_PROVIDERS: PresetProvider[] = [
|
||||
|
||||
interface SortableCredentialItemProps {
|
||||
credential: GitCredential;
|
||||
index: number;
|
||||
onUpdate: (index: number, credential: Partial<GitCredential>) => void;
|
||||
onRemove: (index: number) => void;
|
||||
onUpdate: (updatedCredential: GitCredential) => void;
|
||||
onRemove: (id: string) => void;
|
||||
showDragHandle: boolean;
|
||||
}
|
||||
|
||||
function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDragHandle }: SortableCredentialItemProps) {
|
||||
function SortableCredentialItem({ credential, onUpdate, onRemove, showDragHandle }: SortableCredentialItemProps) {
|
||||
const { t } = useTranslation();
|
||||
const origin = `${credential.protocol}://${credential.host}`;
|
||||
const preset = PRESET_PROVIDERS.find(p => p.origin === origin);
|
||||
@@ -89,7 +88,7 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra
|
||||
transform,
|
||||
transition,
|
||||
isDragging,
|
||||
} = useSortable({ id: `credential-${index}` });
|
||||
} = useSortable({ id: credential.id });
|
||||
|
||||
const style = {
|
||||
transform: CSS.Transform.toString(transform),
|
||||
@@ -114,16 +113,20 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra
|
||||
}
|
||||
};
|
||||
|
||||
const updateCredential = (updates: Partial<GitCredential>) => {
|
||||
onUpdate({ ...credential, ...updates });
|
||||
};
|
||||
|
||||
const handleOriginChange = (newOrigin: string) => {
|
||||
const parsed = parseOrigin(newOrigin);
|
||||
onUpdate(index, parsed);
|
||||
updateCredential(parsed);
|
||||
};
|
||||
|
||||
return (
|
||||
<AccordionItem
|
||||
ref={setNodeRef}
|
||||
style={style}
|
||||
value={`credential-${index}`}
|
||||
value={credential.id}
|
||||
className="border rounded-lg"
|
||||
>
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
@@ -143,7 +146,7 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra
|
||||
fallback={<GitBranch size={16} />}
|
||||
/>
|
||||
<span className="font-medium">
|
||||
{preset?.name || credential.host}
|
||||
{credential.name}
|
||||
</span>
|
||||
{isCustom && <Badge variant="outline">{t('custom')}</Badge>}
|
||||
</div>
|
||||
@@ -151,42 +154,53 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${index}-origin`}>
|
||||
<Label htmlFor={`credential-${credential.id}-name`}>
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`credential-${credential.id}-name`}
|
||||
placeholder="GitHub"
|
||||
value={credential.name}
|
||||
onChange={(e) => updateCredential({ name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${credential.id}-origin`}>
|
||||
{t('origin')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`credential-${index}-origin`}
|
||||
id={`credential-${credential.id}-origin`}
|
||||
placeholder="https://github.com"
|
||||
value={origin}
|
||||
onChange={(e) => handleOriginChange(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${index}-username`}>
|
||||
<Label htmlFor={`credential-${credential.id}-username`}>
|
||||
{t('username')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`credential-${index}-username`}
|
||||
id={`credential-${credential.id}-username`}
|
||||
placeholder="git"
|
||||
value={credential.username}
|
||||
onChange={(e) => onUpdate(index, { username: e.target.value })}
|
||||
onChange={(e) => updateCredential({ username: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${index}-password`}>
|
||||
<Label htmlFor={`credential-${credential.id}-password`}>
|
||||
{t('password')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id={`credential-${index}-password`}
|
||||
id={`credential-${credential.id}-password`}
|
||||
placeholder={t('enterPassword')}
|
||||
value={credential.password}
|
||||
onChange={(e) => onUpdate(index, { password: e.target.value })}
|
||||
onChange={(e) => updateCredential({ password: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={() => onRemove(index)}
|
||||
onClick={() => onRemove(credential.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
{t('delete')}
|
||||
@@ -199,10 +213,11 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra
|
||||
|
||||
export function GitSettings() {
|
||||
const { t } = useTranslation();
|
||||
const { settings, addCredential, removeCredential, updateCredential, setCredentials, updateSettings, isInitialized } = useGitSettings();
|
||||
const { settings, addCredential, removeCredential, setCredentials, updateSettings, isInitialized } = useGitSettings();
|
||||
const { initiateOAuth, isLoading: isOAuthLoading, error: oauthError, isOAuthConfigured } = useGitHubOAuth();
|
||||
const isMobile = useIsMobile();
|
||||
const navigate = useNavigate();
|
||||
const [customName, setCustomName] = useState('');
|
||||
const [customOrigin, setCustomOrigin] = useState('');
|
||||
const [customUsername, setCustomUsername] = useState('');
|
||||
const [customPassword, setCustomPassword] = useState('');
|
||||
@@ -240,6 +255,8 @@ export function GitSettings() {
|
||||
|
||||
const parsed = parseOrigin(preset.origin);
|
||||
const newCredential: GitCredential = {
|
||||
id: crypto.randomUUID(),
|
||||
name: preset.name,
|
||||
...parsed,
|
||||
username: preset.username,
|
||||
password: token.trim(),
|
||||
@@ -256,10 +273,13 @@ export function GitSettings() {
|
||||
};
|
||||
|
||||
const handleAddCustomProvider = () => {
|
||||
if (!customOrigin.trim() || !customUsername.trim() || !customPassword.trim()) return;
|
||||
if (!customName.trim() || !customOrigin.trim() || !customUsername.trim() || !customPassword.trim()) return;
|
||||
|
||||
const parsed = parseOrigin(customOrigin.trim());
|
||||
|
||||
const newCredential: GitCredential = {
|
||||
id: crypto.randomUUID(),
|
||||
name: customName.trim(),
|
||||
...parsed,
|
||||
username: customUsername.trim(),
|
||||
password: customPassword.trim(),
|
||||
@@ -268,27 +288,31 @@ export function GitSettings() {
|
||||
// Auto-save: Add credential immediately to persistent storage
|
||||
addCredential(newCredential);
|
||||
|
||||
setCustomName('');
|
||||
setCustomOrigin('');
|
||||
setCustomUsername('');
|
||||
setCustomPassword('');
|
||||
};
|
||||
|
||||
const handleRemoveCredential = (index: number) => {
|
||||
const handleRemoveCredential = (id: string) => {
|
||||
// Auto-save: Remove credential immediately from persistent storage
|
||||
removeCredential(index);
|
||||
removeCredential(id);
|
||||
};
|
||||
|
||||
const handleUpdateCredential = (index: number, credential: Partial<GitCredential>) => {
|
||||
const handleUpdateCredential = (updatedCredential: GitCredential) => {
|
||||
// Auto-save: Update credential immediately in persistent storage
|
||||
updateCredential(index, credential);
|
||||
const updatedCredentials = settings.credentials.map((cred) =>
|
||||
cred.id === updatedCredential.id ? updatedCredential : cred
|
||||
);
|
||||
setCredentials(updatedCredentials);
|
||||
};
|
||||
|
||||
const handleDragEnd = (event: DragEndEvent) => {
|
||||
const { active, over } = event;
|
||||
|
||||
if (active.id !== over?.id) {
|
||||
const oldIndex = settings.credentials.findIndex((_, i) => `credential-${i}` === active.id);
|
||||
const newIndex = settings.credentials.findIndex((_, i) => `credential-${i}` === over?.id);
|
||||
const oldIndex = settings.credentials.findIndex((cred) => cred.id === active.id);
|
||||
const newIndex = settings.credentials.findIndex((cred) => cred.id === over?.id);
|
||||
|
||||
if (oldIndex !== -1 && newIndex !== -1) {
|
||||
const newCredentials = arrayMove(settings.credentials, oldIndex, newIndex);
|
||||
@@ -385,15 +409,14 @@ export function GitSettings() {
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<SortableContext
|
||||
items={settings.credentials.map((_, i) => `credential-${i}`)}
|
||||
items={settings.credentials.map((cred) => cred.id)}
|
||||
strategy={verticalListSortingStrategy}
|
||||
>
|
||||
<Accordion type="multiple" className="w-full space-y-2">
|
||||
{settings.credentials.map((credential, index) => (
|
||||
{settings.credentials.map((credential) => (
|
||||
<SortableCredentialItem
|
||||
key={`credential-${index}`}
|
||||
key={credential.id}
|
||||
credential={credential}
|
||||
index={index}
|
||||
onUpdate={handleUpdateCredential}
|
||||
onRemove={handleRemoveCredential}
|
||||
showDragHandle={settings.credentials.length > 1}
|
||||
@@ -553,6 +576,17 @@ export function GitSettings() {
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="My Git Server"
|
||||
value={customName}
|
||||
onChange={(e) => setCustomName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-origin">
|
||||
{t('origin')} <span className="text-destructive">*</span>
|
||||
@@ -589,10 +623,11 @@ export function GitSettings() {
|
||||
<Button
|
||||
onClick={handleAddCustomProvider}
|
||||
disabled={
|
||||
!customName.trim() ||
|
||||
!customOrigin.trim() ||
|
||||
!customUsername.trim() ||
|
||||
!customPassword.trim() ||
|
||||
customOrigin.trim() in settings.credentials
|
||||
!customUsername.trim() ||
|
||||
!customPassword.trim() ||
|
||||
customOrigin.trim() in settings.credentials
|
||||
}
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
|
||||
Reference in new issue
Block a user