feat: bump client-services (accumulated feature work + deploy fixes)
Snapshot of in-progress work across local_backend, manager_dashboard, and waiter_pwa (pricing, chat, fiscal, prep zones, recovery codes, CRM, inventory, permissions), plus the nginx/docker-compose deploy fixes for the Unraid + NPM reverse-proxy setup. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useEffect } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import client from '../api/client'
|
||||
import db from '../db/posdb'
|
||||
|
||||
export const PRODUCTS_KEY = ['products']
|
||||
export const CATEGORIES_KEY = ['categories']
|
||||
|
||||
async function fetchAndCacheProducts() {
|
||||
const [prodRes, catRes] = await Promise.all([
|
||||
@@ -14,7 +13,6 @@ async function fetchAndCacheProducts() {
|
||||
const products = prodRes.data
|
||||
const categories = catRes.data
|
||||
|
||||
// Write to IndexedDB in the background — don't await so UI isn't blocked
|
||||
db.products.bulkPut(products).catch(() => {})
|
||||
db.categories.bulkPut(categories).catch(() => {})
|
||||
|
||||
@@ -32,38 +30,43 @@ async function loadFromCache() {
|
||||
|
||||
export function useProductCache() {
|
||||
const queryClient = useQueryClient()
|
||||
const [idbData, setIdbData] = useState(null)
|
||||
const [idbLoaded, setIdbLoaded] = useState(false)
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: PRODUCTS_KEY,
|
||||
queryFn: fetchAndCacheProducts,
|
||||
// Serve stale data instantly — products don't change every second
|
||||
staleTime: 5 * 60 * 1000, // 5 min before background re-fetch
|
||||
gcTime: 60 * 60 * 1000, // keep in memory for 1 hour
|
||||
placeholderData: undefined,
|
||||
})
|
||||
|
||||
// On mount, if the query has no data yet, seed it from IndexedDB immediately
|
||||
// so the UI renders without waiting for the network round-trip
|
||||
// Load IDB data immediately on mount — this is the offline fallback
|
||||
useEffect(() => {
|
||||
const cached = queryClient.getQueryData(PRODUCTS_KEY)
|
||||
if (cached) return
|
||||
|
||||
loadFromCache().then(idbData => {
|
||||
if (idbData) {
|
||||
// Set as placeholder — React Query will still fetch in background
|
||||
queryClient.setQueryData(PRODUCTS_KEY, idbData)
|
||||
loadFromCache().then(data => {
|
||||
setIdbData(data)
|
||||
setIdbLoaded(true)
|
||||
// Seed the query cache so React Query uses it as starting data
|
||||
if (data && !queryClient.getQueryData(PRODUCTS_KEY)) {
|
||||
queryClient.setQueryData(PRODUCTS_KEY, data)
|
||||
}
|
||||
})
|
||||
}, [queryClient])
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: PRODUCTS_KEY,
|
||||
queryFn: fetchAndCacheProducts,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
gcTime: 60 * 60 * 1000,
|
||||
// Don't retry on failure — axios interceptor already fired backend-offline
|
||||
// which switches connection state; we fall back to IDB data instead
|
||||
retry: false,
|
||||
// Only start the query after we've checked IDB, so IDB data is visible first
|
||||
enabled: idbLoaded,
|
||||
})
|
||||
|
||||
// Prefer live query data; fall back to IDB snapshot if query failed or is loading
|
||||
const data = query.data ?? idbData
|
||||
|
||||
return {
|
||||
products: query.data?.products ?? [],
|
||||
categories: query.data?.categories ?? [],
|
||||
isLoading: query.isLoading && !query.data,
|
||||
products: data?.products ?? [],
|
||||
categories: data?.categories ?? [],
|
||||
isLoading: !idbLoaded && query.isLoading,
|
||||
}
|
||||
}
|
||||
|
||||
// Call this from SSEContext when products_changed arrives
|
||||
export function invalidateProductCache(queryClient) {
|
||||
queryClient.invalidateQueries({ queryKey: PRODUCTS_KEY })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user