Something went wrong. Try again.
AI slop app to read Korean with a pop-up dictionary
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import type { Book, DeckSettings, MinedCard, ReviewLog, WordUnderstandingRecord,} from '../types'import { createSyncedBook, deleteBook, ensureBookContentHash, listBooks } from './books'import { deleteMinedCard, getDeckSettings, listMinedCards, listReviewLogs,} from './cards'import { bookSyncKey, type RemoteBookRecord } from './remoteSyncRecords'import { BOOKS_STORE, CARDS_STORE, REVIEW_LOGS_STORE, SETTINGS_STORE, withStore,} from './storage'import { applySyncedWordUnderstanding, listWordUnderstandingRecords,} from './wordUnderstanding'
export interface LocalSyncSnapshot { books: Book[] cards: MinedCard[] reviews: ReviewLog[] deckSettings: DeckSettings wordLevels: WordUnderstandingRecord[]}
export async function getLocalSyncSnapshot(): Promise<LocalSyncSnapshot> { const [rawBooks, cards, reviews, deckSettings, wordLevels] = await Promise.all([ listBooks(), listMinedCards(), listReviewLogs(), getDeckSettings(), listWordUnderstandingRecords(), ]) const books: Book[] = [] for (const book of rawBooks) { try { books.push(await ensureBookContentHash(book)) } catch { books.push(book) } } return { books, cards, reviews, deckSettings, wordLevels }}
export { applySyncedWordUnderstanding }
export async function applySyncedCard(card: MinedCard): Promise<boolean> { const current = await withStore<MinedCard | undefined>( CARDS_STORE, 'readonly', (store) => store.get(card.id), ) if (current && current.updatedAt >= card.updatedAt) return false await withStore(CARDS_STORE, 'readwrite', (store) => store.put(card)) return true}
export async function applySyncedReview(log: ReviewLog): Promise<boolean> { const current = await withStore<ReviewLog | undefined>( REVIEW_LOGS_STORE, 'readonly', (store) => store.get(log.id), ) if (current) return false await withStore(REVIEW_LOGS_STORE, 'readwrite', (store) => store.put(log)) return true}
export async function applySyncedDeckSettings(settings: DeckSettings): Promise<boolean> { const current = await getDeckSettings() if (current.updatedAt >= settings.updatedAt) return false await withStore(SETTINGS_STORE, 'readwrite', (store) => store.put(settings)) return true}
export async function applySyncedBookRecord(record: RemoteBookRecord): Promise<boolean> { const books = await listBooks() const match = books.find((book) => ( book.id === record.localBookId || bookSyncKey(book) === record.syncKey )) if (!match) return false const remoteTitleUpdatedAt = record.titleUpdatedAt ?? record.createdAt const localTitleUpdatedAt = match.titleUpdatedAt ?? match.createdAt const useRemoteTitle = remoteTitleUpdatedAt > localTitleUpdatedAt const useRemoteProgress = record.progress.updatedAt > match.progress.updatedAt if (!useRemoteTitle && !useRemoteProgress) return false await withStore(BOOKS_STORE, 'readwrite', (store) => store.put({ ...match, title: useRemoteTitle ? record.title : match.title, titleUpdatedAt: useRemoteTitle ? remoteTitleUpdatedAt : match.titleUpdatedAt, progress: useRemoteProgress ? record.progress : match.progress, updatedAt: Math.max(match.updatedAt, record.updatedAt), })) return true}
export async function applySyncedBookContent( record: RemoteBookRecord, content: string,): Promise<boolean> { const books = await listBooks() if (books.some((book) => bookSyncKey(book) === record.syncKey)) return false await createSyncedBook(record, content) return true}
export async function deleteSyncedCard(id: string): Promise<boolean> { const existing = await withStore<MinedCard | undefined>( CARDS_STORE, 'readonly', (store) => store.get(id), ) if (!existing) return false await deleteMinedCard(id, { sync: false }) return true}
export async function deleteSyncedBook(syncKey: string): Promise<boolean> { const books = await listBooks() const match = books.find((book) => bookSyncKey(book) === syncKey) if (!match) return false await deleteBook(match.id, { sync: false }) return true}