Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BrowserTabMenu.tsx

152 lines5.3 KB

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}: {
45 readonly environmentId: EnvironmentId;
46 readonly tab: PreviewSessionSnapshot;
47 readonly disabled: boolean;
48}) {
49 const adjust = useAtomCommand(previewEnvironment.adjust, "browser tab change");
50 const resize = useAtomCommand(previewEnvironment.resize, "browser viewport change");
51 const target = { threadId: tab.threadId as PreviewAdjustInput["threadId"], tabId: tab.tabId };
52 const colorScheme = tab.colorScheme ?? "system";
53 const zoomFactor = tab.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR;
54 const zoomIndex = PREVIEW_ZOOM_LEVELS.indexOf(zoomFactor);
55 const zoomTo = (step: -1 | 1) =>
56 PREVIEW_ZOOM_LEVELS[
57 Math.min(Math.max((zoomIndex < 0 ? 7 : zoomIndex) + step, 0), PREVIEW_ZOOM_LEVELS.length - 1)
58 ]!;
59 const viewportId =
60 tab.viewport === undefined || tab.viewport._tag === "fill"
61 ? "fill"
62 : tab.viewport._tag === "preset"
63 ? `preset:${tab.viewport.presetId}`
64 : "custom";
65​
66 const actions: MenuAction[] = [
67 { id: "hard-reload", title: "Hard reload", image: "arrow.clockwise.circle" },
68 {
69 id: "appearance",
70 title: "Appearance",
71 image: "circle.lefthalf.filled",
72 subactions: APPEARANCES.map((option) => ({
73 id: `appearance:${option.id}`,
74 title: option.title,
75 state: option.id === colorScheme ? "on" : "off",
76 })),
77 },
78 {
79 id: "zoom",
80 title: `Zoom (${Math.round(zoomFactor * 100)}%)`,
81 image: "plus.magnifyingglass",
82 subactions: [
83 { id: "zoom:in", title: "Zoom in", image: "plus.magnifyingglass" },
84 { id: "zoom:out", title: "Zoom out", image: "minus.magnifyingglass" },
85 { id: "zoom:reset", title: "Actual size", image: "1.magnifyingglass" },
86 ],
87 },
88 {
89 id: "viewport",
90 title: "Viewport",
91 image: "rectangle.and.arrow.up.right.and.arrow.down.left",
92 subactions: VIEWPORTS.map((option) => ({
93 id: `viewport:${option.id}`,
94 title: option.title,
95 state: option.id === viewportId ? "on" : "off",
96 })),
97 },
98 {
99 id: "site-data",
100 title: "Site data",
101 image: "trash",
102 subactions: [
103 { id: "clear:cookies", title: "Clear cookies", attributes: { destructive: true } },
104 { id: "clear:cache", title: "Clear cache", attributes: { destructive: true } },
105 ],
106 },
107 ];
108​
109 const run = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {
110 const result = await adjust({ environmentId, input: { ...target, ...change } });
111 if (result._tag === "Failure") Dialog.alert("Could not change this browser tab");
112 };
113​
114 const onAction = (id: string) => {
115 if (id === "hard-reload") return void run({ hardReload: true });
116 if (id === "zoom:in") return void run({ zoomFactor: zoomTo(1) });
117 if (id === "zoom:out") return void run({ zoomFactor: zoomTo(-1) });
118 if (id === "zoom:reset") return void run({ zoomFactor: DEFAULT_PREVIEW_ZOOM_FACTOR });
119 if (id === "clear:cookies") return void run({ clear: "cookies" });
120 if (id === "clear:cache") return void run({ clear: "cache" });
121 if (id.startsWith("appearance:")) {
122 const option = APPEARANCES.find((candidate) => `appearance:${candidate.id}` === id);
123 if (option) void run({ colorScheme: option.id });
124 return;
125 }
126 if (id.startsWith("viewport:")) {
127 const option = VIEWPORTS.find((candidate) => `viewport:${candidate.id}` === id);
128 if (!option) return;
129 void resize({ environmentId, input: { ...target, viewport: option.setting } }).then(
130 (result) => {
131 if (result._tag === "Failure") Dialog.alert("Could not change the viewport");
132 },
133 );
134 }
135 };
136​
137 const pill = (
138 <ControlPill icon="ellipsis" accessibilityLabel="Browser tab options" disabled={disabled} />
139 );
140 if (disabled) return pill;
141 return (
142 <Menu
143 accessible
144 accessibilityLabel="Browser tab options"
145 accessibilityRole="button"
146 actions={actions}
147 onPressAction={({ nativeEvent }) => onAction(nativeEvent.event)}
148 >
149 {pill}
150 </Menu>
151 );