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