Devices report freshness per vault every 30 s (counts only); the canvas shows current / behind / offline, held secrets in red, and a Nudge that reaches the device at its next check-in. Notes kept in two versions can be compared side by side and tidied on the hub's copy. Handovers are reviewed in the vault panel; people can be removed from the team and restored with a one-time link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.4 KiB
TypeScript
118 lines
4.4 KiB
TypeScript
// 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<T>(method: string, path: string, body?: unknown): Promise<T> {
|
|
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<CanvasData>('GET', '/api/canvas'),
|
|
log: (count = 8) => call<LogEntry[]>('GET', `/api/log?count=${count}`),
|
|
signin: (code: string) => call<object>('POST', '/api/web/signin', { code }),
|
|
signout: () => call<object>('POST', '/api/web/signout'),
|
|
grant: (person: string, vault: string, role: Role) => call<object>('POST', '/api/grants', { person, vault, role }),
|
|
inviteGrant: (person: string, vault: string, role: Role | null, remove = false) =>
|
|
call<object>('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<object>('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<object>('POST', '/api/vaults', { name }),
|
|
vaultSetting: (vault: string, deleteFromDevices: boolean) =>
|
|
call<object>('POST', '/api/vaults/settings', { vault, delete_from_devices: deleteFromDevices }),
|
|
renameDevice: (deviceId: string, label: string) => call<object>('POST', '/api/devices/rename', { device_id: deviceId, label }),
|
|
nudge: (deviceId: string) => call<object>('POST', '/api/nudge', { device_id: deviceId }),
|
|
compare: (vault: string, copy: string) => call<Comparison>('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<string[]>('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<object>('POST', '/api/team/remove', { person }),
|
|
teamRestore: (person: string) => call<{ link: string }>('POST', '/api/team/restore', { person }),
|
|
removeDevice: (deviceId: string) => call<object>('POST', '/api/device/remove', { device_id: deviceId }),
|
|
};
|