switch from next to vue

This commit is contained in:
2026-02-06 01:11:09 +01:00
parent 1193ca3552
commit 43463bbe8f
71 changed files with 5380 additions and 5164 deletions
+48
View File
@@ -0,0 +1,48 @@
import type { Database } from 'sql.js'
import type { Entry } from './types'
import { getMeta, persistDb, setMeta, upsertEntries } from './db'
const LAST_SYNC_KEY = 'last_sync_at'
export async function syncEntries(db: Database) {
const lastSyncAt = getMeta(db, LAST_SYNC_KEY)
const url = new URL('https://sanasto.rin.no/api/entries')
if (lastSyncAt) {
url.searchParams.set('since', lastSyncAt)
}
const response = await fetch(url.toString(), {
headers: {
'X-Sanasto-App': 'app.sanasto',
},
})
if (!response.ok) {
throw new Error(`Sync failed with ${response.status}`)
}
const entries = (await response.json()) as Entry[]
if (entries.length === 0) {
return { updated: 0, lastSyncAt }
}
upsertEntries(db, entries)
const newest = entries.reduce<Date | null>((current, entry) => {
if (!entry.updated_at) {
return current
}
const date = new Date(entry.updated_at)
if (!current || date > current) {
return date
}
return current
}, lastSyncAt ? new Date(lastSyncAt) : null)
if (newest) {
setMeta(db, LAST_SYNC_KEY, newest.toISOString())
}
await persistDb(db)
return { updated: entries.length, lastSyncAt: newest?.toISOString() ?? lastSyncAt }
}