// What the sidecar tells the page about the cloud, and small helpers over it. export type DocJson = { id: string; name: string; parent: string; type: string; isFolder: boolean; pinned: boolean; deleted: boolean; trashed: boolean; lastModified: number | null; hash: string; size: number; path: string; fileType?: string; pageCount?: number; cover?: string | null; }; export type PageInfo = { id: string; template: string | null; redir: number | null; modified: number | null; hasRm: boolean; hasThumbnail: boolean }; export type DocInfo = { doc: DocJson; fileType: string; content: Record; pages: PageInfo[]; authors: { index: number; uuid: string }[]; author: { uuid: string; index: number }; files: string[]; }; export type PenKind = 'fineliner' | 'ballpoint' | 'marker' | 'pencil' | 'mechanical' | 'brush' | 'calligraphy' | 'highlighter' | 'shader'; export type Thickness = 1 | 2 | 3; export type PenPrefs = { kind: PenKind; color: number; thickness: Thickness }; export type Settings = { authorUuid: string; syncDir?: string; syncFormats?: string[]; syncRaw?: boolean; syncTrash?: boolean; syncPrune?: boolean; view?: 'grid' | 'list'; pen?: PenPrefs; }; export type User = { email: string | null; name: string | null; userId: string | null; deviceDesc: string | null; deviceId: string | null; scopes: string[]; expires: string | null }; export type Status = { loggedIn: boolean; user: User | null; hosts: { authHost: string; syncHost: string }; configPath: string; settings: Settings; error?: string }; export type SyncEvent = { type: 'download' | 'export' | 'move' | 'prune' | 'skip'; doc?: { id: string; name: string }; path?: string; from?: string; to?: string; format?: string; files?: string[]; reason?: string; name?: string }; export type SyncSummary = { downloaded: number; exported: number; moved: number; pruned: number; unchanged: number; skipped: number }; export const ROOT = ''; export const TRASH = 'trash'; export const byName = (a: DocJson, b: DocJson) => Number(b.isFolder) - Number(a.isFolder) || a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' }); /** The live documents directly in a folder (the root when `parent` is ''), folders first. */ export function childrenOf(docs: DocJson[], parent: string): DocJson[] { return docs.filter((d) => !d.deleted && d.parent === parent).sort(byName); } /** The folder and its ancestors, root first (the root itself is not a document). */ export function ancestorsOf(docs: DocJson[], folderId: string): DocJson[] { const byId = new Map(docs.map((d) => [d.id, d])); const out: DocJson[] = []; let cur = byId.get(folderId); const seen = new Set(); while (cur && !seen.has(cur.id)) { seen.add(cur.id); out.unshift(cur); cur = byId.get(cur.parent); } return out; } /** Every live document under a folder, at any depth. */ export function descendantsOf(docs: DocJson[], folderId: string): DocJson[] { const out: DocJson[] = []; const walk = (parent: string) => { for (const d of childrenOf(docs, parent)) { out.push(d); if (d.isFolder) walk(d.id); } }; walk(folderId); return out; } export function isPdf(doc: DocJson | { fileType?: string }): boolean { return doc.fileType === 'pdf'; } export function kindLabel(doc: DocJson): string { if (doc.isFolder) return 'Folder'; if (doc.fileType === 'pdf') return 'PDF'; if (doc.fileType === 'epub') return 'EPUB'; return 'Notebook'; } export function kindIcon(doc: DocJson): string { if (doc.isFolder) return 'icon-folder'; if (doc.fileType === 'pdf') return 'icon-file-text'; if (doc.fileType === 'epub') return 'icon-book'; return 'icon-pencil'; } export function fmtDate(ms: number | null | undefined): string { if (!ms) return ''; const d = new Date(ms); const now = new Date(); const sameDay = d.toDateString() === now.toDateString(); if (sameDay) return d.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' }); return d.toLocaleDateString(undefined, { year: d.getFullYear() === now.getFullYear() ? undefined : 'numeric', month: 'short', day: 'numeric' }); } export function fmtSize(bytes: number | null | undefined): string { if (!bytes) return ''; if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } export const msg = (e: unknown): string => (e instanceof Error ? e.message : String(e));