Open History to see the last commit
1import type {
2 EditorId,
3 EnvironmentId,
4 ProjectId,
5 ProjectScript,
6 ResolvedKeybindingsConfig,
7 ThreadId,
8} from "@t3tools/contracts";
9
10import { scopeProjectRef } from "@t3tools/client-runtime/environment";
11import { isScratchProject } from "@t3tools/client-runtime/state/projects";
12
13import type { ComposerThreadTarget, DraftId } from "../../composerDraftStore";
14import { useT3ProjectFileScripts } from "../../hooks/useT3ProjectFileScripts";
15import { type EnvMode, type EnvironmentOption } from "../BranchToolbar.logic";
16import { BranchToolbar } from "../BranchToolbar";
17import GitActionsControl from "../GitActionsControl";
18import ProjectScriptsControl, {
19 type NewProjectScriptInput,
20 type ProjectScriptActionResult,
21} from "../ProjectScriptsControl";
22import { T, useGT } from "gt-react";
23import { useState, type ComponentProps } from "react";
24import { ThreadDetailsCard } from "./ThreadDetailsCard";
25import { OpenInPicker } from "./OpenInPicker";
26import { ThreadDetailsSection } from "./ThreadDetailsSection";
27import { ThreadAutomationsPanel } from "./ThreadAutomationsPanel";
28import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
29import { ThreadRepositorySections } from "./ThreadRepositorySections";
30import { ThreadServersPanel } from "./ThreadServersPanel";
31import {
32 ThreadDetailsContext,
33 ThreadDetailsUsage,
34 type ThreadDetailsUsageInput,
35} from "./ThreadDetailsUsage";
36import { ThreadDetailsItemsMenuItems } from "./ThreadDetailsItemsMenu";
37import { resolveThreadDetailsVisibility, THREAD_DETAILS_ITEMS } from "./threadDetailsItems";
38import { useClientSettings } from "../../hooks/useSettings";
39import { useProject, useServerConfigs } from "../../state/entities";
40import { filesystemEnvironment } from "../../state/filesystem";
41import { useEnvironmentQuery } from "../../state/query";
42import { ThreadDetailsThreadPrRows } from "./ThreadDetailsPrRows";
43import { cn } from "../../lib/utils";
44import { SlidersHorizontalIcon } from "lucide-react";
45import { ThreadDetailsControl } from "./ThreadDetailsControl";
46import {
47 THREAD_DETAILS_PANEL_ICON_CLASS,
48 THREAD_DETAILS_PANEL_LABEL_CLASS,
49} from "./threadDetailsPanelStyles";
50import { Menu, MenuPopup } from "../ui/menu";
51
52interface ThreadDetailsPanelProps extends Pick<
53 ComponentProps<typeof ThreadDetailsCard>,
54 "anchor" | "handle"
55> {
56 forceNewWorktree?: boolean;
57 environmentId: EnvironmentId;
58 threadId: ThreadId;
59 projectId: ProjectId;
60 worktreePath: string | null;
61 isServerThread: boolean;
62 draftId?: DraftId;
63 composerDraftTarget: ComposerThreadTarget;
64 activeProjectName: string | undefined;
65 activeProjectScripts: ReadonlyArray<ProjectScript> | undefined;
66 preferredScriptId: string | null;
67 keybindings: ResolvedKeybindingsConfig;
68 availableEditors: ReadonlyArray<EditorId>;
69 showOpenInPicker: boolean;
70 openInShortcutEnabled: boolean;
71 gitCwd: string | null;
72 workspaceMutationId: string | null;
73 isGitRepo: boolean;
74 envLocked: boolean;
75 availableEnvironments: readonly EnvironmentOption[];
76 autoEnvironmentLabel?: string | undefined;
77 onAutoEnvironment?: (() => void) | undefined;
78 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
79 onEnvModeChange: (mode: EnvMode) => void;
80 envMode: EnvMode;
81 activeThreadBranchOverride?: string | null;
82 onActiveThreadBranchOverrideChange?: (branch: string | null) => void;
83 startFromOrigin: boolean;
84 onStartFromOriginChange: (startFromOrigin: boolean) => void;
85 onCheckoutPullRequestRequest?: (reference: string) => void;
86 onComposerFocusRequest: () => void;
87 onOpenChanges?: () => void;
88 onRunProjectScript: (script: ProjectScript) => void;
89 onAddProjectScript: (input: NewProjectScriptInput) => Promise<ProjectScriptActionResult>;
90 onUpdateProjectScript: (
91 scriptId: string,
92 input: NewProjectScriptInput,
93 ) => Promise<ProjectScriptActionResult>;
94 onDeleteProjectScript: (scriptId: string) => Promise<ProjectScriptActionResult>;
95 usage: ThreadDetailsUsageInput;
96}
97
98export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) {
99 const visible = useClientSettings(resolveThreadDetailsVisibility);
100 const gt = useGT();
101 const [menuPosition, setMenuPosition] = useState<{ x: number; y: number } | null>(null);
102 const fileScripts = useT3ProjectFileScripts(
103 props.environmentId,
104 props.activeProjectScripts && visible.scripts ? props.gitCwd : null,
105 );
106 const activeThreadRef = { environmentId: props.environmentId, threadId: props.threadId };
107 const project = useProject(scopeProjectRef(props.environmentId, props.projectId));
108 const serverConfig = useServerConfigs().get(props.environmentId);
109 const tracksFolders = (project?.trackedFolders?.length ?? 0) > 0;
110 // A worktree thread reaches tracked repositories only through its own task folder.
111 const taskFolderQuery = useEnvironmentQuery(
112 // Asked whatever the project tracks now: a thread keeps its task folder when that changes.
113 props.isServerThread && props.worktreePath !== null
114 ? filesystemEnvironment.taskFolder({
115 environmentId: props.environmentId,
116 input: { worktreePath: props.worktreePath },
117 })
118 : null,
119 );
120 const taskFolder = taskFolderQuery.data?.taskFolder ?? null;
121 // A task folder's plain project directory can sit inside someone else's repository.
122 const folderIsRepo = props.isGitRepo && taskFolder?.projectIsWorktree !== false;
123 // A No project thread's folder sits inside the shared scratch root, so it must never get git.
124 // Until the config and project load, a No project thread looks like any other folder.
125 const offersGit =
126 props.gitCwd !== null &&
127 serverConfig !== undefined &&
128 project !== null &&
129 !isScratchProject(project, serverConfig.scratchWorkspaceRoot);
130 const branchToolbarProps = {
131 showGitControls: folderIsRepo,
132 environmentId: props.environmentId,
133 threadId: props.threadId,
134 projectId: props.projectId,
135 worktreePath: props.worktreePath,
136 isServerThread: props.isServerThread,
137 ...(props.draftId ? { draftId: props.draftId } : {}),
138 onEnvModeChange: props.onEnvModeChange,
139 startFromOrigin: props.startFromOrigin,
140 onStartFromOriginChange: props.onStartFromOriginChange,
141 envMode: props.envMode,
142 ...(props.activeThreadBranchOverride !== undefined
143 ? { activeThreadBranchOverride: props.activeThreadBranchOverride }
144 : {}),
145 ...(props.onActiveThreadBranchOverrideChange
146 ? { onActiveThreadBranchOverrideChange: props.onActiveThreadBranchOverrideChange }
147 : {}),
148 envLocked: props.envLocked,
149 forceNewWorktree: props.forceNewWorktree ?? false,
150 onComposerFocusRequest: props.onComposerFocusRequest,
151 ...(props.onCheckoutPullRequestRequest
152 ? { onCheckoutPullRequestRequest: props.onCheckoutPullRequestRequest }
153 : {}),
154 };
155
156 return (
157 <ThreadDetailsCard
158 threadRef={{ environmentId: props.environmentId, threadId: props.threadId }}
159 anchor={props.anchor}
160 handle={props.handle}
161 contentKey={THREAD_DETAILS_ITEMS.filter((item) => visible[item.id])
162 .map((item) => item.id)
163 .join(",")}
164 onContextMenu={(event) => {
165 // Popups the card opens bubble here through React; they, fields, links, and selected text
166 // keep their own menus.
167 const target = event.target;
168 const selection = window.getSelection();
169 if (
170 !(target instanceof Element) ||
171 !event.currentTarget.contains(target) ||
172 target.closest("input, textarea, [contenteditable], a[href]") ||
173 (selection &&
174 !selection.isCollapsed &&
175 (event.currentTarget.contains(selection.anchorNode) ||
176 event.currentTarget.contains(selection.focusNode)))
177 )
178 return;
179 event.preventDefault();
180 setMenuPosition({ x: event.clientX, y: event.clientY });
181 }}
182 >
183 {(density) => {
184 // Section flags follow the rows that will render, so no empty section steals the divider rule.
185 const show = {
186 environment: density === "full" && visible.environment,
187 openIn: density !== "essential" && visible.openIn && props.showOpenInPicker,
188 scripts: visible.scripts && props.activeProjectScripts !== undefined,
189 branch: offersGit && folderIsRepo && visible.branch,
190 // Without a branch selector, linked pull requests still need somewhere to show.
191 pullRequests: !(offersGit && folderIsRepo) && visible.branch,
192 // A plain folder that tracks repositories is not asked to become one itself.
193 git:
194 offersGit &&
195 props.activeProjectName !== undefined &&
196 (folderIsRepo || (!tracksFolders && taskFolder === null)) &&
197 (visible.gitActions || (visible.changes && folderIsRepo)),
198 // Also for a project that tracks nothing: a thread can change a submodule.
199 repositories: offersGit && density !== "essential" && visible.gitActions,
200 };
201 return (
202 <>
203 <div className="group/details-items">
204 {show.environment || show.openIn || show.scripts ? (
205 <ThreadDetailsSection
206 headingId="thread-details-workspace-heading"
207 title={gt("Workspace")}
208 separated={false}
209 showHeading={false}
210 >
211 <div className="flex flex-col">
212 {show.environment ? (
213 <BranchToolbar
214 layout="panel"
215 panelSection="workspace"
216 availableEnvironments={props.availableEnvironments}
217 {...(props.onEnvironmentChange
218 ? { onEnvironmentChange: props.onEnvironmentChange }
219 : {})}
220 autoEnvironmentLabel={props.autoEnvironmentLabel}
221 onAutoEnvironment={props.onAutoEnvironment}
222 {...branchToolbarProps}
223 />
224 ) : null}
225
226 {show.openIn ? (
227 <OpenInPicker
228 keybindings={props.keybindings}
229 environmentId={props.environmentId}
230 availableEditors={props.availableEditors}
231 openInCwd={props.gitCwd}
232 enableShortcut={props.openInShortcutEnabled}
233 displayMode="panel"
234 />
235 ) : null}
236
237 {show.scripts && props.activeProjectScripts ? (
238 <ProjectScriptsControl
239 environmentId={props.environmentId}
240 displayMode="panel"
241 scripts={props.activeProjectScripts}
242 fileScripts={fileScripts}
243 preferredScriptId={props.preferredScriptId}
244 onRunScript={props.onRunProjectScript}
245 onAddScript={props.onAddProjectScript}
246 onUpdateScript={props.onUpdateProjectScript}
247 onDeleteScript={props.onDeleteProjectScript}
248 />
249 ) : null}
250 </div>
251 </ThreadDetailsSection>
252 ) : null}
253
254 {show.branch || show.pullRequests || show.git || show.repositories ? (
255 <ThreadDetailsSection
256 headingId="thread-details-version-control-heading"
257 title={gt("Version Control")}
258 showHeading={false}
259 separated={density === "full"}
260 >
261 <div className="flex flex-col">
262 {show.branch ? (
263 <BranchToolbar layout="panel" panelSection="branch" {...branchToolbarProps} />
264 ) : null}
265 {show.pullRequests ? (
266 <ThreadDetailsThreadPrRows
267 threadRef={activeThreadRef}
268 project={project}
269 composerTarget={props.composerDraftTarget}
270 />
271 ) : null}
272 {show.git && props.gitCwd ? (
273 <GitActionsControl
274 compact={density !== "full"}
275 showActions={visible.gitActions}
276 showChanges={visible.changes}
277 gitCwd={props.gitCwd}
278 name={props.activeProjectName ?? ""}
279 workspaceMutationId={props.workspaceMutationId}
280 activeThreadRef={activeThreadRef}
281 {...(props.draftId ? { draftId: props.draftId } : {})}
282 {...(props.onOpenChanges ? { onOpenChanges: props.onOpenChanges } : {})}
283 />
284 ) : null}
285 {show.repositories ? (
286 <ThreadRepositorySections
287 environmentId={props.environmentId}
288 threadId={props.threadId}
289 projectId={props.projectId}
290 isServerThread={props.isServerThread}
291 threadCwd={props.gitCwd}
292 taskFolder={taskFolder}
293 reachesTracked={
294 props.worktreePath === null ? tracksFolders : taskFolder !== null
295 }
296 onTaskFolderChange={taskFolderQuery.refresh}
297 explainsEmpty={!folderIsRepo}
298 showActions={visible.gitActions}
299 />
300 ) : null}
301 </div>
302 </ThreadDetailsSection>
303 ) : null}
304
305 {density === "full" && !props.draftId && visible.servers ? (
306 <ThreadServersPanel environmentId={props.environmentId} threadId={props.threadId} />
307 ) : null}
308
309 {density === "full" && !props.draftId && visible.automations ? (
310 <ThreadAutomationsPanel
311 environmentId={props.environmentId}
312 threadId={props.threadId}
313 />
314 ) : null}
315
316 {density === "full" && !props.draftId && (visible.lineage || visible.sideChats) ? (
317 <ThreadRelationshipsPanel
318 environmentId={props.environmentId}
319 threadId={props.threadId}
320 showLineage={visible.lineage}
321 showSideChats={visible.sideChats}
322 />
323 ) : null}
324
325 {visible.usage ? <ThreadDetailsUsage {...props.usage} /> : null}
326
327 {visible.context && props.usage.contextWindow ? (
328 <ThreadDetailsContext usage={props.usage.contextWindow} />
329 ) : null}
330
331 {/* Shown only when no section above has content, so an emptied card can be refilled. */}
332 <div className="p-2 group-has-[>section>:last-child:not(:empty)]/details-items:hidden">
333 <ThreadDetailsControl
334 size="xs"
335 variant="ghost"
336 part="row"
337 panel
338 onClick={(event) => {
339 const rect = event.currentTarget.getBoundingClientRect();
340 setMenuPosition({ x: rect.left, y: rect.bottom });
341 }}
342 >
343 <SlidersHorizontalIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} aria-hidden />
344 <span className={cn("ml-0.5", THREAD_DETAILS_PANEL_LABEL_CLASS)}>
345 <T>Choose what to show</T>
346 </span>
347 </ThreadDetailsControl>
348 </div>
349 </div>
350
351 <Menu
352 open={menuPosition !== null}
353 onOpenChange={(open) => !open && setMenuPosition(null)}
354 >
355 <MenuPopup
356 anchor={
357 menuPosition
358 ? {
359 getBoundingClientRect: () =>
360 new DOMRect(menuPosition.x, menuPosition.y, 0, 0),
361 }
362 : undefined
363 }
364 align="start"
365 side="bottom"
366 >
367 <ThreadDetailsItemsMenuItems />
368 </MenuPopup>
369 </Menu>
370 </>
371 );
372 }}
373 </ThreadDetailsCard>
374 );
375}