The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 116 lines 3.63 KB
Raw
1 import { useGT } from "gt-react";
2 import { type CSSProperties, type ReactNode, useState } from "react";
3
4 import { RightPanelResizeHandle } from "~/components/preview/RightPanelResizeHandle";
5 import { RESIZABLE_WIDTH_PROPERTY, useResizableWidth } from "~/hooks/useResizableWidth";
6 import { observeResize } from "~/lib/observeResize";
7 import { cn } from "~/lib/utils";
8
9 const MIN_WIDTH = 160;
10 const MAX_WIDTH = 560;
11 const FILE_CONTENT_MIN_WIDTH = 256;
12
13 /** A persisted, resizable file-tree column; fills the row when resizing is disabled.
14 * Callers can retain a responsive initial width via defaultWidthFraction (a fraction of the row),
15 * and use a separate saved width, minimum and resize label. A chosen width overrides the default.
16 */
17 export function FileExplorerPane({
18 resizable,
19 children,
20 storageKey = "t3code.fileExplorerWidth",
21 defaultWidth = 256,
22 defaultWidthFraction,
23 minWidth = MIN_WIDTH,
24 resizeLabel,
25 }: {
26 resizable: boolean;
27 children: ReactNode;
28 storageKey?: string;
29 defaultWidth?: number;
30 defaultWidthFraction?: number;
31 minWidth?: number;
32 resizeLabel?: string;
33 }) {
34 const gt = useGT();
35 const [maxWidth, setMaxWidth] = useState(MAX_WIDTH);
36 const [responsiveDefaultWidth, setResponsiveDefaultWidth] = useState(defaultWidth);
37 const { width, setWidth, handlers } = useResizableWidth({
38 storageKey,
39 defaultWidth: responsiveDefaultWidth,
40 minWidth,
41 maxWidth,
42 edge: "left",
43 });
44
45 return (
46 <aside
47 ref={(host) => {
48 const row = host?.parentElement;
49 if (!row || !resizable) return;
50 const measure = () => {
51 const initialWidth = Math.max(
52 minWidth,
53 Math.min(defaultWidth, row.clientWidth * (defaultWidthFraction ?? 1)),
54 );
55 // A responsive default must not shrink to make room for the content reserve.
56 const availableWidth = Math.max(
57 row.clientWidth - FILE_CONTENT_MIN_WIDTH,
58 defaultWidthFraction === undefined ? minWidth : initialWidth,
59 );
60 setMaxWidth(Math.max(minWidth, Math.min(MAX_WIDTH, availableWidth)));
61 setResponsiveDefaultWidth(initialWidth);
62 };
63 measure();
64 return observeResize(row, measure);
65 }}
66 aria-label={gt("File explorer")}
67 className={cn(
68 "relative flex min-h-0 min-w-0 shrink-0 bg-background",
69 resizable ? "border-l border-border/60" : "flex-1",
70 )}
71 style={
72 resizable
73 ? ({
74 [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
75 width: `var(${RESIZABLE_WIDTH_PROPERTY})`,
76 } as CSSProperties)
77 : undefined
78 }
79 >
80 {resizable ? (
81 <RightPanelResizeHandle
82 handlers={handlers}
83 label={resizeLabel ?? gt("Resize file explorer")}
84 tabIndex={0}
85 aria-valuenow={width}
86 aria-valuemin={minWidth}
87 aria-valuemax={maxWidth}
88 onKeyDown={(event) => {
89 if (event.altKey || event.ctrlKey || event.metaKey) return;
90 let nextWidth: number;
91 switch (event.key) {
92 case "ArrowLeft":
93 nextWidth = width + 16;
94 break;
95 case "ArrowRight":
96 nextWidth = width - 16;
97 break;
98 case "Home":
99 nextWidth = minWidth;
100 break;
101 case "End":
102 nextWidth = maxWidth;
103 break;
104 default:
105 return;
106 }
107 event.preventDefault();
108 event.stopPropagation();
109 setWidth(nextWidth);
110 }}
111 />
112 ) : null}
113 {children}
114 </aside>
115 );
116 }