import { del, get, keys, set } from 'idb-keyval' export interface Draft { id: string title: string markdown: string sourceUri: string topics?: string[] createdAt: string } /** Drafts belong to the account that saved them. */ function prefix(owner: string): string { return `threadbare:draft:${owner}/` } export async function listDrafts(owner: string): Promise { const stored = await keys() const own = prefix(owner) const drafts: Draft[] = [] for (const key of stored) { if (typeof key !== 'string' || !key.startsWith(own)) continue const draft = await get(key) if (draft) drafts.push(draft) } return drafts.sort((left, right) => right.createdAt.localeCompare(left.createdAt)) } export async function loadDraft(owner: string, id: string): Promise { return get(prefix(owner) + id) } export async function saveDraft(owner: string, draft: Draft): Promise { await set(prefix(owner) + draft.id, draft) } export async function deleteDraft(owner: string, id: string): Promise { await del(prefix(owner) + id) }