diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-02 11:11:53 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-02 11:12:00 +0900 |
| commit | 7ca9941982a7d7a4c126d215770ce71ad2f7f427 (patch) | |
| tree | 0178b48094e9b7b143fd47c4d8479d3d588bb1d7 /src/client/db/study-builder.ts | |
| parent | 8f1a08fefee3a8e928baec741c830a88a4cd7200 (diff) | |
| download | kioku-7ca9941982a7d7a4c126d215770ce71ad2f7f427.tar.gz kioku-7ca9941982a7d7a4c126d215770ce71ad2f7f427.tar.zst kioku-7ca9941982a7d7a4c126d215770ce71ad2f7f427.zip | |
feat(client): read decks/cards/study from IndexedDB first
Switch deckAtom, cardsByDeckAtomFamily, noteTypesAtom, and studyDataAtom
to a stale-while-revalidate pattern: read from IndexedDB synchronously,
trigger sync in the background, and refetch on sync_complete. When local
is empty, await a single bootstrap pull before deciding there's no data.
Add study-builder to assemble StudyCards from LocalCard + Note + NoteType
+ field values, replacing the server /study endpoint dependency. The
study session can now run end-to-end offline.
Disable submit on all write modals when offline since writes still
require the server. Add a "Showing cached data" hint to the sync status
indicator. Drop cacheStudyCards (cards arrive via regular sync pull now)
and update page tests to reflect that lists no longer refresh by hitting
the GET API.
Diffstat (limited to 'src/client/db/study-builder.ts')
| -rw-r--r-- | src/client/db/study-builder.ts | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/client/db/study-builder.ts b/src/client/db/study-builder.ts new file mode 100644 index 0000000..25d1652 --- /dev/null +++ b/src/client/db/study-builder.ts @@ -0,0 +1,82 @@ +import type { CardStateType } from "./index"; +import { + localCardRepository, + localNoteFieldTypeRepository, + localNoteFieldValueRepository, + localNoteRepository, + localNoteTypeRepository, +} from "./repositories"; + +export interface StudyCardView { + id: string; + deckId: string; + noteId: string; + isReversed: boolean; + state: CardStateType; + noteType: { + frontTemplate: string; + backTemplate: string; + }; + fieldValuesMap: Record<string, string>; +} + +/** + * Build study card views for all due cards in a deck from IndexedDB. + * + * Cards whose note or note type has been soft-deleted are skipped, mirroring + * the server-side enrichment in `enrichCardsForStudy`. + */ +export async function buildStudyCards( + deckId: string, +): Promise<StudyCardView[]> { + const dueCards = await localCardRepository.findDueCards(deckId); + if (dueCards.length === 0) { + return []; + } + + const noteTypeFieldsCache = new Map<string, Map<string, string>>(); + const result: StudyCardView[] = []; + + for (const card of dueCards) { + const note = await localNoteRepository.findById(card.noteId); + if (!note || note.deletedAt !== null) continue; + + const noteType = await localNoteTypeRepository.findById(note.noteTypeId); + if (!noteType || noteType.deletedAt !== null) continue; + + let fieldTypeIdToName = noteTypeFieldsCache.get(noteType.id); + if (!fieldTypeIdToName) { + const fieldTypes = await localNoteFieldTypeRepository.findByNoteTypeId( + noteType.id, + ); + fieldTypeIdToName = new Map(fieldTypes.map((ft) => [ft.id, ft.name])); + noteTypeFieldsCache.set(noteType.id, fieldTypeIdToName); + } + + const fieldValues = await localNoteFieldValueRepository.findByNoteId( + note.id, + ); + const fieldValuesMap: Record<string, string> = {}; + for (const fv of fieldValues) { + const name = fieldTypeIdToName.get(fv.noteFieldTypeId); + if (name) { + fieldValuesMap[name] = fv.value; + } + } + + result.push({ + id: card.id, + deckId: card.deckId, + noteId: card.noteId, + isReversed: card.isReversed, + state: card.state, + noteType: { + frontTemplate: noteType.frontTemplate, + backTemplate: noteType.backTemplate, + }, + fieldValuesMap, + }); + } + + return result; +} |
