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

wsNativeApi.ts

107 lines3.7 KB

1import { type ContextMenuItem, type NativeApi } from "@t3tools/contracts";
2​
3import { showContextMenuFallback } from "./contextMenuFallback";
4import { resetServerStateForTests } from "./rpc/serverState";
5import { __resetWsRpcClientForTests, getWsRpcClient } from "./wsRpcClient";
6​
7let instance: { api: NativeApi } | null = null;
8​
9export function __resetWsNativeApiForTests() {
10 instance = null;
11 __resetWsRpcClientForTests();
12 resetServerStateForTests();
14​
15export function createWsNativeApi(): NativeApi {
16 if (instance) {
17 return instance.api;
18 }
19​
20 const rpcClient = getWsRpcClient();
21​
22 const api: NativeApi = {
23 dialogs: {
24 pickFolder: async () => {
25 if (!window.desktopBridge) return null;
26 return window.desktopBridge.pickFolder();
27 },
28 confirm: async (message) => {
29 if (window.desktopBridge) {
30 return window.desktopBridge.confirm(message);
31 }
32 return window.confirm(message);
33 },
34 },
35 terminal: {
36 open: (input) => rpcClient.terminal.open(input as never),
37 write: (input) => rpcClient.terminal.write(input as never),
38 resize: (input) => rpcClient.terminal.resize(input as never),
39 clear: (input) => rpcClient.terminal.clear(input as never),
40 restart: (input) => rpcClient.terminal.restart(input as never),
41 close: (input) => rpcClient.terminal.close(input as never),
42 onEvent: (callback) => rpcClient.terminal.onEvent(callback),
43 },
44 projects: {
45 searchEntries: rpcClient.projects.searchEntries,
46 writeFile: rpcClient.projects.writeFile,
47 },
48 shell: {
49 openInEditor: (cwd, editor) => rpcClient.shell.openInEditor({ cwd, editor }),
50 openExternal: async (url) => {
51 if (window.desktopBridge) {
52 const opened = await window.desktopBridge.openExternal(url);
53 if (!opened) {
54 throw new Error("Unable to open link.");
55 }
56 return;
57 }
58​
59 window.open(url, "_blank", "noopener,noreferrer");
60 },
61 },
62 git: {
63 pull: rpcClient.git.pull,
64 status: rpcClient.git.status,
65 listBranches: rpcClient.git.listBranches,
66 createWorktree: rpcClient.git.createWorktree,
67 removeWorktree: rpcClient.git.removeWorktree,
68 createBranch: rpcClient.git.createBranch,
69 checkout: rpcClient.git.checkout,
70 init: rpcClient.git.init,
71 resolvePullRequest: rpcClient.git.resolvePullRequest,
72 preparePullRequestThread: rpcClient.git.preparePullRequestThread,
73 },
74 contextMenu: {
75 show: async <T extends string>(
76 items: readonly ContextMenuItem<T>[],
77 position?: { x: number; y: number },
78 ): Promise<T | null> => {
79 if (window.desktopBridge) {
80 return window.desktopBridge.showContextMenu(items, position) as Promise<T | null>;
81 }
82 return showContextMenuFallback(items, position);
83 },
84 },
85 server: {
86 getConfig: rpcClient.server.getConfig,
87 refreshProviders: rpcClient.server.refreshProviders,
88 upsertKeybinding: rpcClient.server.upsertKeybinding,
89 getSettings: rpcClient.server.getSettings,
90 updateSettings: rpcClient.server.updateSettings,
91 },
92 orchestration: {
93 getSnapshot: rpcClient.orchestration.getSnapshot,
94 dispatchCommand: rpcClient.orchestration.dispatchCommand,
95 getTurnDiff: rpcClient.orchestration.getTurnDiff,
96 getFullThreadDiff: rpcClient.orchestration.getFullThreadDiff,
97 replayEvents: (fromSequenceExclusive) =>
98 rpcClient.orchestration
99 .replayEvents({ fromSequenceExclusive })
100 .then((events) => [...events]),
101 onDomainEvent: (callback) => rpcClient.orchestration.onDomainEvent(callback),
102 },
103 };
104​
105 instance = { api };
106 return api;