Open History to see the last commit
1import { contextBridge, ipcRenderer } from "electron";
2import type { DesktopBridge } from "@t3tools/contracts";
3
4const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
5const CONFIRM_CHANNEL = "desktop:confirm";
6const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
7const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
8const MENU_ACTION_CHANNEL = "desktop:menu-action";
9const wsUrl = process.env.T3CODE_DESKTOP_WS_URL ?? null;
10
11contextBridge.exposeInMainWorld("desktopBridge", {
12 getWsUrl: () => wsUrl,
13 pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL),
14 confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
15 showContextMenu: (items) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items),
16 openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url),
17 onMenuAction: (listener) => {
18 const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
19 if (typeof action !== "string") return;
20 listener(action);
21 };
22
23 ipcRenderer.on(MENU_ACTION_CHANNEL, wrappedListener);
24 return () => {
25 ipcRenderer.removeListener(MENU_ACTION_CHANNEL, wrappedListener);
26 };
27 },
28} satisfies DesktopBridge);