feat: PWA offline capture — IndexedDB queue, service worker cache, auto-sync on reconnect

This commit is contained in:
2026-07-11 19:07:35 +08:00
parent 8c88118b15
commit c048878600
7 changed files with 251 additions and 4 deletions
+33 -4
View File
@@ -1,7 +1,7 @@
// Stub — full implementation in Task 5 (IndexedDB offline queue)
// This file exists so TypeScript can resolve the dynamic import in report-form.tsx.
import { openDB, type IDBPDatabase } from 'idb'
export interface PendingReport {
id?: number
zone_token: string
incident_type: string
description: string
@@ -11,6 +11,35 @@ export interface PendingReport {
created_at: string
}
export async function addPendingReport(_report: PendingReport): Promise<void> {
throw new Error('IndexedDB offline store not yet implemented — Task 5 will replace this stub')
let dbPromise: Promise<IDBPDatabase> | null = null
function getDb(): Promise<IDBPDatabase> {
if (!dbPromise) {
dbPromise = openDB('ims-offline', 1, {
upgrade(db) {
db.createObjectStore('pending_reports', { keyPath: 'id', autoIncrement: true })
},
})
}
return dbPromise
}
export async function addPendingReport(report: Omit<PendingReport, 'id'>): Promise<number> {
const db = await getDb()
return db.add('pending_reports', report) as Promise<number>
}
export async function getPendingReports(): Promise<PendingReport[]> {
const db = await getDb()
return db.getAll('pending_reports')
}
export async function removePendingReport(id: number): Promise<void> {
const db = await getDb()
return db.delete('pending_reports', id)
}
export async function getPendingCount(): Promise<number> {
const db = await getDb()
return db.count('pending_reports')
}