Open History to see the last commit
1import * as Dialog from "../../components/ui/Dialog";
2import type { MenuAction } from "@react-native-menu/menu";
3import {
4 DEFAULT_PREVIEW_ZOOM_FACTOR,
5 type EnvironmentId,
6 FILL_PREVIEW_VIEWPORT,
7 PREVIEW_ZOOM_LEVELS,
8 type PreviewAdjustInput,
9 type PreviewSessionSnapshot,
10 type PreviewViewportSetting,
11} from "@t3tools/contracts";
12import { PREVIEW_VIEWPORT_PRESETS } from "@t3tools/shared/previewViewport";
13
14import { ControlPill } from "../../components/ui/ControlPill";
15import { Menu } from "../../components/ui/Menu";
16import { previewEnvironment } from "../../state/preview";
17import { useAtomCommand } from "../../state/use-atom-command";
18
19const APPEARANCES = [
20 { id: "system", title: "System" },
21 { id: "light", title: "Light" },
22 { id: "dark", title: "Dark" },
23] as const;
24
25const VIEWPORTS = [
26 { id: "fill", title: "Fit to screen", setting: FILL_PREVIEW_VIEWPORT },
27 ...(["Phone", "Tablet", "Desktop"] as const).flatMap((category) => {
28 const preset = PREVIEW_VIEWPORT_PRESETS.find((candidate) => candidate.category === category);
29 if (!preset) return [];
30 const setting: PreviewViewportSetting = {
31 _tag: "preset",
32 presetId: preset.id,
33 width: preset.width,
34 height: preset.height,
35 };
36 return [{ id: `preset:${preset.id}`, title: `${category} (${preset.label})`, setting }];
37 }),
38];
39
40export function BrowserTabMenu({
41 environmentId,
42 tab,
43 disabled,
44 onPaste,
45}: {
46 readonly environmentId: EnvironmentId;
47 readonly tab: PreviewSessionSnapshot;
48 readonly disabled: boolean;
49 readonly onPaste?: () => void;
50}) {
51 const adjust = useAtomCommand(previewEnvironment.adjust, "browser tab change");
52 const resize = useAtomCommand(previewEnvironment.resize, "browser viewport change");
53 const target = { threadId: tab.threadId as PreviewAdjustInput["threadId"], tabId: tab.tabId };
54 const colorScheme = tab.colorScheme ?? "system";
55 const zoomFactor = tab.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR;
56 const zoomIndex = PREVIEW_ZOOM_LEVELS.indexOf(zoomFactor);
57 const zoomTo = (step: -1 | 1) =>
58 PREVIEW_ZOOM_LEVELS[
59 Math.min(Math.max((zoomIndex < 0 ? 7 : zoomIndex) + step, 0), PREVIEW_ZOOM_LEVELS.length - 1)
60 ]!;
61 const viewportId =
62 tab.viewport === undefined || tab.viewport._tag === "fill"
63 ? "fill"
64 : tab.viewport._tag === "preset"
65 ? `preset:${tab.viewport.presetId}`
66 : "custom";
67
68 const actions: MenuAction[] = [
69 ...(onPaste ? [{ id: "paste", title: "Paste", image: "doc.on.clipboard" }] : []),
70 { id: "hard-reload", title: "Hard reload", image: "arrow.clockwise.circle" },
71 {
72 id: "appearance",
73 title: "Appearance",
74 image: "circle.lefthalf.filled",
75 subactions: APPEARANCES.map((option) => ({
76 id: `appearance:${option.id}`,
77 title: option.title,
78 state: option.id === colorScheme ? "on" : "off",
79 })),
80 },
81 {
82 id: "zoom",
83 title: `Zoom (${Math.round(zoomFactor * 100)}%)`,
84 image: "plus.magnifyingglass",
85 subactions: [
86 { id: "zoom:in", title: "Zoom in", image: "plus.magnifyingglass" },
87 { id: "zoom:out", title: "Zoom out", image: "minus.magnifyingglass" },
88 { id: "zoom:reset", title: "Actual size", image: "1.magnifyingglass" },
89 ],
90 },
91 {
92 id: "viewport",
93 title: "Viewport",
94 image: "rectangle.and.arrow.up.right.and.arrow.down.left",
95 subactions: VIEWPORTS.map((option) => ({
96 id: `viewport:${option.id}`,
97 title: option.title,
98 state: option.id === viewportId ? "on" : "off",
99 })),
100 },
101 {
102 id: "site-data",
103 title: "Site data",
104 image: "trash",
105 subactions: [
106 { id: "clear:cookies", title: "Clear cookies", attributes: { destructive: true } },
107 { id: "clear:cache", title: "Clear cache", attributes: { destructive: true } },
108 ],
109 },
110 ];
111
112 const run = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {
113 const result = await adjust({ environmentId, input: { ...target, ...change } });
114 if (result._tag === "Failure") Dialog.alert("Could not change this browser tab");
115 };
116
117 const onAction = (id: string) => {
118 if (id === "paste") return onPaste?.();
119 if (id === "hard-reload") return void run({ hardReload: true });
120 if (id === "zoom:in") return void run({ zoomFactor: zoomTo(1) });
121 if (id === "zoom:out") return void run({ zoomFactor: zoomTo(-1) });
122 if (id === "zoom:reset") return void run({ zoomFactor: DEFAULT_PREVIEW_ZOOM_FACTOR });
123 if (id === "clear:cookies") return void run({ clear: "cookies" });
124 if (id === "clear:cache") return void run({ clear: "cache" });
125 if (id.startsWith("appearance:")) {
126 const option = APPEARANCES.find((candidate) => `appearance:${candidate.id}` === id);
127 if (option) void run({ colorScheme: option.id });
128 return;
129 }
130 if (id.startsWith("viewport:")) {
131 const option = VIEWPORTS.find((candidate) => `viewport:${candidate.id}` === id);
132 if (!option) return;
133 void resize({ environmentId, input: { ...target, viewport: option.setting } }).then(
134 (result) => {
135 if (result._tag === "Failure") Dialog.alert("Could not change the viewport");
136 },
137 );
138 }
139 };
140
141 const pill = (
142 <ControlPill icon="ellipsis" accessibilityLabel="Browser tab options" disabled={disabled} />
143 );
144 if (disabled) return pill;
145 return (
146 <Menu
147 accessible
148 accessibilityLabel="Browser tab options"
149 accessibilityRole="button"
150 actions={actions}
151 onPressAction={({ nativeEvent }) => onAction(nativeEvent.event)}
152 >
153 {pill}
154 </Menu>
155 );
156}