// Talking to the hub. The session cookie signs requests; X-Lune marks them as coming from the canvas. export type Role = 'read' | 'edit' | 'manage'; export interface Vault { id: string; name: string; can_manage: boolean; delete_from_devices: boolean; /** Notes kept in two versions (S4), from the hub's copy. */ tidy: { note: string; copy: string; label: string }[]; /** Departures needing a manager: waiting for the device, or received for review. */ handovers: { id: number; person: string; device: string; state: 'waiting' | 'received' }[]; } export type Status = 'current' | 'behind' | 'offline'; export interface Health { status: Status; last_seen: number | null; last_synced: number | null; waiting: number; held_secrets: number; holding: boolean; } export interface Device { id: string; label: string; health: Health; } export interface Person { id: string; name: string; owner: boolean; /** Invited but not joined yet. */ invited: boolean; /** Removed from the team (shown to the owner, restorable). */ removed: boolean; devices: Device[]; } export interface Member { vault: string; person: string; role: Role; } export interface CanvasData { me: { id: string; person: string; owner: boolean }; vaults: Vault[]; people: Person[]; members: Member[]; /** Removals waiting out their Undo window. */ pending: { id: number; vault: string; person: string }[]; } export interface LogEntry { at: number; actor: string; action: string; detail: string; } export interface Comparison { note: string; copy: string; label: string; note_text: string | null; copy_text: string | null; note_exists: boolean; } export class SignedOut extends Error {} async function call(method: string, path: string, body?: unknown): Promise { const res = await fetch(path, { method, credentials: 'same-origin', headers: { 'X-Lune': '1', ...(body ? { 'Content-Type': 'application/json' } : {}) }, body: body ? JSON.stringify(body) : undefined, }); const data = await res.json().catch(() => ({})); if (res.status === 401) throw new SignedOut(data.error ?? 'please sign in'); if (!res.ok) throw new Error(data.error ?? 'something went wrong on the hub'); return data as T; } export const api = { canvas: () => call('GET', '/api/canvas'), log: (count = 8) => call('GET', `/api/log?count=${count}`), signin: (code: string) => call('POST', '/api/web/signin', { code }), signout: () => call('POST', '/api/web/signout'), grant: (person: string, vault: string, role: Role) => call('POST', '/api/grants', { person, vault, role }), inviteGrant: (person: string, vault: string, role: Role | null, remove = false) => call('POST', '/api/invites/grant', { person, vault, role, remove }), remove: (person: string, vault: string, delaySeconds: number) => call<{ pending?: number }>('POST', '/api/remove', { person, vault, delay_seconds: delaySeconds }), undoRemove: (pending: number) => call('POST', '/api/remove/undo', { pending }), invite: (person: string, grants: { vault: string; role: Role }[]) => call<{ link: string }>('POST', '/api/invites', { person, grants }), createVault: (name: string) => call('POST', '/api/vaults', { name }), vaultSetting: (vault: string, deleteFromDevices: boolean) => call('POST', '/api/vaults/settings', { vault, delete_from_devices: deleteFromDevices }), renameDevice: (deviceId: string, label: string) => call('POST', '/api/devices/rename', { device_id: deviceId, label }), nudge: (deviceId: string) => call('POST', '/api/nudge', { device_id: deviceId }), compare: (vault: string, copy: string) => call('POST', '/api/tidy/compare', { vault, copy }), resolve: (vault: string, copy: string, keep: 'note' | 'copy' | 'both') => call<{ message: string }>('POST', '/api/tidy/resolve', { vault, copy, keep }), handoverShow: (id: number) => call('POST', '/api/handovers/show', { id }), handoverDecide: (id: number, decision: 'keep' | 'discard' | 'skip') => call<{ message: string }>('POST', '/api/handovers/decide', { id, decision }), teamRemove: (person: string) => call('POST', '/api/team/remove', { person }), teamRestore: (person: string) => call<{ link: string }>('POST', '/api/team/restore', { person }), removeDevice: (deviceId: string) => call('POST', '/api/device/remove', { device_id: deviceId }), };