import { useState } from 'react' import { useMutation, useQueryClient } from '@tanstack/react-query' import { createRepo, discoverRepos } from '@/api/repos' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs' import { Loader2 } from 'lucide-react' import { showToast } from '@/lib/toast' import type { DiscoverReposResponse } from '@opencode-manager/shared/types' import type { Repo } from '@/api/types' interface AddRepoDialogProps { open: boolean onOpenChange: (open: boolean) => void } export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) { const [repoType, setRepoType] = useState<'remote' | 'local' | 'folder'>('remote') const [repoUrl, setRepoUrl] = useState('') const [localPath, setLocalPath] = useState('') const [folderPath, setFolderPath] = useState('') const [branch, setBranch] = useState('') const [skipSSHVerification, setSkipSSHVerification] = useState(false) const queryClient = useQueryClient() const isSSHUrl = (url: string): boolean => { return url.startsWith('git@') || url.startsWith('ssh://') } const showSkipSSHCheckbox = repoType === 'remote' && isSSHUrl(repoUrl) type AddRepoResult = | { mode: 'single'; repo: Repo } | ({ mode: 'discover' } & DiscoverReposResponse) const mutation = useMutation({ mutationFn: async (): Promise => { if (repoType === 'local') { const repo = await createRepo(undefined, localPath, branch || undefined, undefined, false) return { mode: 'single', repo } } if (repoType === 'folder') { const result = await discoverRepos(folderPath) return { mode: 'discover', ...result } } const repo = await createRepo(repoUrl, undefined, branch || undefined, undefined, false, skipSSHVerification) return { mode: 'single', repo } }, onSuccess: (result) => { queryClient.invalidateQueries({ queryKey: ['repos'] }) queryClient.invalidateQueries({ queryKey: ['reposGitStatus'] }) setRepoUrl('') setLocalPath('') setFolderPath('') setBranch('') setRepoType('remote') setSkipSSHVerification(false) if (result.mode === 'discover') { const summary = [ result.discoveredCount > 0 ? `${result.discoveredCount} new` : null, result.existingCount > 0 ? `${result.existingCount} existing` : null, ].filter(Boolean).join(', ') if (result.errors.length > 0) { showToast.warning('Repository discovery completed with issues', { description: `${summary || 'No repos imported'}. ${result.errors[0]?.error || 'Some folders could not be imported.'}`, }) } else if (result.discoveredCount === 0 && result.existingCount === 0) { showToast.info('No Git repositories found in that folder') } else { showToast.success('Repository discovery complete', { description: summary, }) } } else { showToast.success('Repository added') } onOpenChange(false) }, }) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if ((repoType === 'remote' && repoUrl) || (repoType === 'local' && localPath) || (repoType === 'folder' && folderPath)) { mutation.mutate() } } const handleRepoUrlChange = (value: string) => { setRepoUrl(value) if (!isSSHUrl(value)) { setSkipSSHVerification(false) } } return ( Add Repository
setRepoType(value as 'remote' | 'local' | 'folder')}> Remote Local Folder
{repoType === 'remote' ? (
handleRepoUrlChange(e.target.value)} disabled={mutation.isPending} className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base" />

Full URL or shorthand format (owner/repo for GitHub)

) : repoType === 'local' ? (
setLocalPath(e.target.value)} disabled={mutation.isPending} className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base" />

Directory name for a new repo, or an absolute path to link an existing Git repo

) : (
setFolderPath(e.target.value)} disabled={mutation.isPending} className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base" />

Scans the folder for nested Git repositories and links each one

)}
setBranch(e.target.value)} disabled={mutation.isPending || repoType === 'folder'} className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base" />

{repoType === 'folder' ? 'Links each repository on its current branch' : branch ? `Uses '${branch}' branch` : 'Uses default branch' }

{showSkipSSHCheckbox && (
setSkipSSHVerification(e.target.checked)} disabled={mutation.isPending} className="mt-1 h-5 w-5 rounded border-[#2a2a2a] bg-[#1a1a1a] text-blue-600 focus:ring-blue-600" />

Auto-accept the SSH host key for self-hosted or internal servers

)} {mutation.isError && (

{mutation.error.message}

)}
) }