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