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

PanelLayoutControls.tsx

286 lines10.4 KB

1import {
2 PanelBottomIcon,
3 PanelRightIcon,
4 SquareMenuIcon,
5 Maximize2Icon,
6 Minimize2Icon,
7} from "lucide-react";
8import { useGT } from "gt-react";
9import { memo, type ReactNode, useLayoutEffect, useRef } from "react";
10​
11import { cn } from "~/lib/utils";
12import { requestWorkspaceContentFocus } from "~/lib/workspaceFocus";
13​
14import type { ThreadPanelPresentation } from "../../rightPanelLayout";
15import { PopoverCreateHandle, PopoverTrigger } from "../ui/popover";
16import { useMobileSidebarSheetMounted } from "../ui/sidebar";
17import { Toggle } from "../ui/toggle";
18import { SharedTooltipTrigger } from "../ui/shared-tooltip";
19​
20/** Matches the right inset of `ThreadPanelHeaderControl`. */
21const THREAD_PANEL_HEADER_INSET_PX = 12;
22​
23export interface PanelLayoutControlsProps {
24 showThreadPanelControl?: boolean;
25 showTerminalControl?: boolean;
26 showRightPanelControl?: boolean;
27 terminalAvailable: boolean;
28 terminalOpen: boolean;
29 terminalShortcutLabel: string | null;
30 threadPanelOpen: boolean;
31 threadPanelPresentation: ThreadPanelPresentation;
32 threadPanelPopoverHandle?: ReturnType<typeof PopoverCreateHandle>;
33 threadPanelShortcutLabel: string | null;
34 rightPanelAvailable: boolean;
35 rightPanelOpen: boolean;
36 rightPanelShortcutLabel: string | null;
37 rightPanelUnavailableLabel?: string;
38 onToggleTerminal: () => void;
39 onToggleThreadPanel: () => void;
40 onToggleRightPanel: () => void;
42​
43export const PanelLayoutControls = memo(function PanelLayoutControls({
44 showThreadPanelControl = true,
45 showTerminalControl = true,
46 showRightPanelControl = true,
47 terminalAvailable,
48 terminalOpen,
49 terminalShortcutLabel,
50 threadPanelOpen,
51 threadPanelPresentation,
52 threadPanelPopoverHandle,
53 threadPanelShortcutLabel,
54 rightPanelAvailable,
55 rightPanelOpen,
56 rightPanelShortcutLabel,
57 rightPanelUnavailableLabel,
58 onToggleTerminal,
59 onToggleThreadPanel,
60 onToggleRightPanel,
61}: PanelLayoutControlsProps) {
62 const gt = useGT();
63 const closeFromPanelByPointer = useRef(false);
64 const threadPanelToggle = (
65 <Toggle
66 className="relative shrink-0 [-webkit-app-region:no-drag]"
67 pressed={threadPanelOpen}
68 onPressedChange={threadPanelPresentation === "popover" ? undefined : onToggleThreadPanel}
69 aria-label={gt("Toggle thread details panel")}
70 variant="ghost"
71 size="sm"
72 >
73 <SquareMenuIcon className="size-4" />
74 </Toggle>
75 );
76 const threadPanelControl = (
77 <SharedTooltipTrigger
78 render={<span className="flex shrink-0" data-thread-panel-toggle />}
79 label={
80 threadPanelShortcutLabel
81 ? gt("Toggle thread details ({shortcut})", { shortcut: threadPanelShortcutLabel })
82 : gt("Toggle thread details")
83 }
84 side="bottom"
85 >
86 {threadPanelPresentation === "popover" ? (
87 <PopoverTrigger handle={threadPanelPopoverHandle} render={threadPanelToggle} />
88 ) : (
89 threadPanelToggle
90 )}
91 </SharedTooltipTrigger>
92 );
93​
94 return (
95 <div
96 className="flex h-full shrink-0 items-center gap-1 [-webkit-app-region:no-drag]"
97 data-panel-layout-controls
98 >
99 {showThreadPanelControl ? threadPanelControl : null}
100 {showTerminalControl ? (
101 <SharedTooltipTrigger
102 render={<span className="flex shrink-0" />}
103 label={
104 terminalAvailable
105 ? terminalShortcutLabel
106 ? gt("Toggle terminal drawer ({shortcut})", { shortcut: terminalShortcutLabel })
107 : gt("Toggle terminal drawer")
108 : gt("Terminal drawer is unavailable")
109 }
110 side="bottom"
111 >
112 <Toggle
113 className="shrink-0 [-webkit-app-region:no-drag]"
114 pressed={terminalOpen}
115 onPressedChange={onToggleTerminal}
116 aria-label={gt("Toggle terminal drawer")}
117 variant="ghost"
118 size="sm"
119 disabled={!terminalAvailable}
120 >
121 <PanelBottomIcon className="size-4" />
122 </Toggle>
123 </SharedTooltipTrigger>
124 ) : null}
125 {showRightPanelControl ? (
126 <SharedTooltipTrigger
127 render={<span className="flex shrink-0" />}
128 label={
129 rightPanelAvailable
130 ? rightPanelShortcutLabel
131 ? gt("Toggle right panel ({shortcut})", { shortcut: rightPanelShortcutLabel })
132 : gt("Toggle right panel")
133 : (rightPanelUnavailableLabel ?? gt("Right panel is unavailable"))
134 }
135 side="bottom"
136 >
137 <Toggle
138 className="shrink-0 [-webkit-app-region:no-drag]"
139 pressed={rightPanelOpen}
140 onPointerDown={() => {
141 // The press moves focus onto this button, so read where it was first.
142 closeFromPanelByPointer.current =
143 rightPanelOpen &&
144 document.activeElement?.closest('[data-workspace-area="panel"]') != null;
145 }}
146 onPressedChange={(_pressed, { event }) => {
147 const isPointerClick = event instanceof MouseEvent && event.detail > 0;
148 const shouldFocusChat = isPointerClick && closeFromPanelByPointer.current;
149 closeFromPanelByPointer.current = false;
150 onToggleRightPanel();
151 if (shouldFocusChat) requestWorkspaceContentFocus();
152 }}
153 aria-label={gt("Toggle right panel")}
154 data-right-panel-toggle
155 variant="ghost"
156 size="sm"
157 disabled={!rightPanelAvailable}
158 >
159 <PanelRightIcon className="size-4" />
160 </Toggle>
161 </SharedTooltipTrigger>
162 ) : null}
163 </div>
164 );
165});
166​
167export const RightPanelMaximizeControl = memo(function RightPanelMaximizeControl({
168 maximized,
169 onToggle,
170 shortcutLabel,
171}: {
172 maximized: boolean;
173 onToggle: () => void;
174 shortcutLabel: string | null;
175}) {
176 const gt = useGT();
177 const label = maximized ? gt("Restore panel size") : gt("Maximize panel");
178 const tooltipLabel = shortcutLabel
179 ? maximized
180 ? gt("Restore panel size ({shortcut})", { shortcut: shortcutLabel })
181 : gt("Maximize panel ({shortcut})", { shortcut: shortcutLabel })
182 : label;
183 return (
184 <SharedTooltipTrigger
185 render={
186 <Toggle
187 className="shrink-0 [-webkit-app-region:no-drag]"
188 pressed={maximized}
189 onPressedChange={onToggle}
190 aria-label={label}
191 variant="ghost"
192 size="sm"
193 >
194 {maximized ? <Minimize2Icon className="size-4" /> : <Maximize2Icon className="size-4" />}
195 </Toggle>
196 }
197 label={tooltipLabel}
198 side="bottom"
199 />
200 );
201});
202​
203/** The thread panel toggle on a header whose right side the inline right panel covers. */
204export function ThreadPanelHeaderControl(props: PanelLayoutControlsProps) {
205 const mobileSidebarSheetMounted = useMobileSidebarSheetMounted();
206 return (
207 <div
208 className={cn(
209 "absolute top-[var(--workspace-controls-top)] right-[calc(env(safe-area-inset-right)+0.75rem)] z-50 flex h-[var(--workspace-topbar-height)] items-center [-webkit-app-region:no-drag]",
210 mobileSidebarSheetMounted && "hidden",
211 )}
212 data-workspace-titlebar-controls
213 >
214 <PanelLayoutControls {...props} showTerminalControl={false} showRightPanelControl={false} />
215 </div>
216 );
218​
219/** The window's top-right panel controls, with the maximize toggle while the panel is open. */
220export function WorkspaceTitlebarControls(props: {
221 readonly showMaximizeControl: boolean;
222 readonly rightPanelOpen: boolean;
223 readonly animationsActive: boolean;
224 readonly maximized: boolean;
225 readonly onToggleMaximized: () => void;
226 readonly maximizeShortcutLabel: string | null;
227 readonly children: ReactNode;
228}) {
229 const { rightPanelOpen, maximized } = props;
230 const controlsRef = useRef<HTMLDivElement | null>(null);
231 const previous = useRef({ rightPanelOpen, maximized });
232 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the toggle's slide before the commit that swapped it in paints; the animation is cancelled on change or unmount.
233 useLayoutEffect(() => {
234 const { rightPanelOpen: wasOpen, maximized: wasMaximized } = previous.current;
235 previous.current = { rightPanelOpen, maximized };
236 if (!wasOpen || rightPanelOpen || wasMaximized) return;
237 const toggle = controlsRef.current?.querySelector("[data-thread-panel-toggle]");
238 const panel = toggle?.ownerDocument.querySelector("[data-preview-panel-mode='inline']");
239 const timing = panel?.getAnimations()[0]?.effect?.getTiming();
240 if (!toggle || !panel || !timing) return;
241 // The header's toggle was just removed, so slide this one in from where it sat.
242 const headerRight = panel.getBoundingClientRect().left - THREAD_PANEL_HEADER_INSET_PX;
243 const offset = headerRight - toggle.getBoundingClientRect().right;
244 const animation = toggle.animate(
245 [{ transform: `translateX(${offset}px)` }, { transform: "none" }],
246 { ...timing, easing: getComputedStyle(panel).transitionTimingFunction },
247 );
248 return () => animation.cancel();
249 }, [rightPanelOpen, maximized]);
250​
251 const mobileSidebarSheetMounted = useMobileSidebarSheetMounted();
252 return (
253 <div
254 ref={controlsRef}
255 className={cn(
256 "pointer-events-none fixed top-[var(--workspace-controls-top)] right-[var(--workspace-controls-right)] z-50 mr-px flex h-[var(--workspace-topbar-height)] items-center gap-1 [-webkit-app-region:no-drag]",
257 mobileSidebarSheetMounted && "hidden",
258 )}
259 data-workspace-titlebar-controls
260 >
261 {props.showMaximizeControl ? (
262 <span
263 aria-hidden={!props.rightPanelOpen}
264 // Fading out on close would show it beside the thread details toggle that replaces it.
265 className={cn(
266 "flex shrink-0",
267 props.rightPanelOpen &&
268 props.animationsActive &&
269 "motion-safe:transition-opacity motion-safe:duration-(--panel-animation-duration) motion-safe:ease-out",
270 props.rightPanelOpen
271 ? "pointer-events-auto opacity-100"
272 : "pointer-events-none absolute right-full mr-1 opacity-0",
273 )}
274 inert={!props.rightPanelOpen}
275 >
276 <RightPanelMaximizeControl
277 maximized={props.maximized}
278 onToggle={props.onToggleMaximized}
279 shortcutLabel={props.maximizeShortcutLabel}
280 />
281 </span>
282 ) : null}
283 <div className="pointer-events-auto flex h-full items-center">{props.children}</div>
284 </div>
285 );