import { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { getElectronAPI } from "@/lib/electron"; import { toast } from "sonner"; import { GitBranchPlus, Loader2 } from "lucide-react"; interface WorktreeInfo { path: string; branch: string; isMain: boolean; hasChanges?: boolean; changedFilesCount?: number; } interface CreateBranchDialogProps { open: boolean; onOpenChange: (open: boolean) => void; worktree: WorktreeInfo | null; onCreated: () => void; } export function CreateBranchDialog({ open, onOpenChange, worktree, onCreated, }: CreateBranchDialogProps) { const [branchName, setBranchName] = useState(""); const [isCreating, setIsCreating] = useState(false); const [error, setError] = useState(null); // Reset state when dialog opens/closes useEffect(() => { if (open) { setBranchName(""); setError(null); } }, [open]); const handleCreate = async () => { if (!worktree || !branchName.trim()) return; // Basic validation const invalidChars = /[\s~^:?*[\]\\]/; if (invalidChars.test(branchName)) { setError("Branch name contains invalid characters"); return; } setIsCreating(true); setError(null); try { const api = getElectronAPI(); if (!api?.worktree?.checkoutBranch) { toast.error("Branch API not available"); return; } const result = await api.worktree.checkoutBranch(worktree.path, branchName.trim()); if (result.success && result.result) { toast.success(result.result.message); onCreated(); onOpenChange(false); } else { setError(result.error || "Failed to create branch"); } } catch (err) { console.error("Create branch failed:", err); setError("Failed to create branch"); } finally { setIsCreating(false); } }; return ( Create New Branch Create a new branch from {worktree?.branch || "current branch"}
{ setBranchName(e.target.value); setError(null); }} onKeyDown={(e) => { if (e.key === "Enter" && branchName.trim() && !isCreating) { handleCreate(); } }} disabled={isCreating} autoFocus /> {error && (

{error}

)}
); }