ultisuite-client/lib/api/query-provider.tsx
R3D347HR4Y 6ec95262af Add OnlyOffice integration and update project configurations
- Updated .env.example to include configuration for OnlyOffice Document Server.
- Modified the workspace configuration to remove the drive-suite path.
- Adjusted TypeScript environment imports for consistency.
- Enhanced Next.js configuration to disable canvas in Webpack.
- Updated package.json to include new dependencies for OnlyOffice and PDF.js.
- Added global styles for OnlyOffice theme integration in the CSS.
- Created new layout and page components for the Drive feature, including public sharing and editing functionalities.
- Updated metadata handling across various layouts to reflect the new app structure.
2026-06-07 15:49:21 +02:00

98 lines
2.4 KiB
TypeScript

"use client"
import { useState } from "react"
import { QueryClient } from "@tanstack/react-query"
import {
PersistQueryClientProvider,
type PersistedClient,
} from "@tanstack/react-query-persist-client"
import { openDB, type IDBPDatabase } from "idb"
import type { Persister } from "@tanstack/react-query-persist-client"
import {
isPreviewThumbQueryKey,
revokePreviewBlobData,
} from "@/lib/api/preview-blob-url"
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()
const restored = await db.get<PersistedClient>(STORE_NAME, "cache")
if (!restored?.clientState?.queries) return restored
restored.clientState.queries = restored.clientState.queries.filter(
(entry) => !isPreviewThumbQueryKey(entry.queryKey)
)
return restored
},
removeClient: async () => {
const db = await getDb()
await db.delete(STORE_NAME, "cache")
},
}
function attachPreviewBlobGc(queryClient: QueryClient) {
return queryClient.getQueryCache().subscribe((event) => {
if (event.type === "removed") {
revokePreviewBlobData(event.query.state.data)
}
})
}
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(() => {
const client = makeQueryClient()
attachPreviewBlobGc(client)
return client
})
return (
<PersistQueryClientProvider
client={queryClient}
persistOptions={{
persister: idbPersister,
dehydrateOptions: {
shouldDehydrateQuery: (query) =>
query.state.status === "success" && !isPreviewThumbQueryKey(query.queryKey),
},
}}
>
{children}
</PersistQueryClientProvider>
)
}