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

ThreadDetailsPanel.tsx

311 lines12.6 KB

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