// Where the app runs and which server (if any) this device talks to. In the // browser the API is same-origin and these helpers are inert; in a native // shell (Tauri) the app is standalone by default and a server is an explicit, // device-level choice stored locally. import { setApiBase, setAuthToken } from './api'; const SERVER_URL_KEY = 'synapse.serverUrl'; const TOKEN_KEY = 'synapse.serverToken'; const AUTOSYNC_KEY = 'synapse.autoSync'; export function isTauri(): boolean { return typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window; } // A desktop native shell (macOS/Windows/Linux), as opposed to the iOS/Android // shells. Desktop-only features (local LibreOffice/pandoc conversion, "reveal in // file manager") gate on this, not on isTauri, so they stay hidden on phones // where they are meaningless. export function isDesktopShell(): boolean { if (!isTauri()) return false; const ua = typeof navigator === 'undefined' ? '' : navigator.userAgent; return !/Mobi|Android|iPhone|iPad|iPod/i.test(ua); } // Web client build: the app served from a static host, no server anywhere // (pnpm --filter @synapse/web build:web-client). Same standalone semantics // as a native shell without a configured server. export function isWebClient(): boolean { return import.meta.env.VITE_WEB_CLIENT === '1'; } // No server in play: the workspace lives on this device alone, features that // need a server (accounts, server AI, uploads through the API) stand down. export function standalone(): boolean { return (isTauri() || isWebClient()) && !deviceServer(); } export interface DeviceServer { url: string; token: string | null; } export function deviceServer(): DeviceServer | null { if (typeof localStorage === 'undefined') return null; const url = localStorage.getItem(SERVER_URL_KEY); if (!url) return null; return { url, token: localStorage.getItem(TOKEN_KEY) }; } export function setDeviceServer(url: string, token: string | null): void { localStorage.setItem(SERVER_URL_KEY, url.replace(/\/+$/, '')); if (token) localStorage.setItem(TOKEN_KEY, token); else localStorage.removeItem(TOKEN_KEY); applyDeviceServer(); } export function clearDeviceServer(): void { localStorage.removeItem(SERVER_URL_KEY); localStorage.removeItem(TOKEN_KEY); setApiBase(''); setAuthToken(null); } // Opt-in background sync with nearby devices. Off by default, stored locally. export function autoSyncEnabled(): boolean { return typeof localStorage !== 'undefined' && localStorage.getItem(AUTOSYNC_KEY) === '1'; } export function setAutoSyncEnabled(on: boolean): void { if (typeof localStorage === 'undefined') return; if (on) localStorage.setItem(AUTOSYNC_KEY, '1'); else localStorage.removeItem(AUTOSYNC_KEY); } // Point the api client at the configured server (boot + after changes). export function applyDeviceServer(): void { const server = deviceServer(); if (!server) return; setApiBase(server.url); setAuthToken(server.token); }