// Earlier versions of a single note (Observe, Interpret, Apply…), kept on this device so one note can be rolled // back without restoring the whole study. A version is saved when you start and when you stop editing a note, so // whatever was there before a big rewrite is always a click away. export const MAX_VERSIONS = 12; const keyFor = (chunkId, field) => `note-history:${chunkId}:${field}`; // -> [{ at, text }], newest first export function listNoteVersions(chunkId, field) { try { const raw = JSON.parse(localStorage.getItem(keyFor(chunkId, field)) || '[]'); return Array.isArray(raw) ? raw.filter((v) => v && typeof v.text === 'string') : []; } catch { return []; } } // Keeps `text` as the newest version unless it is blank or the same as the newest one. Returns the list. export function recordNoteVersion(chunkId, field, text, now = Date.now()) { const list = listNoteVersions(chunkId, field); const value = text ?? ''; if (!value.trim() || list[0]?.text === value) return list; const next = [{ at: now, text: value }, ...list].slice(0, MAX_VERSIONS); try { localStorage.setItem(keyFor(chunkId, field), JSON.stringify(next)); } catch { /* storage full or unavailable */ } return next; }