Some checks failed
E2E / Playwright e2e (push) Has been cancelled
Move mail, compose, contacts, and accounts off mocks onto REST + WS. Add client, auth store, IDB-backed query cache, offline queue, and sync bar; hybrid Zustand for UI-only state. Settings still local until backend has preferences API.
68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
"use client"
|
|
|
|
import { useState } from "react"
|
|
import { QueryClient } from "@tanstack/react-query"
|
|
import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client"
|
|
import { openDB, type IDBPDatabase } from "idb"
|
|
import type { PersistedClient, Persister } from "@tanstack/react-query-persist-client"
|
|
|
|
const DB_NAME = "ultimail-query-cache"
|
|
const STORE_NAME = "query-cache"
|
|
|
|
let dbPromise: Promise<IDBPDatabase> | null = null
|
|
|
|
function getDb() {
|
|
if (!dbPromise) {
|
|
dbPromise = openDB(DB_NAME, 1, {
|
|
upgrade(db) {
|
|
db.createObjectStore(STORE_NAME)
|
|
},
|
|
})
|
|
}
|
|
return dbPromise
|
|
}
|
|
|
|
const idbPersister: Persister = {
|
|
persistClient: async (client: PersistedClient) => {
|
|
const db = await getDb()
|
|
await db.put(STORE_NAME, client, "cache")
|
|
},
|
|
restoreClient: async (): Promise<PersistedClient | undefined> => {
|
|
const db = await getDb()
|
|
return db.get(STORE_NAME, "cache")
|
|
},
|
|
removeClient: async () => {
|
|
const db = await getDb()
|
|
await db.delete(STORE_NAME, "cache")
|
|
},
|
|
}
|
|
|
|
function makeQueryClient() {
|
|
return new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
gcTime: 1000 * 60 * 60 * 24,
|
|
staleTime: 1000 * 60 * 5,
|
|
networkMode: "offlineFirst",
|
|
retry: 3,
|
|
},
|
|
mutations: {
|
|
networkMode: "offlineFirst",
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
export function QueryProvider({ children }: { children: React.ReactNode }) {
|
|
const [queryClient] = useState(makeQueryClient)
|
|
|
|
return (
|
|
<PersistQueryClientProvider
|
|
client={queryClient}
|
|
persistOptions={{ persister: idbPersister }}
|
|
>
|
|
{children}
|
|
</PersistQueryClientProvider>
|
|
)
|
|
}
|