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, 03:37 UTC
| 1 | import type { |
| 2 | ConfirmDialogOptions, |
| 3 | ContextMenuItem, |
| 4 | ContextMenuSource, |
| 5 | LocalApi, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { msg } from "gt-react"; |
| 8 | |
| 9 | import { isWebUrl } from "./browser/webUrl"; |
| 10 | import { requestConfirmDialog } from "./confirmDialog"; |
| 11 | import { dismissContextMenu, showContextMenuFallback } from "./contextMenuFallback"; |
| 12 | import { readBrowserClientSettings, writeBrowserClientSettings } from "./clientPersistenceStorage"; |
| 13 | import { m } from "./i18n/messages"; |
| 14 | |
| 15 | let cachedApi: LocalApi | undefined; |
| 16 | |
| 17 | export function createLocalApi(): LocalApi { |
| 18 | return { |
| 19 | dialogs: { |
| 20 | pickFolder: async (options) => { |
| 21 | if (!window.desktopBridge) return null; |
| 22 | return window.desktopBridge.pickFolder(options); |
| 23 | }, |
| 24 | confirm: async (message, options?: ConfirmDialogOptions) => { |
| 25 | return requestConfirmDialog(message, options) ?? false; |
| 26 | }, |
| 27 | }, |
| 28 | shell: { |
| 29 | openExternal: async (url) => { |
| 30 | if (window.desktopBridge) { |
| 31 | const opened = await window.desktopBridge.openExternal(url); |
| 32 | if (!opened) { |
| 33 | throw new Error(m(msg("Unable to open link."))); |
| 34 | } |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (!isWebUrl(url)) throw new Error(m(msg("Unable to open link."))); |
| 39 | window.open(url, "_blank", "noopener,noreferrer"); |
| 40 | }, |
| 41 | openSystemSettings: async (pane) => { |
| 42 | if (!window.desktopBridge) { |
| 43 | throw new Error(m(msg("Unable to open System Settings."))); |
| 44 | } |
| 45 | const opened = await window.desktopBridge.openSystemSettings(pane); |
| 46 | if (!opened) { |
| 47 | throw new Error(m(msg("Unable to open System Settings."))); |
| 48 | } |
| 49 | }, |
| 50 | }, |
| 51 | contextMenu: { |
| 52 | show: async <T extends string>( |
| 53 | items: readonly ContextMenuItem<T>[], |
| 54 | position?: { x: number; y: number }, |
| 55 | source?: ContextMenuSource, |
| 56 | ): Promise<T | null> => { |
| 57 | if (window.desktopBridge) { |
| 58 | return window.desktopBridge.showContextMenu(items, position, source) as Promise<T | null>; |
| 59 | } |
| 60 | return showContextMenuFallback(items, position); |
| 61 | }, |
| 62 | // Unlike native menus, the DOM fallback needs dismissal when underlying state disappears. |
| 63 | close: async () => { |
| 64 | if (!window.desktopBridge) { |
| 65 | dismissContextMenu(); |
| 66 | } |
| 67 | }, |
| 68 | }, |
| 69 | persistence: { |
| 70 | getClientSettings: async () => { |
| 71 | if (window.desktopBridge) { |
| 72 | return window.desktopBridge.getClientSettings(); |
| 73 | } |
| 74 | return readBrowserClientSettings(); |
| 75 | }, |
| 76 | setClientSettings: async (settings) => { |
| 77 | if (window.desktopBridge) { |
| 78 | return window.desktopBridge.setClientSettings(settings); |
| 79 | } |
| 80 | writeBrowserClientSettings(settings); |
| 81 | }, |
| 82 | }, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | export function readLocalApi(): LocalApi | undefined { |
| 87 | if (typeof window === "undefined") return undefined; |
| 88 | if (cachedApi) return cachedApi; |
| 89 | |
| 90 | cachedApi = createLocalApi(); |
| 91 | return cachedApi; |
| 92 | } |
| 93 | |
| 94 | export function ensureLocalApi(): LocalApi { |
| 95 | const api = readLocalApi(); |
| 96 | if (!api) { |
| 97 | throw new Error("Local API not found"); |
| 98 | } |
| 99 | return api; |
| 100 | } |