import type { ConfirmDialogOptions, ContextMenuItem, LocalApi } from "@t3tools/contracts"; import { requestConfirmDialog } from "./confirmDialog";import { showContextMenuFallback } from "./contextMenuFallback";import { readBrowserClientSettings, writeBrowserClientSettings } from "./clientPersistenceStorage";import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState"; let cachedApi: LocalApi | undefined; function createBrowserLocalApi(): LocalApi { return { dialogs: { pickFolder: async (options) => { if (!window.desktopBridge) return null; return window.desktopBridge.pickFolder(options); }, confirm: async (message, options?: ConfirmDialogOptions) => { return requestConfirmDialog(message, options) ?? false; }, }, shell: { openExternal: async (url) => { if (window.desktopBridge) { const opened = await window.desktopBridge.openExternal(url); if (!opened) { throw new Error("Unable to open link."); } return; } window.open(url, "_blank", "noopener,noreferrer"); }, }, contextMenu: { show: async <T extends string>( items: readonly ContextMenuItem<T>[], position?: { x: number; y: number }, ): Promise<T | null> => { if (window.desktopBridge) { return window.desktopBridge.showContextMenu(items, position) as Promise<T | null>; } return showContextMenuFallback(items, position); }, }, persistence: { getClientSettings: async () => { if (window.desktopBridge) { return window.desktopBridge.getClientSettings(); } return readBrowserClientSettings(); }, setClientSettings: async (settings) => { if (window.desktopBridge) { return window.desktopBridge.setClientSettings(settings); } writeBrowserClientSettings(settings); }, }, };} export function createLocalApi(): LocalApi { return createBrowserLocalApi();} export function readLocalApi(): LocalApi | undefined { if (typeof window === "undefined") return undefined; if (cachedApi) return cachedApi; cachedApi = createLocalApi(); return cachedApi;} export function ensureLocalApi(): LocalApi { const api = readLocalApi(); if (!api) { throw new Error("Local API not found"); } return api;} export async function __resetLocalApiForTests() { cachedApi = undefined; const { __resetClientSettingsPersistenceForTests } = await import("./hooks/useSettings"); __resetClientSettingsPersistenceForTests(); resetRequestLatencyStateForTests();}
import type { ConfirmDialogOptions, ContextMenuItem, LocalApi } from "@t3tools/contracts";
import { requestConfirmDialog } from "./confirmDialog";
import { showContextMenuFallback } from "./contextMenuFallback";
import { readBrowserClientSettings, writeBrowserClientSettings } from "./clientPersistenceStorage";
import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
let cachedApi: LocalApi | undefined;
function createBrowserLocalApi(): LocalApi {
return {
dialogs: {
pickFolder: async (options) => {
if (!window.desktopBridge) return null;
return window.desktopBridge.pickFolder(options);
},
confirm: async (message, options?: ConfirmDialogOptions) => {
return requestConfirmDialog(message, options) ?? false;
},
},
shell: {
openExternal: async (url) => {
if (window.desktopBridge) {
const opened = await window.desktopBridge.openExternal(url);
if (!opened) {
throw new Error("Unable to open link.");
}
return;
}
window.open(url, "_blank", "noopener,noreferrer");
},
},
contextMenu: {
show: async <T extends string>(
items: readonly ContextMenuItem<T>[],
position?: { x: number; y: number },
): Promise<T | null> => {
if (window.desktopBridge) {
return window.desktopBridge.showContextMenu(items, position) as Promise<T | null>;
}
return showContextMenuFallback(items, position);
},
},
persistence: {
getClientSettings: async () => {
if (window.desktopBridge) {
return window.desktopBridge.getClientSettings();
}
return readBrowserClientSettings();
},
setClientSettings: async (settings) => {
if (window.desktopBridge) {
return window.desktopBridge.setClientSettings(settings);
}
writeBrowserClientSettings(settings);
},
},
};
}
export function createLocalApi(): LocalApi {
return createBrowserLocalApi();
}
export function readLocalApi(): LocalApi | undefined {
if (typeof window === "undefined") return undefined;
if (cachedApi) return cachedApi;
cachedApi = createLocalApi();
return cachedApi;
}
export function ensureLocalApi(): LocalApi {
const api = readLocalApi();
if (!api) {
throw new Error("Local API not found");
}
return api;
}
export async function __resetLocalApiForTests() {
cachedApi = undefined;
const { __resetClientSettingsPersistenceForTests } = await import("./hooks/useSettings");
__resetClientSettingsPersistenceForTests();
resetRequestLatencyStateForTests();
}