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

RightPanelTabs.tsx

511 lines17.6 KB

1import type { ContextMenuItem, PreviewSessionSnapshot } from "@t3tools/contracts";
2import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
3import { Bot, FileDiff, Files, Globe2, Plus, TerminalSquare, X } from "lucide-react";
4import {
5 type MouseEvent as ReactMouseEvent,
6 type ReactElement,
7 type ReactNode,
8 useCallback,
9 useEffect,
10 useRef,
11 useState,
12} from "react";
13​
14import { isElectron } from "~/env";
15import type { RightPanelSurface } from "~/rightPanelStore";
16import { cn } from "~/lib/utils";
17import { readLocalApi } from "~/localApi";
18import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
19import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
20import { ScrollArea } from "~/components/ui/scroll-area";
21import { faviconUrlForOrigin } from "~/lib/favicon";
22import { useTheme } from "~/hooks/useTheme";
23import type { PreviewPanelInlineSize } from "~/hooks/usePreviewPanelInlineSize";
24import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
25​
26import { PreviewPanelShell, type PreviewPanelMode } from "./preview/PreviewPanelShell";
27import { PierreEntryIcon } from "./chat/PierreEntryIcon";
28​
29interface RightPanelTabsProps {
30 mode: PreviewPanelMode;
31 maximized?: boolean;
32 inlineSize?: PreviewPanelInlineSize;
33 layoutControls?: ReactNode;
34 surfaces: readonly RightPanelSurface[];
35 activeSurfaceId: string | null;
36 pendingSurfaceIds: ReadonlySet<string>;
37 previewSessions: Readonly<Record<string, PreviewSessionSnapshot>>;
38 terminalLabelsById: ReadonlyMap<string, string>;
39 onActivate: (surface: RightPanelSurface) => void;
40 onCloseSurface: (surface: RightPanelSurface) => void;
41 onCloseOtherSurfaces: (surface: RightPanelSurface) => void;
42 onCloseSurfacesToRight: (surface: RightPanelSurface) => void;
43 onCloseAllSurfaces: () => void;
44 onCopyFilePath: (relativePath: string) => void;
45 onAddBrowser: () => void;
46 onAddTerminal: () => void;
47 onAddDiff: () => void;
48 onAddFiles: () => void;
49 onAddAgents: () => void;
50 browserAvailable: boolean;
51 diffAvailable: boolean;
52 filesAvailable: boolean;
53 children: ReactNode;
55​
56const SURFACE_DISABLED_REASONS = {
57 browser: "Browser previews are only available in the T3 Code desktop app.",
58 files: "Files are only available when a project is open.",
59 diff: "Diff is only available for server threads in Git repositories.",
60} as const;
61​
62type TabContextMenuAction = "copy-path" | "close" | "close-others" | "close-to-right" | "close-all";
63​
64function DisabledReasonTooltip(props: { reason: string; trigger: ReactElement }) {
65 return (
66 <Tooltip>
67 <TooltipTrigger render={props.trigger} />
68 <TooltipPopup side="top">{props.reason}</TooltipPopup>
69 </Tooltip>
70 );
72​
73function SurfaceMenuItem(props: {
74 available: boolean;
75 disabledReason?: string;
76 onClick: () => void;
77 children: ReactNode;
78}) {
79 const item = (
80 <MenuItem
81 className={!props.available ? "data-disabled:pointer-events-auto" : undefined}
82 onClick={props.onClick}
83 disabled={!props.available}
84 >
85 {props.children}
86 </MenuItem>
87 );
88 if (props.available || !props.disabledReason) return item;
89 return <DisabledReasonTooltip reason={props.disabledReason} trigger={item} />;
91​
92function RightPanelEmptyState(props: {
93 onAddBrowser: () => void;
94 onAddTerminal: () => void;
95 onAddDiff: () => void;
96 onAddFiles: () => void;
97 onAddAgents: () => void;
98 browserAvailable: boolean;
99 diffAvailable: boolean;
100 filesAvailable: boolean;
101}) {
102 const actions = [
103 {
104 label: "Browser",
105 description: "Open a local app or URL.",
106 icon: Globe2,
107 available: props.browserAvailable,
108 disabledReason: SURFACE_DISABLED_REASONS.browser,
109 onClick: props.onAddBrowser,
110 },
111 {
112 label: "Terminal",
113 description: "Start a shell in this workspace.",
114 icon: TerminalSquare,
115 available: true,
116 disabledReason: null,
117 onClick: props.onAddTerminal,
118 },
119 {
120 label: "Files",
121 description: "Browse and read workspace files.",
122 icon: Files,
123 available: props.filesAvailable,
124 disabledReason: SURFACE_DISABLED_REASONS.files,
125 onClick: props.onAddFiles,
126 },
127 {
128 label: "Diff",
129 description: "Review changes in this thread.",
130 icon: FileDiff,
131 available: props.diffAvailable,
132 disabledReason: SURFACE_DISABLED_REASONS.diff,
133 onClick: props.onAddDiff,
134 },
135 {
136 label: "Agents",
137 description: "Watch subagents and workflows run.",
138 icon: Bot,
139 available: true,
140 disabledReason: null,
141 onClick: props.onAddAgents,
142 },
143 ] as const;
144​
145 return (
146 <div className="flex min-h-0 flex-1 items-center justify-center p-6">
147 <div className="w-full max-w-xl">
148 <div className="mb-5 text-center">
149 <h3 className="text-sm font-medium text-foreground">Open a surface</h3>
150 <p className="mt-1 text-xs text-muted-foreground">
151 Choose what to show in the right panel.
152 </p>
153 </div>
154 <div className="grid grid-cols-2 gap-2">
155 {actions.map((action) => {
156 const Icon = action.icon;
157 const content = (
158 <>
159 <Icon className="mb-3 size-5" />
160 <span className="text-sm font-medium">{action.label}</span>
161 <span className="mt-1 text-xs leading-relaxed text-muted-foreground">
162 {action.description}
163 </span>
164 </>
165 );
166 if (action.available) {
167 return (
168 <button
169 key={action.label}
170 type="button"
171 onClick={action.onClick}
172 className="flex min-h-28 w-full flex-col items-start rounded-lg border border-border/80 bg-card p-4 text-left transition hover:border-border hover:bg-accent/60 dark:border-transparent dark:shadow-none dark:inset-ring-1 dark:inset-ring-white/5"
173 >
174 {content}
175 </button>
176 );
177 }
178 const disabledCard = (
179 <button
180 type="button"
181 className="flex min-h-28 w-full cursor-not-allowed flex-col items-start rounded-lg border border-border/80 bg-card p-4 text-left opacity-40 dark:border-transparent dark:shadow-none dark:inset-ring-1 dark:inset-ring-white/5"
182 aria-disabled="true"
183 >
184 {content}
185 </button>
186 );
187 return (
188 <DisabledReasonTooltip
189 key={action.label}
190 reason={action.disabledReason}
191 trigger={disabledCard}
192 />
193 );
194 })}
195 </div>
196 </div>
197 </div>
198 );
200​
201function surfaceTitle(
202 surface: RightPanelSurface,
203 sessions: Readonly<Record<string, PreviewSessionSnapshot>>,
204 terminalLabelsById: ReadonlyMap<string, string>,
205): string {
206 switch (surface.kind) {
207 case "diff":
208 return "Diff";
209 case "files":
210 return "Files";
211 case "file":
212 return surface.relativePath.slice(surface.relativePath.lastIndexOf("/") + 1);
213 case "terminal":
214 return (
215 terminalLabelsById.get(surface.activeTerminalId) ??
216 getTerminalLabel(surface.activeTerminalId)
217 );
218 case "agents":
219 return "Agents";
220 case "preview": {
221 const snapshot = surface.resourceId ? sessions[surface.resourceId] : null;
222 if (!snapshot || snapshot.navStatus._tag === "Idle") return "Browser";
223 if (snapshot.navStatus.title.trim().length > 0) return snapshot.navStatus.title;
224 try {
225 return new URL(snapshot.navStatus.url).host || "Browser";
226 } catch {
227 return "Browser";
228 }
229 }
230 }
232​
233function PreviewFavicon({ url }: { url: string | null }) {
234 const faviconUrl = faviconUrlForOrigin(url, 32);
235 const [failedUrl, setFailedUrl] = useState<string | null>(null);
236 if (!faviconUrl || failedUrl === faviconUrl) return <Globe2 className="size-3 shrink-0" />;
237 return (
238 <img
239 src={faviconUrl}
240 alt=""
241 aria-hidden
242 draggable={false}
243 className="size-3 shrink-0 rounded-sm"
244 onError={() => setFailedUrl(faviconUrl)}
245 />
246 );
248​
249function SurfaceIcon({
250 surface,
251 sessions,
252 theme,
253}: {
254 surface: RightPanelSurface;
255 sessions: Readonly<Record<string, PreviewSessionSnapshot>>;
256 theme: "light" | "dark";
257}) {
258 switch (surface.kind) {
259 case "preview": {
260 const snapshot = surface.resourceId ? sessions[surface.resourceId] : null;
261 const url = !snapshot || snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url;
262 return <PreviewFavicon url={url} />;
263 }
264 case "diff":
265 return <FileDiff className="size-3 shrink-0" />;
266 case "files":
267 return <Files className="size-3 shrink-0" />;
268 case "file":
269 return (
270 <PierreEntryIcon
271 pathValue={surface.relativePath}
272 kind="file"
273 theme={theme}
274 className="size-3"
275 />
276 );
277 case "terminal":
278 return <TerminalSquare className="size-3 shrink-0" />;
279 case "agents":
280 return <Bot className="size-3 shrink-0" />;
281 }
283​
284export function RightPanelTabs(props: RightPanelTabsProps) {
285 const ownsDesktopTitleBar = isElectron && props.mode === "inline";
286 const { resolvedTheme } = useTheme();
287 const tabListRef = useRef<HTMLDivElement>(null);
288​
289 const handleTabContextMenu = useCallback(
290 async (event: ReactMouseEvent, surface: RightPanelSurface) => {
291 event.preventDefault();
292 event.stopPropagation();
293​
294 const api = readLocalApi();
295 if (!api) return;
296​
297 const surfaceIndex = props.surfaces.findIndex((entry) => entry.id === surface.id);
298 if (surfaceIndex < 0) return;
299​
300 const items: ContextMenuItem<TabContextMenuAction>[] = [];
301 if (surface.kind === "file") {
302 items.push({ id: "copy-path", label: "Copy path" });
303 }
304 items.push(
305 { id: "close", label: "Close" },
306 {
307 id: "close-others",
308 label: "Close others",
309 disabled: props.surfaces.length <= 1,
310 },
311 {
312 id: "close-to-right",
313 label: "Close to the right",
314 disabled: surfaceIndex >= props.surfaces.length - 1,
315 },
316 {
317 id: "close-all",
318 label: "Close all",
319 disabled: props.surfaces.length === 0,
320 },
321 );
322​
323 const action = await api.contextMenu.show(items, { x: event.clientX, y: event.clientY });
324 switch (action) {
325 case "copy-path":
326 if (surface.kind === "file") props.onCopyFilePath(surface.relativePath);
327 break;
328 case "close":
329 props.onCloseSurface(surface);
330 break;
331 case "close-others":
332 props.onCloseOtherSurfaces(surface);
333 break;
334 case "close-to-right":
335 props.onCloseSurfacesToRight(surface);
336 break;
337 case "close-all":
338 props.onCloseAllSurfaces();
339 break;
340 case null:
341 break;
342 }
343 },
344 [props],
345 );
346 const handleTabMouseDown = useCallback((event: ReactMouseEvent) => {
347 if (event.button !== 1) return;
348 event.preventDefault();
349 }, []);
350 const handleTabAuxClick = useCallback(
351 (event: ReactMouseEvent, surface: RightPanelSurface) => {
352 if (event.button !== 1) return;
353 event.preventDefault();
354 event.stopPropagation();
355 props.onCloseSurface(surface);
356 },
357 [props],
358 );
359​
360 useEffect(() => {
361 const activeTab = tabListRef.current?.querySelector<HTMLElement>("[data-active-tab='true']");
362 activeTab?.scrollIntoView({ block: "nearest", inline: "nearest" });
363 }, [props.activeSurfaceId]);
364​
365 return (
366 <PreviewPanelShell
367 mode={props.mode}
368 {...(props.maximized !== undefined ? { maximized: props.maximized } : {})}
369 {...(props.inlineSize ? { inlineSize: props.inlineSize } : {})}
370 >
371 <div
372 className={cn(
373 "workspace-topbar gap-1 pl-2",
374 props.mode !== "inline" && "[--workspace-topbar-height:--spacing(11)]",
375 props.mode === "inline" ? "pr-28" : "pr-3",
376 ownsDesktopTitleBar && "wco:pr-[calc(var(--workspace-native-controls-inset)+6rem)]",
377 props.mode === "inline" && props.maximized && COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS,
378 )}
379 data-right-panel-tabbar
380 >
381 <ScrollArea
382 ref={tabListRef}
383 hideScrollbars
384 scrollFade
385 className={cn("min-w-0 flex-1 rounded-none", ownsDesktopTitleBar && "drag-region")}
386 data-right-panel-tab-list
387 >
388 <div className="flex h-full w-max min-w-full items-center gap-1">
389 {props.surfaces.map((surface) => {
390 const active = surface.id === props.activeSurfaceId;
391 const pending = props.pendingSurfaceIds.has(surface.id);
392 const title = surfaceTitle(surface, props.previewSessions, props.terminalLabelsById);
393 return (
394 <div
395 key={surface.id}
396 data-active-tab={active}
397 onMouseDown={handleTabMouseDown}
398 onAuxClick={(event) => handleTabAuxClick(event, surface)}
399 onContextMenu={(event) => void handleTabContextMenu(event, surface)}
400 className={cn(
401 "group/tab flex h-6 max-w-36 shrink-0 items-center gap-0.5 rounded-md pr-2 pl-1.5 text-xs",
402 active
403 ? "bg-accent text-foreground"
404 : "text-muted-foreground hover:bg-accent/60 hover:text-foreground",
405 )}
406 >
407 <button
408 type="button"
409 className="group/close relative flex size-4 shrink-0 items-center justify-center rounded-sm hover:bg-muted"
410 aria-label={`Close ${title}`}
411 onClick={() => props.onCloseSurface(surface)}
412 >
413 <span className="relative flex size-3 items-center justify-center group-hover/tab:hidden group-focus-visible/close:hidden">
414 <SurfaceIcon
415 surface={surface}
416 sessions={props.previewSessions}
417 theme={resolvedTheme}
418 />
419 {pending ? (
420 <span
421 className="absolute -right-0.5 -bottom-0.5 size-1.5 rounded-full bg-current"
422 aria-hidden
423 />
424 ) : null}
425 </span>
426 <X className="hidden size-3 group-hover/tab:block group-focus-visible/close:block" />
427 </button>
428 <Tooltip>
429 <TooltipTrigger
430 render={
431 <button
432 type="button"
433 className="flex min-w-0 items-center"
434 onClick={() => props.onActivate(surface)}
435 >
436 <span className="truncate">{title}</span>
437 </button>
438 }
439 />
440 <TooltipPopup>{title}</TooltipPopup>
441 </Tooltip>
442 </div>
443 );
444 })}
445 {props.surfaces.length > 0 ? (
446 <Menu>
447 <MenuTrigger
448 className="relative inline-flex size-6 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
449 aria-label="Add panel surface"
450 >
451 <Plus className="size-3.5" />
452 </MenuTrigger>
453 <MenuPopup align="start" side="bottom" sideOffset={6} className="min-w-44">
454 <SurfaceMenuItem
455 available={props.browserAvailable}
456 disabledReason={SURFACE_DISABLED_REASONS.browser}
457 onClick={props.onAddBrowser}
458 >
459 <Globe2 />
460 Browser
461 </SurfaceMenuItem>
462 <SurfaceMenuItem available onClick={props.onAddTerminal}>
463 <TerminalSquare />
464 Terminal
465 </SurfaceMenuItem>
466 <SurfaceMenuItem
467 available={props.filesAvailable}
468 disabledReason={SURFACE_DISABLED_REASONS.files}
469 onClick={props.onAddFiles}
470 >
471 <Files />
472 Files
473 </SurfaceMenuItem>
474 <SurfaceMenuItem
475 available={props.diffAvailable}
476 disabledReason={SURFACE_DISABLED_REASONS.diff}
477 onClick={props.onAddDiff}
478 >
479 <FileDiff />
480 Diff
481 </SurfaceMenuItem>
482 <SurfaceMenuItem available onClick={props.onAddAgents}>
483 <Bot />
484 Agents
485 </SurfaceMenuItem>
486 </MenuPopup>
487 </Menu>
488 ) : null}
489 </div>
490 </ScrollArea>
491 {props.layoutControls}
492 </div>
493 <div className="flex min-h-0 flex-1 flex-col" data-right-panel-surface-content>
494 {props.activeSurfaceId === null ? (
495 <RightPanelEmptyState
496 onAddBrowser={props.onAddBrowser}
497 onAddTerminal={props.onAddTerminal}
498 onAddDiff={props.onAddDiff}
499 onAddFiles={props.onAddFiles}
500 onAddAgents={props.onAddAgents}
501 browserAvailable={props.browserAvailable}
502 diffAvailable={props.diffAvailable}
503 filesAvailable={props.filesAvailable}
504 />
505 ) : (
506 props.children
507 )}
508 </div>
509 </PreviewPanelShell>
510 );