Open History to see the last commit
1import type { ThreadId } from "@t3tools/contracts";
2import { FolderIcon, GitForkIcon } from "lucide-react";
3import { useCallback } from "react";
4
5import { newCommandId } from "../lib/utils";
6import { readNativeApi } from "../nativeApi";
7import { useComposerDraftStore } from "../composerDraftStore";
8import { useStore } from "../store";
9import {
10 EnvMode,
11 resolveDraftEnvModeAfterBranchChange,
12 resolveEffectiveEnvMode,
13} from "./BranchToolbar.logic";
14import { BranchToolbarBranchSelector } from "./BranchToolbarBranchSelector";
15import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "./ui/select";
16
17const envModeItems = [
18 { value: "local", label: "Local" },
19 { value: "worktree", label: "New worktree" },
20] as const;
21
22interface BranchToolbarProps {
23 threadId: ThreadId;
24 onEnvModeChange: (mode: EnvMode) => void;
25 envLocked: boolean;
26 onCheckoutPullRequestRequest?: (reference: string) => void;
27 onComposerFocusRequest?: () => void;
28}
29
30export default function BranchToolbar({
31 threadId,
32 onEnvModeChange,
33 envLocked,
34 onCheckoutPullRequestRequest,
35 onComposerFocusRequest,
36}: BranchToolbarProps) {
37 const threads = useStore((store) => store.threads);
38 const projects = useStore((store) => store.projects);
39 const setThreadBranchAction = useStore((store) => store.setThreadBranch);
40 const draftThread = useComposerDraftStore((store) => store.getDraftThread(threadId));
41 const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
42
43 const serverThread = threads.find((thread) => thread.id === threadId);
44 const activeProjectId = serverThread?.projectId ?? draftThread?.projectId ?? null;
45 const activeProject = projects.find((project) => project.id === activeProjectId);
46 const activeThreadId = serverThread?.id ?? (draftThread ? threadId : undefined);
47 const activeThreadBranch = serverThread?.branch ?? draftThread?.branch ?? null;
48 const activeWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
49 const branchCwd = activeWorktreePath ?? activeProject?.cwd ?? null;
50 const hasServerThread = serverThread !== undefined;
51 const effectiveEnvMode = resolveEffectiveEnvMode({
52 activeWorktreePath,
53 hasServerThread,
54 draftThreadEnvMode: draftThread?.envMode,
55 });
56
57 const setThreadBranch = useCallback(
58 (branch: string | null, worktreePath: string | null) => {
59 if (!activeThreadId) return;
60 const api = readNativeApi();
61 // If the effective cwd is about to change, stop the running session so the
62 // next message creates a new one with the correct cwd.
63 if (serverThread?.session && worktreePath !== activeWorktreePath && api) {
64 void api.orchestration
65 .dispatchCommand({
66 type: "thread.session.stop",
67 commandId: newCommandId(),
68 threadId: activeThreadId,
69 createdAt: new Date().toISOString(),
70 })
71 .catch(() => undefined);
72 }
73 if (api && hasServerThread) {
74 void api.orchestration.dispatchCommand({
75 type: "thread.meta.update",
76 commandId: newCommandId(),
77 threadId: activeThreadId,
78 branch,
79 worktreePath,
80 });
81 }
82 if (hasServerThread) {
83 setThreadBranchAction(activeThreadId, branch, worktreePath);
84 return;
85 }
86 const nextDraftEnvMode = resolveDraftEnvModeAfterBranchChange({
87 nextWorktreePath: worktreePath,
88 currentWorktreePath: activeWorktreePath,
89 effectiveEnvMode,
90 });
91 setDraftThreadContext(threadId, {
92 branch,
93 worktreePath,
94 envMode: nextDraftEnvMode,
95 });
96 },
97 [
98 activeThreadId,
99 serverThread?.session,
100 activeWorktreePath,
101 hasServerThread,
102 setThreadBranchAction,
103 setDraftThreadContext,
104 threadId,
105 effectiveEnvMode,
106 ],
107 );
108
109 if (!activeThreadId || !activeProject) return null;
110
111 return (
112 <div className="mx-auto flex w-full max-w-3xl items-center justify-between px-5 pb-3 pt-1">
113 {envLocked || activeWorktreePath ? (
114 <span className="inline-flex items-center gap-1 border border-transparent px-[calc(--spacing(3)-1px)] text-sm font-medium text-muted-foreground/70 sm:text-xs">
115 {activeWorktreePath ? (
116 <>
117 <GitForkIcon className="size-3" />
118 Worktree
119 </>
120 ) : (
121 <>
122 <FolderIcon className="size-3" />
123 Local
124 </>
125 )}
126 </span>
127 ) : (
128 <Select
129 value={effectiveEnvMode}
130 onValueChange={(value) => onEnvModeChange(value as EnvMode)}
131 items={envModeItems}
132 >
133 <SelectTrigger variant="ghost" size="xs" className="font-medium">
134 {effectiveEnvMode === "worktree" ? (
135 <GitForkIcon className="size-3" />
136 ) : (
137 <FolderIcon className="size-3" />
138 )}
139 <SelectValue />
140 </SelectTrigger>
141 <SelectPopup>
142 <SelectItem value="local">
143 <span className="inline-flex items-center gap-1.5">
144 <FolderIcon className="size-3" />
145 Local
146 </span>
147 </SelectItem>
148 <SelectItem value="worktree">
149 <span className="inline-flex items-center gap-1.5">
150 <GitForkIcon className="size-3" />
151 New worktree
152 </span>
153 </SelectItem>
154 </SelectPopup>
155 </Select>
156 )}
157
158 <BranchToolbarBranchSelector
159 activeProjectCwd={activeProject.cwd}
160 activeThreadBranch={activeThreadBranch}
161 activeWorktreePath={activeWorktreePath}
162 branchCwd={branchCwd}
163 effectiveEnvMode={effectiveEnvMode}
164 envLocked={envLocked}
165 onSetThreadBranch={setThreadBranch}
166 {...(onCheckoutPullRequestRequest ? { onCheckoutPullRequestRequest } : {})}
167 {...(onComposerFocusRequest ? { onComposerFocusRequest } : {})}
168 />
169 </div>
170 );
171}