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