"use client" import { useEffect, useState } from "react" import { Check, GitBranch, Laptop, Loader2, Search, } from "lucide-react" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import type { GitWorkspaceState } from "../use-git-workspace" import { GitWorkspaceToolbar } from "./git-workspace-toolbar" import type { GitBranch as Branch, GitProject, GitStatus, PublishInput, PublishPreview, PublishResult, } from "../git-workspace" const field = "w-full min-w-0 rounded-md border border-white/10 bg-[#121212] px-3 py-2 text-xs text-[#ddd] outline-none focus:border-[#777]" const button = "inline-flex items-center justify-center gap-2 rounded-md border border-white/10 px-3 py-2 text-xs text-[#ddd] transition hover:bg-white/5 disabled:opacity-40" export function GitWorkspaceControls({ state, disabled = false, repositoryFullName, onOpenWorkspace, workspaceLabels, }: { state: GitWorkspaceState disabled?: boolean repositoryFullName?: string | null onOpenWorkspace?: (threadId: string) => void workspaceLabels?: Record }) { const [open, setOpen] = useState(false) const [port, setPort] = useState("47832") const [code, setCode] = useState("") const [projects, setProjects] = useState([]) const [projectId, setProjectId] = useState(state.choice?.projectId ?? "") const [branches, setBranches] = useState([]) const [sourceRef, setSourceRef] = useState(state.choice?.sourceRef ?? "") const [mode, setMode] = useState<"current" | "worktree">( state.choice?.mode ?? "worktree", ) const [newBranch, setNewBranch] = useState("") const [copyIgnored, setCopyIgnored] = useState(false) const [ignoredFiles, setIgnoredFiles] = useState([]) const [query, setQuery] = useState("") const [busy, setBusy] = useState(false) const [error, setError] = useState(null) const [initConfirm, setInitConfirm] = useState(false) const { client, choice, workspace } = state async function loadProjects() { const items = await client.projects() setProjects(items) const selected = items.find((item) => item.id === choice?.projectId) ?? items.find( (item) => repositoryFullName && item.repositoryFullName?.toLowerCase() === repositoryFullName.toLowerCase(), ) ?? items[0] setProjectId(selected?.id ?? "") } useEffect(() => { if (!open || !client.connected) return let current = true void client .projects() .then((items) => { if (!current) return setProjects(items) const selected = items.find((item) => item.id === choice?.projectId) ?? items.find( (item) => repositoryFullName && item.repositoryFullName?.toLowerCase() === repositoryFullName.toLowerCase(), ) ?? items[0] setProjectId(selected?.id ?? "") }) .catch((cause) => { if (current) setError(cause.message) }) return () => { current = false } }, [open, client, choice?.projectId, repositoryFullName]) useEffect(() => { if (!open || !projectId || !client.connected) return let current = true setBranches([]) setIgnoredFiles([]) setCopyIgnored(false) void client.ignoredFiles(projectId).then(paths => { if (current) setIgnoredFiles(paths) }).catch(cause => { if (current) setError(cause.message) }) void client .branches(projectId) .then((items) => { if (!current) return setBranches(items) setSourceRef( items.find((item) => item.ref === choice?.sourceRef)?.ref ?? items.find((item) => item.current)?.ref ?? items[0]?.ref ?? "", ) }) .catch((cause) => { if (current) setError(cause.message) }) return () => { current = false } }, [open, client, projectId, choice?.sourceRef]) async function perform(operation: () => Promise) { setBusy(true) setError(null) try { await operation() } catch (cause) { setError( cause instanceof Error ? cause.message : "Workspace request failed", ) } finally { setBusy(false) } } const project = projects.find((item) => item.id === projectId) return ( <> setOpen(true)} onOpenWorkspace={onOpenWorkspace} workspaceLabels={workspaceLabels} /> { setOpen(value) setError(null) }} > Thread workspace Choose the checkout and starting branch. Publishing is always confirmed separately. {workspace && !client.connected && (
{ event.preventDefault() void perform(async () => { await client.pair(Number(port), code.trim()) setCode("") state.refresh() }) }} className="flex gap-2" > setPort(event.target.value)} /> setCode(event.target.value)} />
)} {state.bindingError && (

{state.bindingError}

)} {workspace ? (

Local companion ·{" "} {workspace.mode === "worktree" ? "Isolated worktree" : "Current checkout"}

{workspace.path}

Started from
{workspace.sourceRef}
Starting SHA
{workspace.baseSha.slice(0, 12) || "Unborn repository"}
Working branch
{workspace.branch ?? "Detached until publishing"}

This thread keeps its original workspace. Start a new thread to choose a different branch.

) : (
Local branches require Git on the execution computer—not browser folder access. Start the workspace companion from your Nova checkout:
                  bun run workspace:local --project /path/to/project --origin{" "}
                  {typeof window !== "undefined"
                    ? window.location.origin
                    : "https://nova.supercodeai.tech"}
                

Use a local clone for a connected GitHub repository. Unpushed local branches are available on that computer only. No files or credentials are sent until you explicitly work with them.

{!client.connected ? (
{ event.preventDefault() void perform(async () => { await client.pair(Number(port), code.trim()) setCode("") await loadProjects() state.refresh() }) }} className="flex flex-wrap items-end gap-2" >
) : ( <>
Local companion connected
{repositoryFullName && project && project.repositoryFullName?.toLowerCase() !== repositoryFullName.toLowerCase() && (

This folder does not match {repositoryFullName}. Choose its clone or select the matching project in Nova before starting.

)} {project && !project.isGit ? (

This folder has no Git repository. Initialization does not commit any files.

) : ( <>
{branches .filter((branch) => `${branch.name} ${branch.ref}` .toLowerCase() .includes(query.toLowerCase()), ) .map((branch) => ( ))} {!branches.length && (

No commits yet. Use Current checkout for your initial commit.

)}
{mode === "worktree" ? ( <> {ignoredFiles.length > 0 &&

Files selected by .worktreeinclude:

    {ignoredFiles.map(path =>
  • {path}
  • )}
}

Only committed files from the selected branch are included. Original staged and unstaged changes stay in the original checkout.

) : (

Nova’s proposed edits will be applied to this folder itself. The current branch and any existing changes stay in place.

)} {state.locked && (

Start a new thread to select a starting branch. For a bound thread, connect its original companion to restore the workspace.

)} )} )} {choice && !state.locked && ( )}
)} {error && (

{error}

)}
) } function PublishDialog({ state, disabled, }: { state: GitWorkspaceState disabled: boolean }) { const [status, setStatus] = useState(null) const [files, setFiles] = useState([]) const [branch, setBranch] = useState("") const [baseBranch, setBaseBranch] = useState("") const [remote, setRemote] = useState("origin") const [message, setMessage] = useState("") const [title, setTitle] = useState("") const [body, setBody] = useState("") const [preview, setPreview] = useState(null) const [result, setResult] = useState(null) const [error, setError] = useState(null) const [busy, setBusy] = useState(false) const workspace = state.workspace const action = state.publishAction useEffect(() => { if (!action || !workspace) return let active = true setStatus(null) setPreview(null) setResult(null) setError(null) setFiles([]) setBranch(workspace.mode === "worktree" && workspace.branch ? workspace.branch : `nova/work-${workspace.threadId.slice(-8)}`) setBaseBranch( workspace.sourceRef .replace(/^refs\/heads\//, "") .replace(/^refs\/remotes\/[^/]+\//, ""), ) void state.client .status(workspace.id) .then((value) => { if (active) { setStatus(value) setFiles(value.files.map((file) => file.path)) } }) .catch((cause) => { if (active) setError(cause.message) }) return () => { active = false } }, [action, workspace, state.client]) async function review() { if (!workspace || !action) return setBusy(true) setError(null) try { setPreview( await state.client.preview({ workspaceId: workspace.id, action, branch, baseBranch, remote, message, files, title, body, }), ) } catch (cause) { setError( cause instanceof Error ? cause.message : "Could not prepare publication", ) } finally { setBusy(false) } } return ( { if (!value && !busy) { state.requestPublish(null) state.refresh() } }} > {result ? "Published" : preview ? "Confirm this exact publication" : "Commit, push, or create a pull request"} Nothing is committed or pushed until you confirm the preview. Changes to the workspace invalidate approval. {workspace?.mode === "current" &&

Current-checkout publication creates the approved branch without switching or staging your original checkout. Use an isolated worktree to continue editing directly on the published branch.

} {result ? (

{result.commitSha.slice(0, 12)} on{" "} {result.branch}

{result.prUrl && ( Open pull request )}
) : preview ? (
Action
{preview.action}
Destination
{preview.remote} / {preview.branch}
{preview.action === "pr" && ( <>
PR base
{preview.baseBranch}
)}
Commit
{preview.message || "No new commit"}
Files
{preview.files.length} selected
              {preview.diff ||
                "No uncommitted changes selected. Existing commits will be published."}
            

Approval expires at{" "} {new Date(preview.expiresAt).toLocaleTimeString()}. HEAD, files, and remote state are checked again before execution.

) : (
{ event.preventDefault() void review() }} className="space-y-3" >
{action === "pr" && ( <>