import { useState, useEffect } from 'react' import { useSettings } from '@/hooks/useSettings' import { Loader2, Plus, Trash2, Save, User, Key, Pencil } from 'lucide-react' import { showToast } from '@/lib/toast' import { GitCredentialDialog } from './GitCredentialDialog' import { Label } from '@/components/ui/label' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import type { GitCredential, GitIdentity } from '@/api/types/settings' export function GitSettings() { const { preferences, isLoading, updateSettingsAsync, isUpdating } = useSettings() const [gitCredentials, setGitCredentials] = useState([]) const [gitIdentity, setGitIdentity] = useState({ name: '', email: '' }) const [isSaving, setIsSaving] = useState(false) const [hasChanges, setHasChanges] = useState(false) const [isCredentialDialogOpen, setIsCredentialDialogOpen] = useState(false) const [editingCredentialIndex, setEditingCredentialIndex] = useState(null) useEffect(() => { if (preferences) { setGitCredentials(preferences.gitCredentials || []) setGitIdentity(preferences.gitIdentity || { name: '', email: '' }) setHasChanges(false) } }, [preferences]) const checkForIdentityChanges = (newIdentity: GitIdentity) => { const currentIdentity = preferences?.gitIdentity || { name: '', email: '' } const identityChanged = currentIdentity.name !== newIdentity.name || currentIdentity.email !== newIdentity.email setHasChanges(identityChanged) } const openAddCredentialDialog = () => { setEditingCredentialIndex(null) setIsCredentialDialogOpen(true) } const openEditCredentialDialog = (index: number) => { setEditingCredentialIndex(index) setIsCredentialDialogOpen(true) } const handleEditClick = (e: React.MouseEvent, index: number) => { e.preventDefault() e.stopPropagation() openEditCredentialDialog(index) } const handleDeleteClick = (e: React.MouseEvent, index: number) => { e.preventDefault() e.stopPropagation() removeCredential(index) } const saveCredential = async (credential: GitCredential) => { let newCredentials: GitCredential[] if (editingCredentialIndex !== null) { newCredentials = [...gitCredentials] newCredentials[editingCredentialIndex] = credential } else { newCredentials = [...gitCredentials, credential] } setGitCredentials(newCredentials) try { await updateSettingsAsync({ gitCredentials: newCredentials, gitIdentity }) showToast.success('Credential saved') } catch { showToast.error('Failed to save credential') } } const removeCredential = async (index: number) => { const newCredentials = gitCredentials.filter((_, i) => i !== index) setGitCredentials(newCredentials) try { await updateSettingsAsync({ gitCredentials: newCredentials, gitIdentity }) showToast.success('Credential deleted') } catch { showToast.error('Failed to delete credential') } } const updateIdentity = (field: keyof GitIdentity, value: string) => { const newIdentity = { ...gitIdentity, [field]: value } setGitIdentity(newIdentity) checkForIdentityChanges(newIdentity) } const saveAll = async () => { setIsSaving(true) try { showToast.loading('Saving git configuration...', { id: 'git-config' }) const result = await updateSettingsAsync({ gitCredentials, gitIdentity }) setHasChanges(false) if (result.reloadError) { showToast.success('Git configuration saved (server reload pending)', { id: 'git-config' }) } else { showToast.success('Git configuration saved', { id: 'git-config' }) } } catch { showToast.error('Failed to save git configuration', { id: 'git-config' }) } finally { setIsSaving(false) } } if (isLoading) { return (
) } return (

Git Configuration

Manage your git identity and credentials for repository operations

{hasChanges && ( )}
Identity {gitIdentity.name || gitIdentity.email ? `${gitIdentity.name || 'No name'} <${gitIdentity.email || 'No email'}>` : 'Not configured'}

Author identity used for git commits. Leave empty to use system defaults.

updateIdentity('name', e.target.value)} disabled={isSaving} className="bg-background border-border text-foreground placeholder:text-muted-foreground" />
updateIdentity('email', e.target.value)} disabled={isSaving} className="bg-background border-border text-foreground placeholder:text-muted-foreground" />
Credentials {gitCredentials.length} configured

Credentials for cloning private repositories

{gitCredentials.length === 0 ? (

No credentials configured. Click "Add" to add credentials.

) : (
{gitCredentials.map((cred, index) => ( ))}
Name Host Type Actions
{cred.name || 'Unnamed'}
{cred.host}
{cred.host} {cred.type === 'ssh' ? 'SSH' : 'PAT'}
)}
) }