diff --git a/src/components/GitSettingsProvider.tsx b/src/components/GitSettingsProvider.tsx index 4294e0fa..5d95d138 100644 --- a/src/components/GitSettingsProvider.tsx +++ b/src/components/GitSettingsProvider.tsx @@ -82,6 +82,13 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) { }); }; + const setCredentials = (credentials: GitCredential[]) => { + setSettings(prev => ({ + ...prev, + credentials, + })); + }; + const isConfigured = settings.credentials.length > 0; const contextValue: GitSettingsContextType = { @@ -90,6 +97,7 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) { addCredential, removeCredential, updateCredential, + setCredentials, isConfigured, isInitialized, }; diff --git a/src/contexts/GitSettingsContext.ts b/src/contexts/GitSettingsContext.ts index ffa373e4..77e7efe2 100644 --- a/src/contexts/GitSettingsContext.ts +++ b/src/contexts/GitSettingsContext.ts @@ -20,6 +20,7 @@ export interface GitSettingsContextType { addCredential: (credential: GitCredential) => void; removeCredential: (index: number) => void; updateCredential: (index: number, credential: Partial) => void; + setCredentials: (credentials: GitCredential[]) => void; isConfigured: boolean; isInitialized: boolean; } diff --git a/src/pages/GitSettings.tsx b/src/pages/GitSettings.tsx index 114a3d7d..e67310a7 100644 --- a/src/pages/GitSettings.tsx +++ b/src/pages/GitSettings.tsx @@ -1,6 +1,25 @@ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { Check, GitBranch, ArrowLeft, Trash2, ChevronDown, Plus, User } from 'lucide-react'; +import { Check, GitBranch, ArrowLeft, Trash2, ChevronDown, Plus, User, GripVertical } from 'lucide-react'; +import { + DndContext, + closestCenter, + KeyboardSensor, + PointerSensor, + useSensor, + useSensors, + type DragEndEvent, +} from '@dnd-kit/core'; +import { + arrayMove, + SortableContext, + sortableKeyboardCoordinates, + verticalListSortingStrategy, +} from '@dnd-kit/sortable'; +import { + useSortable, +} from '@dnd-kit/sortable'; +import { CSS } from '@dnd-kit/utilities'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; @@ -49,9 +68,138 @@ const PRESET_PROVIDERS: PresetProvider[] = [ }, ]; +interface SortableCredentialItemProps { + credential: GitCredential; + index: number; + onUpdate: (index: number, credential: Partial) => void; + onRemove: (index: number) => void; + showDragHandle: boolean; +} + +function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDragHandle }: SortableCredentialItemProps) { + const { t } = useTranslation(); + const origin = `${credential.protocol}://${credential.host}`; + const preset = PRESET_PROVIDERS.find(p => p.origin === origin); + const isCustom = !preset; + + const { + attributes, + listeners, + setNodeRef, + transform, + transition, + isDragging, + } = useSortable({ id: `credential-${index}` }); + + const style = { + transform: CSS.Transform.toString(transform), + transition, + opacity: isDragging ? 0.5 : 1, + }; + + // Helper to parse origin URL into credential parts + const parseOrigin = (origin: string): { protocol: string; host: string } => { + try { + const url = new URL(origin); + return { + protocol: url.protocol.replace(':', ''), // Remove trailing colon + host: url.host, // Includes port if non-standard (e.g., "github.com:8080") + }; + } catch { + // Fallback: assume https and treat as hostname + return { + protocol: 'https', + host: origin, + }; + } + }; + + const handleOriginChange = (newOrigin: string) => { + const parsed = parseOrigin(newOrigin); + onUpdate(index, parsed); + }; + + return ( + + +
+ {showDragHandle && ( +
+ +
+ )} + } + /> + + {preset?.name || credential.host} + + {isCustom && {t('custom')}} +
+
+ +
+
+ + handleOriginChange(e.target.value)} + /> +
+
+ + onUpdate(index, { username: e.target.value })} + /> +
+
+ + onUpdate(index, { password: e.target.value })} + /> +
+ +
+
+
+ ); +} + export function GitSettings() { const { t } = useTranslation(); - const { settings, addCredential, removeCredential, updateCredential, updateSettings, isInitialized } = useGitSettings(); + const { settings, addCredential, removeCredential, updateCredential, setCredentials, updateSettings, isInitialized } = useGitSettings(); const { initiateOAuth, isLoading: isOAuthLoading, error: oauthError, isOAuthConfigured } = useGitHubOAuth(); const isMobile = useIsMobile(); const navigate = useNavigate(); @@ -61,6 +209,13 @@ export function GitSettings() { const [presetTokens, setPresetTokens] = useState>({}); const [forceManualEntry, setForceManualEntry] = useState>({}); + const sensors = useSensors( + useSensor(PointerSensor), + useSensor(KeyboardSensor, { + coordinateGetter: sortableKeyboardCoordinates, + }) + ); + // Helper to parse origin URL into credential parts const parseOrigin = (origin: string): { protocol: string; host: string } => { try { @@ -128,6 +283,20 @@ export function GitSettings() { updateCredential(index, credential); }; + 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); + + if (oldIndex !== -1 && newIndex !== -1) { + const newCredentials = arrayMove(settings.credentials, oldIndex, newIndex); + setCredentials(newCredentials); + } + } + }; + // Helper to get origin string from credential const getOriginFromCredential = (cred: GitCredential): string => { return `${cred.protocol}://${cred.host}`; // host already includes port if non-standard @@ -210,77 +379,29 @@ export function GitSettings() { {settings.credentials.length > 0 && (

{t('configuredCredentials')}

- - {settings.credentials.map((credential, index) => { - const origin = getOriginFromCredential(credential); - const preset = PRESET_PROVIDERS.find(p => p.origin === origin); - const isCustom = !preset; - - return ( - - -
- } - /> - - {preset?.name || credential.host} - - {isCustom && {t('custom')}} -
-
- -
-
- - -
-
- - handleUpdateCredential(index, { username: e.target.value })} - /> -
-
- - handleUpdateCredential(index, { password: e.target.value })} - /> -
- -
-
-
- ); - })} -
+ + `credential-${i}`)} + strategy={verticalListSortingStrategy} + > + + {settings.credentials.map((credential, index) => ( + 1} + /> + ))} + + +
)}