storebound/apps/desktop/src/api.ts
CRT 61a1e832a0 Desktop app shell (apps/desktop): Tauri 2 + React 19 around the storebound sidecar
- Rust host: spawns storebound serve --stdio with fixed args, hands over a per-session secret,
  mints confirm tokens only after a native dialog showing the summary and the dry run
- every app command re-checks window label and page origin; one capability (main window, app
  commands + event listen only, no plugin API in the webview); strict CSP; tests/security.rs pins it
- store keys picked in native dialogs, kept in the OS keychain, passed per call in memory only
- plugins: shell (Rust only), single-instance, deep-link storebound://, window-state, opener,
  dialog, notification, updater (placeholder endpoint and key until signing), process
- app-centric UI: Portfolio, App (Releases, Catalog, Listings, Health, Reviews, TestFlight),
  App folders, Accounts, Dependencies, Settings; light / dark with the storebound.dev tokens,
  reduced motion, home folder shown as ~
- macOS menu: Cmd+Q quits, Edit menu, Window menu keeps the main window; Linux DMABUF fix
- Bun workspace (npm files unchanged), ci runs desktop typecheck, build, cargo test and clippy;
  scrub covers home paths; docs/desktop.md (protocol, safety, dependency sources and licenses)
2026-10-11 01:57:33 +08:00

53 lines
3.7 KiB
TypeScript

// The webview's only way out: the app commands of src-tauri/src/commands.rs. No plugin API is used from here.
import { invoke } from '@tauri-apps/api/core';
import { listen, type UnlistenFn } from '@tauri-apps/api/event';
import type { MethodDescriptor, PortfolioResult, ProgressParams, ToolResult } from '../../../cli/lib/desktop-protocol';
export type {
AppIdentity, AppStatus, CatalogParity, MethodDescriptor, PortfolioResult, ProgressParams, ReleaseTrain, StoreRelease, ToolResult,
} from '../../../cli/lib/desktop-protocol';
export interface AppInfo { version: string; identifier: string; platform: string; debug: boolean; home: string | null }
export type { ToolStatus } from '../../../cli/lib/tools';
export interface LocalTools { tools: import('../../../cli/lib/tools').ToolStatus[] }
export interface SidecarStatus { running: boolean; version: string; protocol: string; methods: Pick<MethodDescriptor, 'method' | 'title' | 'description' | 'kind' | 'network' | 'destructive' | 'json'>[]; tools: LocalTools | null }
/** An app folder (one storebound.config.ts = one app on iOS and / or Android) and the accounts it uses. */
export interface Project { id: string; path: string; name: string; playAccount: string | null; appleAccount: string | null; added: string }
export type AccountKind = 'play' | 'apple';
export interface Account { id: string; label: string; kind: AccountKind; created: string }
export interface WriteOutcome { confirmed: boolean; stage: 'dry-run' | 'dialog' | 'done'; dryRun: ToolResult; result?: ToolResult }
/** What a failed command rejects with (sidecar.rs `Fail`). */
export interface Fail { code: number; message: string; data?: unknown }
export function errorText(e: unknown): string {
if (typeof e === 'string') return e;
if (e && typeof e === 'object' && 'message' in e && typeof (e as Fail).message === 'string') return (e as Fail).message;
return 'Something went wrong';
}
export const api = {
appInfo: () => invoke<AppInfo>('app_info'),
sidecarStatus: () => invoke<SidecarStatus>('sidecar_status'),
/** Reads, or dry runs of writes (the host forces dryRun for writes coming from here). */
call: <T = ToolResult>(method: string, projectId?: string, params?: { args?: Record<string, unknown> }) =>
invoke<T>('sidecar_call', { method, projectId: projectId ?? null, params: params ?? null }),
/** Dry run, native confirm dialog with that dry run, then the real run with a host-minted token. */
writeWithConfirm: (method: string, projectId: string, args: Record<string, unknown> = {}) =>
invoke<WriteOutcome>('write_with_confirm', { method, projectId, args }),
projects: () => invoke<Project[]>('projects_list'),
addProject: () => invoke<Project | null>('project_add'),
removeProject: (id: string) => invoke<void>('project_remove', { id }),
setProjectAccounts: (id: string, play: string | null, apple: string | null) => invoke<Project>('project_set_accounts', { id, play, apple }),
accounts: () => invoke<Account[]>('accounts_list'),
addPlayAccount: (label: string) => invoke<Account | null>('account_add_play', { label }),
addAppleAccount: (label: string, keyId: string, issuerId: string) => invoke<Account | null>('account_add_apple', { label, keyId, issuerId }),
removeAccount: (id: string) => invoke<void>('account_remove', { id }),
portfolio: (live = false) => invoke<PortfolioResult>('portfolio_status', { live }),
openExternal: (url: string) => invoke<void>('open_external', { url }),
};
export const onProgress = (cb: (p: ProgressParams) => void): Promise<UnlistenFn> =>
listen<{ params: ProgressParams }>('sidecar-progress', e => cb(e.payload.params));
export const onDeepLink = (cb: (screen: string) => void): Promise<UnlistenFn> => listen<string>('deep-link', e => cb(e.payload));