Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BranchToolbar.tsx

138 lines4.9 KB

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