Open History to see the last commit
1"use client";
2
3import { Minus, MoreVertical, Plus as PlusIcon, RotateCcw } from "lucide-react";
4
5import { Button } from "~/components/ui/button";
6import { Menu, MenuItem, MenuPopup, MenuSeparator, MenuTrigger } from "~/components/ui/menu";
7import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
8
9import { previewBridge } from "./previewBridge";
10
11interface Props {
12 /** Active preview tab id. Tab-targeting actions are disabled without it. */
13 tabId: string | null;
14 /**
15 * True only after the desktop bridge has registered a `webContentsId` for
16 * the active tab. Tab-targeting actions throw on the desktop side until
17 * then; we disable those items so the menu doesn't fire silent no-ops.
18 */
19 hasWebContents: boolean;
20 /** Current zoom factor as a number (1.0 = 100%). */
21 zoomFactor: number;
22 /** Fixed viewport modes expose the device toolbar and resize rails. */
23 deviceToolbarVisible: boolean;
24 /** Switches between fill-panel mode and a fixed responsive viewport. */
25 onToggleDeviceToolbar: () => void;
26}
27
28/**
29 * Three-dot menu in the chrome row. Wires Hard reload, DevTools, zoom
30 * controls, and storage-clearing actions. Only mounted by `PreviewView`
31 * when the desktop bridge is present, so we can call it unconditionally.
32 */
33export function PreviewMoreMenu({
34 tabId,
35 hasWebContents,
36 zoomFactor,
37 deviceToolbarVisible,
38 onToggleDeviceToolbar,
39}: Props) {
40 if (!previewBridge) return null;
41 const bridge = previewBridge;
42 const tabDisabled = !tabId || !hasWebContents;
43 const callTab = (op: (tabId: string) => Promise<void>) => () => {
44 if (!tabId) return;
45 void op(tabId).catch(() => undefined);
46 };
47
48 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
49 return (
50 <Menu>
51 <Tooltip>
52 <TooltipTrigger
53 render={
54 <MenuTrigger
55 render={
56 <Button variant="ghost" size="icon-xs" type="button" aria-label="Preview menu" />
57 }
58 />
59 }
60 >
61 <MoreVertical />
62 </TooltipTrigger>
63 <TooltipPopup>More</TooltipPopup>
64 </Tooltip>
65 <MenuPopup align="end" sideOffset={6} className="min-w-56">
66 <MenuItem onClick={callTab(bridge.hardReload)} disabled={tabDisabled}>
67 Hard reload
68 </MenuItem>
69 <MenuItem onClick={callTab(bridge.openDevTools)} disabled={tabDisabled}>
70 Open DevTools
71 </MenuItem>
72 <MenuItem onClick={onToggleDeviceToolbar} disabled={tabDisabled}>
73 {deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
74 </MenuItem>
75 <MenuSeparator />
76 {/*
77 Zoom row: label + inline control cluster. `closeOnClick=false`
78 keeps the menu open while the user clicks the +/− buttons.
79 */}
80 <MenuItem
81 closeOnClick={false}
82 onClick={(event: React.MouseEvent) => event.preventDefault()}
83 className="justify-between"
84 disabled={tabDisabled}
85 >
86 <span>Zoom</span>
87 <span className="flex items-center gap-1">
88 <Button
89 variant="outline"
90 size="icon-xs"
91 type="button"
92 onClick={callTab(bridge.zoomOut)}
93 aria-label="Zoom out"
94 disabled={tabDisabled}
95 >
96 <Minus />
97 </Button>
98 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
99 {zoomLabel}
100 </span>
101 <Button
102 variant="outline"
103 size="icon-xs"
104 type="button"
105 onClick={callTab(bridge.zoomIn)}
106 aria-label="Zoom in"
107 disabled={tabDisabled}
108 >
109 <PlusIcon />
110 </Button>
111 <Button
112 variant="ghost"
113 size="icon-xs"
114 type="button"
115 onClick={callTab(bridge.resetZoom)}
116 aria-label="Reset zoom"
117 disabled={tabDisabled}
118 >
119 <RotateCcw />
120 </Button>
121 </span>
122 </MenuItem>
123 <MenuSeparator />
124 <MenuItem onClick={() => void bridge.clearCookies().catch(() => undefined)}>
125 Clear cookies
126 </MenuItem>
127 <MenuItem onClick={() => void bridge.clearCache().catch(() => undefined)}>
128 Clear cache
129 </MenuItem>
130 </MenuPopup>
131 </Menu>
132 );
133}