Open History to see the last commit
1"use client";
2
3import type { DesktopPreviewColorScheme, EnvironmentId } from "@t3tools/contracts";
4import { Minus, MoreVertical, Plus as PlusIcon, RotateCcw } from "lucide-react";
5
6import { Button } from "~/components/ui/button";
7import {
8 Menu,
9 MenuItem,
10 MenuPopup,
11 MenuRadioGroup,
12 MenuRadioItem,
13 MenuGroup,
14 MenuGroupLabel,
15 MenuSeparator,
16 MenuSub,
17 MenuSubPopup,
18 MenuSubTrigger,
19 MenuTrigger,
20} from "~/components/ui/menu";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
22
23import { previewBridge } from "./previewBridge";
24
25const COLOR_SCHEME_OPTIONS: ReadonlyArray<{
26 value: DesktopPreviewColorScheme;
27 label: string;
28}> = [
29 { value: "system", label: "System" },
30 { value: "light", label: "Light" },
31 { value: "dark", label: "Dark" },
32];
33
34interface Props {
35 /** Active preview tab id. Tab-targeting actions are disabled without it. */
36 tabId: string | null;
37 /**
38 * True only after the desktop bridge has registered a `webContentsId` for
39 * the active tab. Tab-targeting actions throw on the desktop side until
40 * then; we disable those items so the menu doesn't fire silent no-ops.
41 */
42 hasWebContents: boolean;
43 /** Current zoom factor as a number (1.0 = 100%). */
44 zoomFactor: number;
45 /** Emulated `prefers-color-scheme` for the guest page. */
46 colorScheme: DesktopPreviewColorScheme;
47 /** Fixed viewport modes expose the device toolbar and resize rails. */
48 deviceToolbarVisible: boolean;
49 /** Switches between fill-panel mode and a fixed responsive viewport. */
50 onToggleDeviceToolbar: () => void;
51 /** Whether the separate native always-on-top preview window is open. */
52 nativePictureInPicture: boolean;
53 /** Toggles the optional native always-on-top preview window. */
54 onNativePictureInPicture: () => void;
55 /** Environment the tab belongs to; scopes storage clearing to its partitions. */
56 environmentId: EnvironmentId;
57 /** Profile the tab was opened under, if the server recorded one. */
58 /**
59 * Required: the IPC layer reads an absent profile as "every profile", so a
60 * tab whose own profile is unknown must resolve the default before it gets
61 * here rather than passing the gap along.
62 */
63 profileId: string;
64 /** Profile display name, shown so the menu says which data is being cleared. */
65 profileName: string | undefined;
66}
67
68/**
69 * Three-dot menu in the chrome row. Wires Hard reload, DevTools, zoom
70 * controls, and storage-clearing actions. Only mounted by `PreviewView`
71 * when the desktop bridge is present, so we can call it unconditionally.
72 */
73export function PreviewMoreMenu({
74 tabId,
75 hasWebContents,
76 zoomFactor,
77 colorScheme,
78 deviceToolbarVisible,
79 onToggleDeviceToolbar,
80 nativePictureInPicture,
81 onNativePictureInPicture,
82 environmentId,
83 profileId,
84 profileName,
85}: Props) {
86 if (!previewBridge) return null;
87 const bridge = previewBridge;
88 const tabDisabled = !tabId || !hasWebContents;
89 const callTab = (op: (tabId: string) => Promise<void>) => () => {
90 if (!tabId) return;
91 void op(tabId).catch(() => undefined);
92 };
93
94 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
95 return (
96 <Menu>
97 <Tooltip>
98 <TooltipTrigger
99 render={
100 <MenuTrigger
101 render={
102 <Button variant="ghost" size="icon-xs" type="button" aria-label="Preview menu" />
103 }
104 />
105 }
106 >
107 <MoreVertical />
108 </TooltipTrigger>
109 <TooltipPopup>More</TooltipPopup>
110 </Tooltip>
111 <MenuPopup align="end" sideOffset={6} className="min-w-56">
112 <MenuItem onClick={callTab(bridge.hardReload)} disabled={tabDisabled}>
113 Hard reload
114 </MenuItem>
115 <MenuItem onClick={callTab(bridge.openDevTools)} disabled={tabDisabled}>
116 Open DevTools
117 </MenuItem>
118 <MenuItem onClick={onNativePictureInPicture} disabled={tabDisabled}>
119 {nativePictureInPicture
120 ? "Close separate preview window"
121 : "Open separate preview window"}
122 </MenuItem>
123 <MenuItem onClick={onToggleDeviceToolbar} disabled={tabDisabled}>
124 {deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
125 </MenuItem>
126 <MenuSub>
127 <MenuSubTrigger disabled={tabDisabled}>Appearance</MenuSubTrigger>
128 <MenuSubPopup className="min-w-32">
129 <MenuRadioGroup
130 value={colorScheme}
131 onValueChange={(value) => {
132 if (!tabId) return;
133 void bridge
134 .setColorScheme(tabId, value as DesktopPreviewColorScheme)
135 .catch(() => undefined);
136 }}
137 >
138 {COLOR_SCHEME_OPTIONS.map((option) => (
139 <MenuRadioItem key={option.value} value={option.value}>
140 {option.label}
141 </MenuRadioItem>
142 ))}
143 </MenuRadioGroup>
144 </MenuSubPopup>
145 </MenuSub>
146 <MenuSeparator />
147 {/*
148 Zoom row: label + inline control cluster. `closeOnClick=false`
149 keeps the menu open while the user clicks the +/− buttons.
150 */}
151 <MenuItem
152 closeOnClick={false}
153 onClick={(event: React.MouseEvent) => event.preventDefault()}
154 className="justify-between"
155 disabled={tabDisabled}
156 >
157 <span>Zoom</span>
158 <span className="flex items-center gap-1">
159 <Button
160 variant="outline"
161 size="icon-xs"
162 type="button"
163 onClick={callTab(bridge.zoomOut)}
164 aria-label="Zoom out"
165 disabled={tabDisabled}
166 >
167 <Minus />
168 </Button>
169 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
170 {zoomLabel}
171 </span>
172 <Button
173 variant="outline"
174 size="icon-xs"
175 type="button"
176 onClick={callTab(bridge.zoomIn)}
177 aria-label="Zoom in"
178 disabled={tabDisabled}
179 >
180 <PlusIcon />
181 </Button>
182 <Button
183 variant="ghost"
184 size="icon-xs"
185 type="button"
186 onClick={callTab(bridge.resetZoom)}
187 aria-label="Reset zoom"
188 className="[:hover,[data-pressed]]:bg-foreground/10"
189 disabled={tabDisabled}
190 >
191 <RotateCcw />
192 </Button>
193 </span>
194 </MenuItem>
195 <MenuSeparator />
196 {/*
197 Grouped so the heading has a `MenuGroup` ancestor — `MenuGroupLabel`
198 reads its context and throws without one. The heading also answers
199 which profile the tab is in, which is otherwise invisible: it is fixed
200 at open and nothing else in the chrome shows it.
201 */}
202 <MenuGroup>
203 {/*
204 The heading carries the profile so the actions below can keep
205 fixed-length labels: repeating a name of up to 48 characters in
206 each one drove the popup far past its width.
207 */}
208 {profileName ? (
209 // Truncation sits on the label itself: it renders a block box, so
210 // `text-overflow` on an inline child inside it never applies and a
211 // long name would push the popup past its width instead.
212 <MenuGroupLabel className="max-w-64 truncate">Profile: {profileName}</MenuGroupLabel>
213 ) : null}
214 <MenuItem
215 onClick={() =>
216 void bridge.clearCookies(environmentId, profileId).catch(() => undefined)
217 }
218 >
219 Clear cookies
220 </MenuItem>
221 <MenuItem
222 onClick={() => void bridge.clearCache(environmentId, profileId).catch(() => undefined)}
223 >
224 Clear cache
225 </MenuItem>
226 </MenuGroup>
227 </MenuPopup>
228 </Menu>
229 );
230}