Open History to see the last commit
1import type { ContextMenuItem, LocalApi } from "@t3tools/contracts";
2
3import { resetGitStatusStateForTests } from "./lib/gitStatusState";
4import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
5import { resetServerStateForTests } from "./rpc/serverState";
6import { resetWsConnectionStateForTests } from "./rpc/wsConnectionState";
7import {
8 resetSavedEnvironmentRegistryStoreForTests,
9 resetSavedEnvironmentRuntimeStoreForTests,
10} from "./environments/runtime";
11import {
12 getPrimaryEnvironmentConnection,
13 resetEnvironmentServiceForTests,
14} from "./environments/runtime";
15import { type WsRpcClient } from "./rpc/wsRpcClient";
16import { showContextMenuFallback } from "./contextMenuFallback";
17
18let cachedApi: LocalApi | undefined;
19
20export function createLocalApi(rpcClient: WsRpcClient): LocalApi {
21 return {
22 dialogs: {
23 pickFolder: async () => {
24 if (!window.desktopBridge) return null;
25 return window.desktopBridge.pickFolder();
26 },
27 confirm: async (message) => {
28 if (window.desktopBridge) {
29 return window.desktopBridge.confirm(message);
30 }
31 return window.confirm(message);
32 },
33 },
34 shell: {
35 openInEditor: (cwd, editor) => rpcClient.shell.openInEditor({ cwd, editor }),
36 openExternal: async (url) => {
37 if (window.desktopBridge) {
38 const opened = await window.desktopBridge.openExternal(url);
39 if (!opened) {
40 throw new Error("Unable to open link.");
41 }
42 return;
43 }
44
45 window.open(url, "_blank", "noopener,noreferrer");
46 },
47 },
48 contextMenu: {
49 show: async <T extends string>(
50 items: readonly ContextMenuItem<T>[],
51 position?: { x: number; y: number },
52 ): Promise<T | null> => {
53 if (window.desktopBridge) {
54 return window.desktopBridge.showContextMenu(items, position) as Promise<T | null>;
55 }
56 return showContextMenuFallback(items, position);
57 },
58 },
59 server: {
60 getConfig: rpcClient.server.getConfig,
61 refreshProviders: rpcClient.server.refreshProviders,
62 upsertKeybinding: rpcClient.server.upsertKeybinding,
63 getSettings: rpcClient.server.getSettings,
64 updateSettings: rpcClient.server.updateSettings,
65 },
66 };
67}
68
69export function readLocalApi(): LocalApi | undefined {
70 if (typeof window === "undefined") return undefined;
71 if (cachedApi) return cachedApi;
72
73 if (window.nativeApi) {
74 cachedApi = window.nativeApi;
75 return cachedApi;
76 }
77
78 cachedApi = createLocalApi(getPrimaryEnvironmentConnection().client);
79 return cachedApi;
80}
81
82export function ensureLocalApi(): LocalApi {
83 const api = readLocalApi();
84 if (!api) {
85 throw new Error("Local API not found");
86 }
87 return api;
88}
89
90export async function __resetLocalApiForTests() {
91 cachedApi = undefined;
92 await resetEnvironmentServiceForTests();
93 resetGitStatusStateForTests();
94 resetRequestLatencyStateForTests();
95 resetSavedEnvironmentRegistryStoreForTests();
96 resetSavedEnvironmentRuntimeStoreForTests();
97 resetServerStateForTests();
98 resetWsConnectionStateForTests();
99}