Open History to see the last commit
1import { type ReactNode, useEffect, useState } from "react";
2
3import { isElectron } from "~/env";
4import { useResizableWidth } from "~/hooks/useResizableWidth";
5import { cn } from "~/lib/utils";
6
7import { RightPanelResizeHandle } from "./RightPanelResizeHandle";
8
9export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
10
11const PREVIEW_PANEL_WIDTH_STORAGE_KEY = "t3code:preview-panel-width";
12const PREVIEW_PANEL_MIN_WIDTH = 360;
13/** Fraction of the viewport allowed, preserving the remaining space for chat. */
14const PREVIEW_PANEL_MAX_WIDTH_FRACTION = 0.7;
15const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
16
17export function getPreviewPanelMaxWidth(viewportWidth: number): number {
18 return Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
19}
20
21/**
22 * Shell for the preview panel. In inline mode the panel is user-resizable
23 * via a drag handle on the left edge; width persists per browser. In
24 * sheet/sidebar modes the parent owns the size.
25 */
26export function PreviewPanelShell(props: {
27 mode: PreviewPanelMode;
28 maximized?: boolean;
29 /**
30 * Overrides the localStorage key used to persist the panel width. Callers
31 * embedding this shell for a different surface (e.g. the pull requests
32 * page) should pass their own key so resizing one panel doesn't clobber
33 * the other's remembered width.
34 */
35 widthStorageKey?: string;
36 /** Overrides the initial width (px) before the user has resized the panel. */
37 defaultWidth?: number;
38 children: ReactNode;
39}) {
40 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
41 const isInline = props.mode === "inline";
42 const maxWidth = useViewportClampedMaxWidth();
43 const { width, handlers } = useResizableWidth({
44 storageKey: props.widthStorageKey ?? PREVIEW_PANEL_WIDTH_STORAGE_KEY,
45 defaultWidth: props.defaultWidth ?? PREVIEW_PANEL_DEFAULT_WIDTH,
46 minWidth: PREVIEW_PANEL_MIN_WIDTH,
47 maxWidth,
48 edge: "left",
49 });
50
51 return (
52 <div
53 className={cn(
54 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
55 isInline
56 ? props.maximized
57 ? "flex-1 border-l border-border"
58 : "shrink-0 border-l border-border"
59 : "w-full",
60 )}
61 style={isInline && !props.maximized ? { width: `${width}px` } : undefined}
62 data-preview-panel-mode={props.mode}
63 data-preview-panel-maximized={props.maximized ? "true" : "false"}
64 >
65 {isInline && !props.maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
66 {useDragRegion ? <div className="electron-drag-region h-0 w-full" aria-hidden /> : null}
67 {props.children}
68 </div>
69 );
70}
71
72/**
73 * Track viewport width to derive a sensible upper bound for the panel.
74 * Resize-aware so dragging the OS window narrower re-clamps the stored
75 * width on the next render (the hook's clamp picks this up automatically).
76 */
77function useViewportClampedMaxWidth(): number {
78 const [vw, setVw] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth));
79 useEffect(() => {
80 if (typeof window === "undefined") return;
81 let frame = 0;
82 const onResize = () => {
83 // Coalesce rapid resize events into one rAF tick.
84 if (frame !== 0) return;
85 frame = window.requestAnimationFrame(() => {
86 frame = 0;
87 setVw(window.innerWidth);
88 });
89 };
90 window.addEventListener("resize", onResize);
91 return () => {
92 window.removeEventListener("resize", onResize);
93 if (frame !== 0) window.cancelAnimationFrame(frame);
94 };
95 }, []);
96 return getPreviewPanelMaxWidth(vw);
97}