From 6113e6d784844f0a812016afe7818ae2cde8896f Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 19 Apr 2026 16:06:19 +0100 Subject: [PATCH] refactor(viewer): drop kon_history localStorage; route edits through update_transcript --- src/lib/stores/page.svelte.js | 6 +++--- src/routes/viewer/+page.svelte | 38 +++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/lib/stores/page.svelte.js b/src/lib/stores/page.svelte.js index 34a1bb4..0ef0db5 100644 --- a/src/lib/stores/page.svelte.js +++ b/src/lib/stores/page.svelte.js @@ -83,9 +83,9 @@ export function saveProfiles() { // array is seeded empty on module init and hydrated from SQLite on boot // in the desktop runtime. Browser preview (no Tauri) shows an empty list. // -// The previous `kon_history` localStorage read-cache was removed to kill -// the cold-start flicker where the UI painted stale localStorage data and -// then overwrote it from SQLite a tick later. +// The previous localStorage history read-cache was removed to kill the +// cold-start flicker where the UI painted stale cached data and then +// overwrote it from SQLite a tick later. import { invoke } from "@tauri-apps/api/core"; import { hasTauriRuntime } from "$lib/utils/runtime.js"; diff --git a/src/routes/viewer/+page.svelte b/src/routes/viewer/+page.svelte index afa3239..b3cfb16 100644 --- a/src/routes/viewer/+page.svelte +++ b/src/routes/viewer/+page.svelte @@ -200,8 +200,11 @@ function toggleStar(idx) { if (item?.segments) { + // TODO: Phase 2 remaining Task 1.5 — starred is not a SQLite column + // on the transcripts table yet, so this mutation is in-memory only + // and will not survive a reload. Task 1.5 will extend update_transcript + // (and the transcripts schema) to carry starred segment state. item.segments[idx].starred = !item.segments[idx].starred; - saveItemToHistory(); } } @@ -214,21 +217,32 @@ } } + // Persist edits back to SQLite via update_transcript (the canonical store). + // Only `text` and `title` are accepted by update_transcript today. Segment + // structure, segment-level starred flags, and manualTags are not SQLite + // columns yet — Phase 2 remaining Task 1.5 will extend the command and the + // transcripts schema to cover those. Until then, segment-level edits that + // change `item.text` (the joined full transcript) survive reload; the + // segment array itself does not. + // + // The cross-window `kon_viewer_item` localStorage payload stays in sync so + // a second viewer window hydrating from it reflects the latest edit. function saveItemToHistory() { - // Persist edits back to localStorage history + if (!item) return; + // Update the cross-window payload only. The old localStorage history + // cache was removed as part of the SQLite-canonical refactor. try { - const raw = localStorage.getItem("kon_history"); - if (raw) { - const hist = JSON.parse(raw); - const idx = hist.findIndex((h) => h.id === item.id); - if (idx >= 0) { - hist[idx] = { ...hist[idx], segments: item.segments, text: item.text }; - localStorage.setItem("kon_history", JSON.stringify(hist)); - } - } - // Also update the viewer item cache localStorage.setItem("kon_viewer_item", JSON.stringify(item)); } catch {} + // Best-effort SQLite persistence. Silently ignored in browser preview + // (no Tauri runtime) — matches the main-window store's behaviour. + invoke("update_transcript", { + id: String(item.id), + text: item.text ?? null, + title: item.title ?? null, + }).catch((err) => { + console.warn("viewer saveItemToHistory: update_transcript failed", err); + }); } function scheduleTextSave() {