"use client"; import { useState } 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 { GitBranch, Loader2 } from "lucide-react"; import { getElectronAPI } from "@/lib/electron"; import { toast } from "sonner"; interface CreatedWorktreeInfo { path: string; branch: string; } interface CreateWorktreeDialogProps { open: boolean; onOpenChange: (open: boolean) => void; projectPath: string; onCreated: (worktree: CreatedWorktreeInfo) => void; } export function CreateWorktreeDialog({ open, onOpenChange, projectPath, onCreated, }: CreateWorktreeDialogProps) { const [branchName, setBranchName] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handleCreate = async () => { if (!branchName.trim()) { setError("Branch name is required"); return; } // Validate branch name (git-compatible) const validBranchRegex = /^[a-zA-Z0-9._/-]+$/; if (!validBranchRegex.test(branchName)) { setError( "Invalid branch name. Use only letters, numbers, dots, underscores, hyphens, and slashes." ); return; } setIsLoading(true); setError(null); try { const api = getElectronAPI(); if (!api?.worktree?.create) { setError("Worktree API not available"); return; } const result = await api.worktree.create(projectPath, branchName); if (result.success && result.worktree) { toast.success( `Worktree created for branch "${result.worktree.branch}"`, { description: result.worktree.isNew ? "New branch created" : "Using existing branch", } ); onCreated({ path: result.worktree.path, branch: result.worktree.branch }); onOpenChange(false); setBranchName(""); } else { setError(result.error || "Failed to create worktree"); } } catch (err) { setError(err instanceof Error ? err.message : "Failed to create worktree"); } finally { setIsLoading(false); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !isLoading && branchName.trim()) { handleCreate(); } }; return ( Create New Worktree Create a new git worktree with its own branch. This allows you to work on multiple features in parallel.
{ setBranchName(e.target.value); setError(null); }} onKeyDown={handleKeyDown} className="font-mono text-sm" autoFocus /> {error &&

{error}

}

Examples:

  • feature/user-auth
  • fix/login-bug
  • hotfix/security-patch
); }