"use client" import { useEffect, useState } from "react" import { Check, ChevronDown, ChevronRight, Cloud, FolderGit2, GitBranch, GitPullRequest, Laptop, Loader2, Search, } from "lucide-react" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { type GitBranch as Branch, type GitProject, type GitWorkspace, } from "../git-workspace" import type { GitWorkspaceState } from "../use-git-workspace" const row = "flex w-full min-w-0 items-center gap-2 rounded px-2 py-2 text-left text-xs text-[#ddd] hover:bg-white/5 disabled:opacity-40" const trigger = "flex min-w-0 items-center gap-1.5 rounded px-1.5 py-1 text-xs text-[#999] hover:bg-white/5 disabled:opacity-40" const panel = "max-w-[calc(100vw-2rem)] border-white/10 bg-[#252525] p-1 text-[#ddd]" export function GitWorkspaceToolbar({ state, disabled, repositoryFullName, onSettings, onOpenWorkspace, workspaceLabels = {}, }: { state: GitWorkspaceState disabled: boolean repositoryFullName?: string | null onSettings: () => void onOpenWorkspace?: (threadId: string) => void workspaceLabels?: Record }) { const [machineOpen, setMachineOpen] = useState(false) const [branchOpen, setBranchOpen] = useState(false) const [existingOpen, setExistingOpen] = useState(false) const [projects, setProjects] = useState([]) const [branches, setBranches] = useState([]) const [existing, setExisting] = useState([]) const [query, setQuery] = useState("") const [worktreeQuery, setWorktreeQuery] = useState("") const [busy, setBusy] = useState(false) const [error, setError] = useState(null) const { client, choice, workspace, locked } = state const projectId = workspace?.projectId ?? choice?.projectId const project = projectId ? projects.find((item) => item.id === projectId) : (projects.find( (item) => repositoryFullName && item.repositoryFullName?.toLowerCase() === repositoryFullName.toLowerCase(), ) ?? (!repositoryFullName ? projects[0] : undefined)) useEffect(() => { if ((!machineOpen && !branchOpen) || !client.connected) return let current = true void client .projects() .then(async (items) => { const selected = projectId ? items.find((item) => item.id === projectId) : (items.find( (item) => repositoryFullName && item.repositoryFullName?.toLowerCase() === repositoryFullName.toLowerCase(), ) ?? (!repositoryFullName ? items[0] : undefined)) const refs = selected ? await client.branches(selected.id) : [] if (current) { setProjects(items) setBranches(refs) setError(null) } }) .catch((cause) => { if (current) setError(cause.message) }) return () => { current = false } }, [machineOpen, branchOpen, client, projectId, repositoryFullName]) function settings() { setMachineOpen(false) setBranchOpen(false) onSettings() } function select(mode: "worktree" | "current", ref?: string) { if (!client.connected || !project) { settings() return } state.select({ projectId: project.id, sourceRef: mode === "current" ? (branches.find((branch) => branch.current)?.ref ?? "") : (ref ?? choice?.sourceRef ?? branches.find((branch) => branch.current)?.ref ?? branches[0]?.ref ?? ""), mode, copyIgnored: choice?.copyIgnored ?? false, ...(mode === "worktree" && choice?.newBranch ? { newBranch: choice.newBranch } : {}), }) setMachineOpen(false) setBranchOpen(false) } async function openExisting(saved: GitWorkspace) { setBusy(true) setError(null) try { const actual = await client.get(saved.id) if ( actual.id !== saved.id || actual.threadId !== saved.threadId || actual.projectId !== saved.projectId || actual.baseSha !== saved.baseSha || actual.sourceRef !== saved.sourceRef || actual.mode !== saved.mode ) throw new Error( "Worktree identity changed; reconnect its original companion", ) onOpenWorkspace?.(actual.threadId) setMachineOpen(false) } catch (cause) { setError(cause instanceof Error ? cause.message : "Worktree unavailable") } finally { setBusy(false) } } const mode = workspace?.mode ?? choice?.mode const source = workspace?.branch ?? workspace?.sourceRef ?? choice?.sourceRef ?? branches.find((branch) => branch.current)?.ref const branchLabel = source?.replace(/^refs\/(?:heads|remotes)\//, "") || "Choose branch" return (
{ setMachineOpen(value) setExistingOpen(false) setQuery("") }} >

Machine

setWorktreeQuery(event.target.value)} className="w-full min-w-0 border-b border-white/10 bg-transparent px-3 py-2 text-xs outline-none" />

Continue a saved worktree’s existing thread.

{existing .filter((item) => `${workspaceLabels[item.threadId] ?? ""} ${item.branch} ${item.sourceRef} ${item.threadId}` .toLowerCase() .includes(worktreeQuery.toLowerCase()), ) .map((item) => ( ))}
{!existing.length && (

No saved worktrees for this project in this browser.

)}
{error && (

{error}

)} { setBranchOpen(value) setQuery("") }} >
{branches .filter((branch) => branch.name.toLowerCase().includes(query.toLowerCase()), ) .map((branch) => ( ))}
{!branches.length && (

{client.connected ? "No matching project branches. Open workspace settings." : "Connect this computer to load its branches."}

)} {locked && (

This thread’s starting branch is fixed. Start a new thread to change it.

)}
{error && (

{error}

)}
{workspace && ( )}
) }