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

ThreadDetailsPanel.tsx

337 lines14.0 KB

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