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

ThreadDetailsPanel.tsx

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