// Shared plumbing for the workspace repos: the Loro container layout, entity // map helpers, and the ResultAsync wrapper that keeps repo signatures // compatible with the api client the pages were written against. // // Container layout of the "workspace" doc: // Map courses course_id -> LoroMap { code, name, exam_date, created_at } // MovableList course_order [course_id] // Map topics topic_id -> LoroMap { course_id, name } // Map documents doc_id -> LoroMap { course_id, topic_id, original_name, // page_count, uploaded_at, has_preview } // Map links link_id -> LoroMap { course_id, topic_id, title, url, created_at } // Map layout course_id -> LoroMap { topic_order: MovableList [topic_id], // items:: MovableList ["doc:x"|"link:y"] } // Map exercises ex_id -> LoroMap { course_id, topic_id, document_id, type, // question, options, correct_answer, // explanation, difficulty, source, created_at } // Map lexicon entry_id -> LoroMap { term, definition_md, created_at, updated_at, // courses: Map course_id -> true, // links: Map link_id -> {document_id, page} } import type { LoroDoc } from 'loro-crdt'; import { ResultAsync } from 'neverthrow'; import { annDocId, openDoc, persistNow, WORKSPACE_DOC, ATTEMPTS_DOC } from './loro'; import { ensureBootstrapped } from './bootstrap'; import { previewingNow } from './previewGuard'; export * from './containers'; export class DataError extends Error { status: number; constructor(message: string, status = 0) { super(message); this.status = status; } } export function run(fn: () => Promise): ResultAsync { return ResultAsync.fromPromise(fn(), (e) => e instanceof DataError ? e : new DataError((e as Error)?.message ?? 'Local data error'), ); } // Repos read through these accessors, so the one-time bootstrap import always // ran before the first read. export async function ws(): Promise { await ensureBootstrapped(); return openDoc(WORKSPACE_DOC); } export async function attemptsDoc(): Promise { await ensureBootstrapped(); return openDoc(ATTEMPTS_DOC); } export async function annotationsDoc(documentId: string): Promise { await ensureBootstrapped(); return openDoc(annDocId(documentId)); } export const nowIso = (): string => new Date().toISOString(); // Commit AND persist before resolving: a write a repo reported as done must // survive an immediate reload, so the debounced background flush is not // enough for these cold-path docs. // // `change` is a compact, language-neutral tag describing the mutation (e.g. // "annotation.add:highlight"), stored as the Loro commit message so the history // view can render "added a highlight". It is a tag, not prose: translating it // happens at display time (see history.ts / describeChange), never in the CRDT. export async function commit( doc: LoroDoc, docId: string = WORKSPACE_DOC, change?: string, ): Promise { // While previewing a past revision the docs are checked out (detached); // committing would fork history off that old point. Refuse so the read-only // preview cannot be written to (the UI also disables the controls). if (previewingNow()) throw new DataError('Editing is disabled while previewing a past version'); doc.commit(change ? { message: change } : undefined); await persistNow(docId); }