|
| 1 | +import { useState, useEffect } from 'react' |
| 2 | +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' |
| 3 | +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' |
| 4 | +import { Button } from '@/components/ui/button' |
| 5 | +import { Input } from '@/components/ui/input' |
| 6 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' |
| 7 | +import { AlertCircle, GitBranch, Loader2 } from 'lucide-react' |
| 8 | +import { createRepo, listBranches } from '@/api/repos' |
| 9 | +import { showToast } from '@/lib/toast' |
| 10 | + |
| 11 | +interface CreateWorktreeDialogProps { |
| 12 | + open: boolean |
| 13 | + onOpenChange: (open: boolean) => void |
| 14 | + repoId: number |
| 15 | + repoUrl?: string | null |
| 16 | + defaultBaseBranch?: string |
| 17 | + onCreated?: () => void |
| 18 | +} |
| 19 | + |
| 20 | +export function CreateWorktreeDialog({ |
| 21 | + open, |
| 22 | + onOpenChange, |
| 23 | + repoId, |
| 24 | + repoUrl, |
| 25 | + defaultBaseBranch, |
| 26 | + onCreated, |
| 27 | +}: CreateWorktreeDialogProps) { |
| 28 | + const queryClient = useQueryClient() |
| 29 | + const [branchName, setBranchName] = useState('') |
| 30 | + const [baseBranch, setBaseBranch] = useState<string>('') |
| 31 | + const [error, setError] = useState<string | null>(null) |
| 32 | + |
| 33 | + const canCreate = Boolean(repoUrl) |
| 34 | + |
| 35 | + const { data: branchesData, isLoading: branchesLoading } = useQuery({ |
| 36 | + queryKey: ['branches', repoId], |
| 37 | + queryFn: () => listBranches(repoId), |
| 38 | + enabled: open && canCreate, |
| 39 | + staleTime: 30000, |
| 40 | + }) |
| 41 | + |
| 42 | + const localBranches = (branchesData?.branches ?? []).filter((b) => b.type === 'local') |
| 43 | + const remoteBranches = (branchesData?.branches ?? []) |
| 44 | + .filter((b) => b.type === 'remote') |
| 45 | + .map((b) => ({ ...b, shortName: b.name.replace(/^remotes\/[^/]+\//, '') })) |
| 46 | + .filter((b) => !localBranches.some((lb) => lb.name === b.shortName)) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (!open) { |
| 50 | + setBranchName('') |
| 51 | + setBaseBranch('') |
| 52 | + setError(null) |
| 53 | + return |
| 54 | + } |
| 55 | + if (defaultBaseBranch) { |
| 56 | + setBaseBranch(defaultBaseBranch) |
| 57 | + } |
| 58 | + }, [open, defaultBaseBranch]) |
| 59 | + |
| 60 | + const worktreeMutation = useMutation({ |
| 61 | + mutationFn: (payload: { branch: string; base: string }) => |
| 62 | + createRepo({ |
| 63 | + repoUrl: repoUrl || undefined, |
| 64 | + branch: payload.branch, |
| 65 | + useWorktree: true, |
| 66 | + baseBranch: payload.base, |
| 67 | + }), |
| 68 | + onSuccess: () => { |
| 69 | + queryClient.invalidateQueries({ queryKey: ['repos'] }) |
| 70 | + queryClient.invalidateQueries({ queryKey: ['reposGitStatus'] }) |
| 71 | + showToast.success('Worktree created') |
| 72 | + onCreated?.() |
| 73 | + onOpenChange(false) |
| 74 | + }, |
| 75 | + onError: (err) => { |
| 76 | + setError(err instanceof Error ? err.message : 'Failed to create worktree') |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + const handleCreate = () => { |
| 81 | + const trimmed = branchName.trim() |
| 82 | + if (!trimmed) { |
| 83 | + setError('Branch name is required') |
| 84 | + return |
| 85 | + } |
| 86 | + if (!baseBranch) { |
| 87 | + setError('Base branch is required') |
| 88 | + return |
| 89 | + } |
| 90 | + setError(null) |
| 91 | + worktreeMutation.mutate({ branch: trimmed, base: baseBranch }) |
| 92 | + } |
| 93 | + |
| 94 | + return ( |
| 95 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 96 | + <DialogContent className="sm:max-w-[440px]"> |
| 97 | + <DialogHeader> |
| 98 | + <DialogTitle className="flex items-center gap-2"> |
| 99 | + <GitBranch className="w-4 h-4" /> |
| 100 | + Create Worktree |
| 101 | + </DialogTitle> |
| 102 | + <DialogDescription> |
| 103 | + Create a separate workspace for a new branch. The worktree is managed as its own repo entry. |
| 104 | + </DialogDescription> |
| 105 | + </DialogHeader> |
| 106 | + |
| 107 | + <div className="space-y-4"> |
| 108 | + {!canCreate ? ( |
| 109 | + <div className="flex items-start gap-2 bg-yellow-500/10 border border-yellow-500/30 rounded p-3"> |
| 110 | + <AlertCircle className="w-4 h-4 text-yellow-600 dark:text-yellow-400 mt-0.5 flex-shrink-0" /> |
| 111 | + <p className="text-sm text-yellow-700 dark:text-yellow-300"> |
| 112 | + Worktrees can only be created for repositories with a remote URL. |
| 113 | + </p> |
| 114 | + </div> |
| 115 | + ) : ( |
| 116 | + <> |
| 117 | + <div className="space-y-1.5"> |
| 118 | + <label className="text-sm font-medium">New branch name</label> |
| 119 | + <Input |
| 120 | + placeholder="feature/my-branch" |
| 121 | + value={branchName} |
| 122 | + onChange={(e) => setBranchName(e.target.value)} |
| 123 | + autoFocus |
| 124 | + onKeyDown={(e) => { |
| 125 | + if (e.key === 'Enter' && !worktreeMutation.isPending) handleCreate() |
| 126 | + }} |
| 127 | + /> |
| 128 | + </div> |
| 129 | + |
| 130 | + <div className="space-y-1.5"> |
| 131 | + <label className="text-sm font-medium">Base branch</label> |
| 132 | + <Select value={baseBranch} onValueChange={setBaseBranch} disabled={branchesLoading}> |
| 133 | + <SelectTrigger className="bg-background border-border text-foreground"> |
| 134 | + <SelectValue placeholder={branchesLoading ? 'Loading branches...' : 'Select a base branch'} /> |
| 135 | + </SelectTrigger> |
| 136 | + <SelectContent className="bg-popover border-border"> |
| 137 | + {localBranches.length > 0 && ( |
| 138 | + <> |
| 139 | + {localBranches.map((branch) => ( |
| 140 | + <SelectItem key={`local-${branch.name}`} value={branch.name}> |
| 141 | + <div className="flex items-center gap-2"> |
| 142 | + <GitBranch className="w-3.5 h-3.5" /> |
| 143 | + <span>{branch.name}</span> |
| 144 | + {branch.current && ( |
| 145 | + <span className="text-xs text-muted-foreground">(current)</span> |
| 146 | + )} |
| 147 | + </div> |
| 148 | + </SelectItem> |
| 149 | + ))} |
| 150 | + </> |
| 151 | + )} |
| 152 | + {remoteBranches.map((branch) => ( |
| 153 | + <SelectItem key={`remote-${branch.name}`} value={branch.shortName}> |
| 154 | + <div className="flex items-center gap-2"> |
| 155 | + <GitBranch className="w-3.5 h-3.5 text-blue-500" /> |
| 156 | + <span>{branch.shortName}</span> |
| 157 | + <span className="text-xs text-muted-foreground">(remote)</span> |
| 158 | + </div> |
| 159 | + </SelectItem> |
| 160 | + ))} |
| 161 | + </SelectContent> |
| 162 | + </Select> |
| 163 | + <p className="text-xs text-muted-foreground"> |
| 164 | + The new branch will be created from this branch. Ignored if the branch name already exists locally or on the remote. |
| 165 | + </p> |
| 166 | + </div> |
| 167 | + </> |
| 168 | + )} |
| 169 | + |
| 170 | + {error && ( |
| 171 | + <div className="flex items-start gap-2 bg-red-500/10 border border-red-500/30 rounded p-3"> |
| 172 | + <AlertCircle className="w-4 h-4 text-red-600 dark:text-red-400 mt-0.5 flex-shrink-0" /> |
| 173 | + <p className="text-sm text-red-600 dark:text-red-400">{error}</p> |
| 174 | + </div> |
| 175 | + )} |
| 176 | + |
| 177 | + <div className="flex gap-2 justify-end"> |
| 178 | + <Button |
| 179 | + variant="outline" |
| 180 | + onClick={() => onOpenChange(false)} |
| 181 | + className="border-border hover:bg-accent" |
| 182 | + > |
| 183 | + Cancel |
| 184 | + </Button> |
| 185 | + <Button |
| 186 | + onClick={handleCreate} |
| 187 | + disabled={!canCreate || !branchName.trim() || !baseBranch || worktreeMutation.isPending} |
| 188 | + className="bg-blue-600 hover:bg-blue-700 disabled:opacity-50" |
| 189 | + > |
| 190 | + {worktreeMutation.isPending ? ( |
| 191 | + <> |
| 192 | + <Loader2 className="w-4 h-4 mr-2 animate-spin" /> |
| 193 | + Creating... |
| 194 | + </> |
| 195 | + ) : ( |
| 196 | + 'Create Worktree' |
| 197 | + )} |
| 198 | + </Button> |
| 199 | + </div> |
| 200 | + </div> |
| 201 | + </DialogContent> |
| 202 | + </Dialog> |
| 203 | + ) |
| 204 | +} |
0 commit comments