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