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/** Hard ceiling so a wide monitor can't yield a panel that swallows the chat. */
14const PREVIEW_PANEL_MAX_WIDTH_PX = 1400;
15/** Fraction of the viewport allowed; the panel is min(this · vw, MAX_PX). */
16const PREVIEW_PANEL_MAX_WIDTH_FRACTION = 0.7;
17const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
18
19/**
20 * Shell for the preview panel. In inline mode the panel is user-resizable
21 * via a drag handle on the left edge; width persists per browser. In
22 * sheet/sidebar modes the parent owns the size.
23 */
24export function PreviewPanelShell(props: {
25 mode: PreviewPanelMode;
26 maximized?: boolean;
27 children: ReactNode;
28}) {
29 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
30 const isInline = props.mode === "inline";
31 const maxWidth = useViewportClampedMaxWidth();
32 const { width, handlers } = useResizableWidth({
33 storageKey: PREVIEW_PANEL_WIDTH_STORAGE_KEY,
34 defaultWidth: PREVIEW_PANEL_DEFAULT_WIDTH,
35 minWidth: PREVIEW_PANEL_MIN_WIDTH,
36 maxWidth,
37 edge: "left",
38 });
39
40 return (
41 <div
42 className={cn(
43 "relative flex h-full min-h-0 min-w-0 flex-col self-stretch bg-background",
44 isInline
45 ? props.maximized
46 ? "flex-1 border-l border-border"
47 : "shrink-0 border-l border-border"
48 : "w-full",
49 )}
50 style={isInline && !props.maximized ? { width: `${width}px` } : undefined}
51 data-preview-panel-mode={props.mode}
52 data-preview-panel-maximized={props.maximized ? "true" : "false"}
53 >
54 {isInline && !props.maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
55 {useDragRegion ? <div className="electron-drag-region h-0 w-full" aria-hidden /> : null}
56 {props.children}
57 </div>
58 );
59}
60
61/**
62 * Track viewport width to derive a sensible upper bound for the panel.
63 * Resize-aware so dragging the OS window narrower re-clamps the stored
64 * width on the next render (the hook's clamp picks this up automatically).
65 */
66function useViewportClampedMaxWidth(): number {
67 const [vw, setVw] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth));
68 useEffect(() => {
69 if (typeof window === "undefined") return;
70 let frame = 0;
71 const onResize = () => {
72 // Coalesce rapid resize events into one rAF tick.
73 if (frame !== 0) return;
74 frame = window.requestAnimationFrame(() => {
75 frame = 0;
76 setVw(window.innerWidth);
77 });
78 };
79 window.addEventListener("resize", onResize);
80 return () => {
81 window.removeEventListener("resize", onResize);
82 if (frame !== 0) window.cancelAnimationFrame(frame);
83 };
84 }, []);
85 return Math.min(PREVIEW_PANEL_MAX_WIDTH_PX, Math.floor(vw * PREVIEW_PANEL_MAX_WIDTH_FRACTION));
86}