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}
23
24/**
25 * Three-dot menu in the chrome row. Wires Hard reload, DevTools, zoom
26 * controls, and storage-clearing actions. Only mounted by `PreviewView`
27 * when the desktop bridge is present, so we can call it unconditionally.
28 */
29export function PreviewMoreMenu({ tabId, hasWebContents, zoomFactor }: Props) {
30 if (!previewBridge) return null;
31 const bridge = previewBridge;
32 const tabDisabled = !tabId || !hasWebContents;
33 const callTab = (op: (tabId: string) => Promise<void>) => () => {
34 if (!tabId) return;
35 void op(tabId).catch(() => undefined);
36 };
37
38 const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
39 return (
40 <Menu>
41 <Tooltip>
42 <TooltipTrigger
43 render={
44 <MenuTrigger
45 render={
46 <Button variant="ghost" size="icon-xs" type="button" aria-label="Preview menu" />
47 }
48 />
49 }
50 >
51 <MoreVertical />
52 </TooltipTrigger>
53 <TooltipPopup>More</TooltipPopup>
54 </Tooltip>
55 <MenuPopup align="end" sideOffset={6} className="min-w-56">
56 <MenuItem onClick={callTab(bridge.hardReload)} disabled={tabDisabled}>
57 Hard reload
58 </MenuItem>
59 <MenuItem onClick={callTab(bridge.openDevTools)} disabled={tabDisabled}>
60 Open DevTools
61 </MenuItem>
62 {/*
63 Zoom row: label + inline control cluster. `closeOnClick=false`
64 keeps the menu open while the user clicks the +/− buttons.
65 */}
66 <MenuItem
67 closeOnClick={false}
68 onClick={(event: React.MouseEvent) => event.preventDefault()}
69 className="justify-between"
70 disabled={tabDisabled}
71 >
72 <span>Zoom</span>
73 <span className="flex items-center gap-1">
74 <Button
75 variant="outline"
76 size="icon-xs"
77 type="button"
78 onClick={callTab(bridge.zoomOut)}
79 aria-label="Zoom out"
80 disabled={tabDisabled}
81 >
82 <Minus />
83 </Button>
84 <span className="min-w-12 text-center text-xs tabular-nums text-muted-foreground">
85 {zoomLabel}
86 </span>
87 <Button
88 variant="outline"
89 size="icon-xs"
90 type="button"
91 onClick={callTab(bridge.zoomIn)}
92 aria-label="Zoom in"
93 disabled={tabDisabled}
94 >
95 <PlusIcon />
96 </Button>
97 <Button
98 variant="ghost"
99 size="icon-xs"
100 type="button"
101 onClick={callTab(bridge.resetZoom)}
102 aria-label="Reset zoom"
103 disabled={tabDisabled}
104 >
105 <RotateCcw />
106 </Button>
107 </span>
108 </MenuItem>
109 <MenuSeparator />
110 <MenuItem onClick={() => void bridge.clearCookies().catch(() => undefined)}>
111 Clear cookies
112 </MenuItem>
113 <MenuItem onClick={() => void bridge.clearCache().catch(() => undefined)}>
114 Clear cache
115 </MenuItem>
116 </MenuPopup>
117 </Menu>
118 );
119}