/** * @file "Create branch here…" and "Create tag here…" from a History commit's * menu: a name (and, for a tag, an optional message making it annotated), * then git-refs.js's `branch-create {branch, from}` / `tag-create {tag, * message?, from}` with that commit's sha as `from` — the server confirms the * sha itself. Creating a branch never switches to it here. Announces * `agenthub:git-changed` so the other views of the workspace read again. * @author Nguyễn Ngọc Trí Vĩ */ import { useState, type KeyboardEvent } from "react"; import { useTranslation } from "react-i18next"; import { api, type GitTarget } from "../../../lib/api"; import { toast } from "../../../lib/toast"; import { isSubmitEnter } from "../../../lib/keys"; import { emitGitChanged } from "../../../lib/gitChanged"; import { GitModal, ModalButton } from "./GitModal"; export function CreateBranchDialog({ target, sha, short, kind = "branch", onClose, onDone, }: { target: GitTarget; sha: string; short: string; kind?: "branch" | "tag"; onClose: () => void; onDone: () => void; }) { const { t } = useTranslation(["gitHistory"]); const [name, setName] = useState(""); const [message, setMessage] = useState(""); const [error, setError] = useState(null); const [running, setRunning] = useState(false); const tag = kind === "tag"; const run = async () => { const ref = name.trim(); if (!ref) return; setRunning(true); setError(null); const action = tag ? "tag-create" : "branch-create"; try { await api.lanes.gitRefAction( target, action, tag ? { tag: ref, from: sha, ...(message.trim() ? { message: message.trim() } : {}) } : { branch: ref, from: sha } ); toast.success(t(tag ? "tagCreated" : "branchCreated", { name: ref, branch: ref, short })); onDone(); } catch (err) { setError(err instanceof Error ? err.message : String(err)); setRunning(false); } finally { emitGitChanged(typeof target === "number" ? target : target.lane, action); } }; const submit = (e: KeyboardEvent) => { if (isSubmitEnter(e)) { e.preventDefault(); void run(); } }; const input = "rounded border border-border-light bg-surface-0 px-2 py-1 font-mono text-[12px] text-fg-primary"; return ( {t("cancel")} void run()} > {t(tag ? "createTag" : "createBranch")} } > {tag && ( )}

{t(tag ? "createTagHint" : "createBranchHint")}

{error && (

{error}

)}
); }