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
25/** One preset per kind of screen; the web's device toolbar offers the full list. */
26const VIEWPORTS = [
27 { id: "fill", title: "Fit to screen", setting: FILL_PREVIEW_VIEWPORT },
28 ...(["Phone", "Tablet", "Desktop"] as const).flatMap((category) => {
29 const preset = PREVIEW_VIEWPORT_PRESETS.find((candidate) => candidate.category === category);
30 if (!preset) return [];
31 const setting: PreviewViewportSetting = {
32 _tag: "preset",
33 presetId: preset.id,
34 width: preset.width,
35 height: preset.height,
36 };
37 return [{ id: `preset:${preset.id}`, title: `${category} (${preset.label})`, setting }];
38 }),
39];
40
41/**
42 * The browser tab's menu: appearance, zoom, viewport, a hard reload, and its
43 * profile's site data. Each runs through the environment, as on web and
44 * desktop, so every client and agent sees the same tab. None need control.
45 */
46export function BrowserTabMenu({
47 environmentId,
48 tab,
49 disabled,
50}: {
51 readonly environmentId: EnvironmentId;
52 readonly tab: PreviewSessionSnapshot;
53 readonly disabled: boolean;
54}) {
55 const adjust = useAtomCommand(previewEnvironment.adjust, "browser tab change");
56 const resize = useAtomCommand(previewEnvironment.resize, "browser viewport change");
57 const target = { threadId: tab.threadId as PreviewAdjustInput["threadId"], tabId: tab.tabId };
58 const colorScheme = tab.colorScheme ?? "system";
59 const zoomFactor = tab.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR;
60 const zoomIndex = PREVIEW_ZOOM_LEVELS.indexOf(zoomFactor);
61 const zoomTo = (step: -1 | 1) =>
62 PREVIEW_ZOOM_LEVELS[
63 Math.min(Math.max((zoomIndex < 0 ? 7 : zoomIndex) + step, 0), PREVIEW_ZOOM_LEVELS.length - 1)
64 ]!;
65 const viewportId =
66 tab.viewport === undefined || tab.viewport._tag === "fill"
67 ? "fill"
68 : tab.viewport._tag === "preset"
69 ? `preset:${tab.viewport.presetId}`
70 : "custom";
71
72 const actions: MenuAction[] = [
73 { id: "hard-reload", title: "Hard reload", image: "arrow.clockwise.circle" },
74 {
75 id: "appearance",
76 title: "Appearance",
77 image: "circle.lefthalf.filled",
78 subactions: APPEARANCES.map((option) => ({
79 id: `appearance:${option.id}`,
80 title: option.title,
81 state: option.id === colorScheme ? "on" : "off",
82 })),
83 },
84 {
85 id: "zoom",
86 title: `Zoom (${Math.round(zoomFactor * 100)}%)`,
87 image: "plus.magnifyingglass",
88 subactions: [
89 { id: "zoom:in", title: "Zoom in", image: "plus.magnifyingglass" },
90 { id: "zoom:out", title: "Zoom out", image: "minus.magnifyingglass" },
91 { id: "zoom:reset", title: "Actual size", image: "1.magnifyingglass" },
92 ],
93 },
94 {
95 id: "viewport",
96 title: "Viewport",
97 image: "rectangle.and.arrow.up.right.and.arrow.down.left",
98 subactions: VIEWPORTS.map((option) => ({
99 id: `viewport:${option.id}`,
100 title: option.title,
101 state: option.id === viewportId ? "on" : "off",
102 })),
103 },
104 {
105 id: "site-data",
106 title: "Site data",
107 image: "trash",
108 subactions: [
109 { id: "clear:cookies", title: "Clear cookies", attributes: { destructive: true } },
110 { id: "clear:cache", title: "Clear cache", attributes: { destructive: true } },
111 ],
112 },
113 ];
114
115 const run = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {
116 const result = await adjust({ environmentId, input: { ...target, ...change } });
117 if (result._tag === "Failure") Dialog.alert("Could not change this browser tab");
118 };
119
120 const onAction = (id: string) => {
121 if (id === "hard-reload") return void run({ hardReload: true });
122 if (id === "zoom:in") return void run({ zoomFactor: zoomTo(1) });
123 if (id === "zoom:out") return void run({ zoomFactor: zoomTo(-1) });
124 if (id === "zoom:reset") return void run({ zoomFactor: DEFAULT_PREVIEW_ZOOM_FACTOR });
125 if (id === "clear:cookies") return void run({ clear: "cookies" });
126 if (id === "clear:cache") return void run({ clear: "cache" });
127 if (id.startsWith("appearance:")) {
128 const option = APPEARANCES.find((candidate) => `appearance:${candidate.id}` === id);
129 if (option) void run({ colorScheme: option.id });
130 return;
131 }
132 if (id.startsWith("viewport:")) {
133 const option = VIEWPORTS.find((candidate) => `viewport:${candidate.id}` === id);
134 if (!option) return;
135 void resize({ environmentId, input: { ...target, viewport: option.setting } }).then(
136 (result) => {
137 if (result._tag === "Failure") Dialog.alert("Could not change the viewport");
138 },
139 );
140 }
141 };
142
143 const pill = (
144 <ControlPill icon="ellipsis" accessibilityLabel="Browser tab options" disabled={disabled} />
145 );
146 if (disabled) return pill;
147 return (
148 <Menu
149 accessible
150 accessibilityLabel="Browser tab options"
151 accessibilityRole="button"
152 actions={actions}
153 onPressAction={({ nativeEvent }) => onAction(nativeEvent.event)}
154 >
155 {pill}
156 </Menu>
157 );
158}