import { useState, useEffect } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { Label } from '@/components/ui/label'; import { Loader2, Trash2, AlertTriangle, FileWarning } from 'lucide-react'; import { getElectronAPI } from '@/lib/electron'; import { toast } from 'sonner'; interface WorktreeInfo { path: string; branch: string; isMain: boolean; hasChanges?: boolean; changedFilesCount?: number; } interface DeleteWorktreeDialogProps { open: boolean; onOpenChange: (open: boolean) => void; projectPath: string; worktree: WorktreeInfo | null; onDeleted: (deletedWorktree: WorktreeInfo, deletedBranch: boolean) => void; /** Number of features assigned to this worktree's branch */ affectedFeatureCount?: number; /** Default value for the "delete branch" checkbox */ defaultDeleteBranch?: boolean; } export function DeleteWorktreeDialog({ open, onOpenChange, projectPath, worktree, onDeleted, affectedFeatureCount = 0, defaultDeleteBranch = false, }: DeleteWorktreeDialogProps) { const [deleteBranch, setDeleteBranch] = useState(defaultDeleteBranch); const [isLoading, setIsLoading] = useState(false); // Reset deleteBranch to default when dialog opens useEffect(() => { if (open) { setDeleteBranch(defaultDeleteBranch); } }, [open, defaultDeleteBranch]); const handleDelete = async () => { if (!worktree) return; setIsLoading(true); try { const api = getElectronAPI(); if (!api?.worktree?.delete) { toast.error('Worktree API not available'); return; } const result = await api.worktree.delete(projectPath, worktree.path, deleteBranch); if (result.success) { toast.success(`Worktree deleted`, { description: deleteBranch ? `Branch "${worktree.branch}" was also deleted` : `Branch "${worktree.branch}" was kept`, }); onDeleted(worktree, deleteBranch); onOpenChange(false); setDeleteBranch(false); } else { toast.error('Failed to delete worktree', { description: result.error, }); } } catch (err) { toast.error('Failed to delete worktree', { description: err instanceof Error ? err.message : 'Unknown error', }); } finally { setIsLoading(false); } }; if (!worktree) return null; return ( Delete Worktree Are you sure you want to delete the worktree for branch{' '} {worktree.branch}? {affectedFeatureCount > 0 && (
{affectedFeatureCount} feature{affectedFeatureCount !== 1 ? 's' : ''}{' '} {affectedFeatureCount !== 1 ? 'are' : 'is'} assigned to this branch.{' '} {affectedFeatureCount !== 1 ? 'They' : 'It'} will be unassigned and moved to the main worktree.
)} {worktree.hasChanges && (
This worktree has {worktree.changedFilesCount} uncommitted change(s). These will be lost if you proceed.
)}
setDeleteBranch(checked === true)} />
); }