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

ThreadDetailsPanel.tsx

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