// The webview's only way out: the app commands of src-tauri/src/commands.rs. No plugin API is used from here. // Dev builds can run on fixture data instead (VITE_FIXTURES=1, src/dev/fixtures.ts): import.meta.env.DEV is false in // release builds, so that branch and the fixture module are dropped from them (tests/webview.test.ts checks dist/). import { invoke as tauriInvoke } from '@tauri-apps/api/core'; import { listen as tauriListen, type UnlistenFn } from '@tauri-apps/api/event'; import type { DraftsResult, IconResult, ListingsResult, MethodDescriptor, PortfolioResult, ProgressParams, ToolResult } from '../../../cli/lib/desktop-protocol'; export type { AppIdentity, AppStatus, CatalogParity, DraftsResult, IconResult, ListingField, ListingLocale, ListingsResult, MethodDescriptor, PortfolioResult, ProgressParams, ProjectSummary, ReleasePhase, ReleaseTrain, ReplyDraft, 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[]; 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 } export interface CacheEntry { value: T; at: string } export interface DownloadInfo { tool: 'ffmpeg' | 'chrome'; available: boolean; version: string | null; size: number | null; license: string; licenseUrl: string; source: string; installed: boolean; folder: string } export interface DownloadEvent { job: string; tool: string; stage: 'resolve' | 'download' | 'verify' | 'unpack' | 'install' | 'done' | 'error' | 'cancelled'; received: number; total: number | null; message?: string } export type { IconResult as Icon }; /** 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'; } /** Dev-only fixture mode (VITE_FIXTURES=1): every command answered from src/dev/fixtures.ts. */ export const FIXTURES: boolean = import.meta.env.DEV && !!import.meta.env.VITE_FIXTURES; async function invoke(cmd: string, args?: Record): Promise { if (import.meta.env.DEV && import.meta.env.VITE_FIXTURES) return (await import('./dev/fixtures')).mockInvoke(cmd, args ?? {}); return tauriInvoke(cmd, args); } async function listen(event: string, cb: (payload: T) => void): Promise { if (import.meta.env.DEV && import.meta.env.VITE_FIXTURES) return (await import('./dev/fixtures')).mockListen(event, cb as (p: unknown) => void); return tauriListen(event, e => cb(e.payload)); } let jobSeq = 0; /** A fresh job id for one background run (progress events and cancel refer to it). */ export const newJob = (kind: string): string => `${kind.replace(/[^A-Za-z0-9_.-]/g, '-').slice(0, 40)}-${Date.now().toString(36)}-${++jobSeq}`; export const api = { appInfo: () => invoke('app_info'), sidecarStatus: () => invoke('sidecar_status'), /** Reads, or dry runs of writes (the host forces dryRun for writes coming from here). */ call: (method: string, projectId?: string, params?: { args?: Record }, job?: string) => invoke('sidecar_call', { method, projectId: projectId ?? null, params: params ?? null, job: job ?? null }), /** Last cached read (projectId "*" = the whole live portfolio); null when never read. */ cached: (method: string, projectId: string, args: Record = {}) => invoke | null>('cache_get', { method, projectId, args }), clearCache: () => invoke('cache_clear'), cancel: (job: string) => invoke('job_cancel', { job }), /** 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 = {}, job?: string) => invoke('write_with_confirm', { method, projectId, args, job: job ?? null }), projects: () => invoke('projects_list'), addProject: () => invoke('project_add'), initProject: (profile: 'app' | 'game' | 'subscription-app') => invoke('project_init', { profile }), removeProject: (id: string) => invoke('project_remove', { id }), setProjectAccounts: (id: string, play: string | null, apple: string | null) => invoke('project_set_accounts', { id, play, apple }), accounts: () => invoke('accounts_list'), addPlayAccount: (label: string) => invoke('account_add_play', { label }), addAppleAccount: (label: string, keyId: string, issuerId: string) => invoke('account_add_apple', { label, keyId, issuerId }), removeAccount: (id: string) => invoke('account_remove', { id }), portfolio: (live = false, job?: string) => invoke('portfolio_status', { live, job: job ?? null }), openExternal: (url: string) => invoke('open_external', { url }), toolsInfo: () => invoke('tools_info'), installTool: (tool: 'ffmpeg' | 'chrome', job: string) => invoke('tools_install', { tool, job }), uninstallTool: (tool: 'ffmpeg' | 'chrome') => invoke('tools_uninstall', { tool }), }; /** Typed reads of the project files (offline) and the stores (network). */ export const reads = { icon: (projectId: string) => api.call('project.icon', projectId), listings: (projectId: string) => api.call('project.listings', projectId), drafts: (projectId: string) => api.call('reviews.drafts', projectId), }; export interface ProgressEvent extends ProgressParams { job?: string } export const onProgress = (cb: (p: ProgressEvent) => void): Promise => listen<{ params: ProgressParams; job?: string }>('sidecar-progress', e => cb({ ...e.params, job: e.job })); export const onDownload = (cb: (e: DownloadEvent) => void): Promise => listen('tool-download', cb); export const onDeepLink = (cb: (screen: string) => void): Promise => listen('deep-link', cb);