The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Dialog from "../../components/ui/Dialog"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import type { MenuAction } from "@react-native-menu/menu"; |
| 4 | import { |
| 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"; |
| 13 | import { PREVIEW_VIEWPORT_PRESETS } from "@t3tools/shared/previewViewport"; |
| 14 | import { msg, useGT, useMessages } from "gt-react-native"; |
| 15 | |
| 16 | import { ControlPill } from "../../components/ui/ControlPill"; |
| 17 | import { Menu } from "../../components/ui/Menu"; |
| 18 | import { m } from "../../i18n/messages"; |
| 19 | import { useEnvironmentPreviewTunnelOff } from "../../state/entities"; |
| 20 | import { previewEnvironment } from "../../state/preview"; |
| 21 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 22 | import { browserTabUrl, loopbackUrlForDevice } from "./browserTabs"; |
| 23 | import { useOpenLocalPreview } from "./useOpenLocalPreview"; |
| 24 | |
| 25 | const APPEARANCES = [ |
| 26 | { |
| 27 | id: "system", |
| 28 | title: msg("System", { $context: "appearance option, follow the system theme" }), |
| 29 | }, |
| 30 | { id: "light", title: msg("Light", { $context: "appearance option" }) }, |
| 31 | { id: "dark", title: msg("Dark", { $context: "appearance option" }) }, |
| 32 | ] as const; |
| 33 | |
| 34 | const VIEWPORTS = [ |
| 35 | { id: "fill", title: msg("Fit to screen"), setting: FILL_PREVIEW_VIEWPORT }, |
| 36 | ...(["Phone", "Tablet", "Desktop"] as const).flatMap((category) => { |
| 37 | const preset = PREVIEW_VIEWPORT_PRESETS.find((candidate) => candidate.category === category); |
| 38 | if (!preset) return []; |
| 39 | const setting: PreviewViewportSetting = { |
| 40 | _tag: "preset", |
| 41 | presetId: preset.id, |
| 42 | width: preset.width, |
| 43 | height: preset.height, |
| 44 | }; |
| 45 | const title = |
| 46 | category === "Phone" |
| 47 | ? msg("Phone ({device})", { device: preset.label }) |
| 48 | : category === "Tablet" |
| 49 | ? msg("Tablet ({device})", { device: preset.label }) |
| 50 | : msg("Desktop ({device})", { device: preset.label }); |
| 51 | return [{ id: `preset:${preset.id}`, title, setting }]; |
| 52 | }), |
| 53 | ]; |
| 54 | |
| 55 | export function BrowserTabMenu({ |
| 56 | environmentId, |
| 57 | tab, |
| 58 | streaming, |
| 59 | onPaste, |
| 60 | onReleaseControl, |
| 61 | }: { |
| 62 | readonly environmentId: EnvironmentId; |
| 63 | readonly tab: PreviewSessionSnapshot; |
| 64 | readonly streaming: boolean; |
| 65 | readonly onPaste?: () => void; |
| 66 | readonly onReleaseControl?: () => void; |
| 67 | }) { |
| 68 | const openLocalPreview = useOpenLocalPreview(); |
| 69 | const tunnelOff = useEnvironmentPreviewTunnelOff(environmentId); |
| 70 | const adjust = useAtomCommand(previewEnvironment.adjust, "browser tab change"); |
| 71 | const resize = useAtomCommand(previewEnvironment.resize, "browser viewport change"); |
| 72 | const close = useAtomCommand(previewEnvironment.close, "browser tab close"); |
| 73 | const canClose = useAtomValue(previewEnvironment.close.permissionAtom(environmentId)); |
| 74 | const gt = useGT(); |
| 75 | const resolveMessage = useMessages(); |
| 76 | const target = { threadId: tab.threadId as PreviewAdjustInput["threadId"], tabId: tab.tabId }; |
| 77 | const colorScheme = tab.colorScheme ?? "system"; |
| 78 | const zoomFactor = tab.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR; |
| 79 | const zoomIndex = PREVIEW_ZOOM_LEVELS.indexOf(zoomFactor); |
| 80 | const zoomTo = (step: -1 | 1) => |
| 81 | PREVIEW_ZOOM_LEVELS[ |
| 82 | Math.min(Math.max((zoomIndex < 0 ? 7 : zoomIndex) + step, 0), PREVIEW_ZOOM_LEVELS.length - 1) |
| 83 | ]!; |
| 84 | const viewportId = |
| 85 | tab.viewport === undefined || tab.viewport._tag === "fill" |
| 86 | ? "fill" |
| 87 | : tab.viewport._tag === "preset" |
| 88 | ? `preset:${tab.viewport.presetId}` |
| 89 | : "custom"; |
| 90 | |
| 91 | const closeAction: MenuAction = { |
| 92 | id: "close", |
| 93 | title: gt("Close tab"), |
| 94 | image: "xmark", |
| 95 | attributes: { destructive: true, disabled: !canClose }, |
| 96 | }; |
| 97 | const deviceUrl = tunnelOff ? null : loopbackUrlForDevice(browserTabUrl(tab)); |
| 98 | // The device preview has its own webview and tunnel, so it doesn't wait for the stream. |
| 99 | const openOnDevice: MenuAction[] = deviceUrl |
| 100 | ? [{ id: "open-on-device", title: gt("Open on this device"), image: "iphone" }] |
| 101 | : []; |
| 102 | const actions: MenuAction[] = [ |
| 103 | ...openOnDevice, |
| 104 | ...(onPaste |
| 105 | ? [{ id: "paste", title: gt("Paste", { $context: "verb" }), image: "doc.on.clipboard" }] |
| 106 | : []), |
| 107 | ...(onReleaseControl |
| 108 | ? [{ id: "release-control", title: gt("Release control"), image: "hand.raised" }] |
| 109 | : []), |
| 110 | { id: "hard-reload", title: gt("Hard reload"), image: "arrow.clockwise.circle" }, |
| 111 | { |
| 112 | id: "appearance", |
| 113 | title: gt("Appearance"), |
| 114 | image: "circle.lefthalf.filled", |
| 115 | subactions: APPEARANCES.map((option) => ({ |
| 116 | id: `appearance:${option.id}`, |
| 117 | title: resolveMessage(option.title), |
| 118 | state: option.id === colorScheme ? "on" : "off", |
| 119 | })), |
| 120 | }, |
| 121 | { |
| 122 | id: "zoom", |
| 123 | title: gt("Zoom ({percent}%)", { percent: Math.round(zoomFactor * 100) }), |
| 124 | image: "plus.magnifyingglass", |
| 125 | subactions: [ |
| 126 | { id: "zoom:in", title: gt("Zoom in"), image: "plus.magnifyingglass" }, |
| 127 | { id: "zoom:out", title: gt("Zoom out"), image: "minus.magnifyingglass" }, |
| 128 | { id: "zoom:reset", title: gt("Actual size"), image: "1.magnifyingglass" }, |
| 129 | ], |
| 130 | }, |
| 131 | { |
| 132 | id: "viewport", |
| 133 | title: gt("Viewport"), |
| 134 | image: "rectangle.and.arrow.up.right.and.arrow.down.left", |
| 135 | subactions: VIEWPORTS.map((option) => ({ |
| 136 | id: `viewport:${option.id}`, |
| 137 | title: resolveMessage(option.title), |
| 138 | state: option.id === viewportId ? "on" : "off", |
| 139 | })), |
| 140 | }, |
| 141 | { |
| 142 | id: "site-data", |
| 143 | title: gt("Site data"), |
| 144 | image: "trash", |
| 145 | subactions: [ |
| 146 | { id: "clear:cookies", title: gt("Clear cookies"), attributes: { destructive: true } }, |
| 147 | { id: "clear:cache", title: gt("Clear cache"), attributes: { destructive: true } }, |
| 148 | ], |
| 149 | }, |
| 150 | closeAction, |
| 151 | ]; |
| 152 | |
| 153 | const run = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => { |
| 154 | const result = await adjust({ environmentId, input: { ...target, ...change } }); |
| 155 | if (result._tag === "Failure") Dialog.alert(m(msg("Could not change this browser tab"))); |
| 156 | }; |
| 157 | |
| 158 | const onAction = (id: string) => { |
| 159 | if (id === "close") { |
| 160 | void close({ environmentId, input: target }).then((result) => { |
| 161 | if (result._tag === "Failure") Dialog.alert(m(msg("Could not close this browser tab"))); |
| 162 | }); |
| 163 | return; |
| 164 | } |
| 165 | if (id === "open-on-device" && deviceUrl) { |
| 166 | return openLocalPreview({ environmentId, threadId: tab.threadId }, deviceUrl); |
| 167 | } |
| 168 | if (id === "paste") return onPaste?.(); |
| 169 | if (id === "release-control") return onReleaseControl?.(); |
| 170 | if (id === "hard-reload") return void run({ hardReload: true }); |
| 171 | if (id === "zoom:in") return void run({ zoomFactor: zoomTo(1) }); |
| 172 | if (id === "zoom:out") return void run({ zoomFactor: zoomTo(-1) }); |
| 173 | if (id === "zoom:reset") return void run({ zoomFactor: DEFAULT_PREVIEW_ZOOM_FACTOR }); |
| 174 | if (id === "clear:cookies") return void run({ clear: "cookies" }); |
| 175 | if (id === "clear:cache") return void run({ clear: "cache" }); |
| 176 | if (id.startsWith("appearance:")) { |
| 177 | const option = APPEARANCES.find((candidate) => `appearance:${candidate.id}` === id); |
| 178 | if (option) void run({ colorScheme: option.id }); |
| 179 | return; |
| 180 | } |
| 181 | if (id.startsWith("viewport:")) { |
| 182 | const option = VIEWPORTS.find((candidate) => `viewport:${candidate.id}` === id); |
| 183 | if (!option) return; |
| 184 | void resize({ environmentId, input: { ...target, viewport: option.setting } }).then( |
| 185 | (result) => { |
| 186 | if (result._tag === "Failure") Dialog.alert(m(msg("Could not change the viewport"))); |
| 187 | }, |
| 188 | ); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | return ( |
| 193 | <Menu |
| 194 | // Remount when streaming changes, so an open menu never keeps offering |
| 195 | // the other state's actions. |
| 196 | key={streaming ? "streaming" : "idle"} |
| 197 | accessible |
| 198 | accessibilityLabel={gt("Browser tab options")} |
| 199 | accessibilityRole="button" |
| 200 | actions={streaming ? actions : [...openOnDevice, closeAction]} |
| 201 | onPressAction={({ nativeEvent }) => onAction(nativeEvent.event)} |
| 202 | > |
| 203 | <ControlPill icon="ellipsis" accessibilityLabel={gt("Browser tab options")} /> |
| 204 | </Menu> |
| 205 | ); |
| 206 | } |