feat(waiter): keep the menu's photos for offline (platform-v2 step 5.2)

Right after the menu is read the phone downloads its photos (4 at a time,
services/photoCache.js). The library's year-long immutable headers keep them
in the browser cache; the installed PWA also caches /api/media/file/ cache-
first (workbox `xenia-media`). With no connection the menu still shows them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 15:57:51 +02:00
co-authored by Claude Opus 5.5
parent e09056a7a4
commit 15bd0f5b57
4 changed files with 45 additions and 1 deletions
+2
View File
@@ -1,3 +1,4 @@
import { warmPhotos } from '../services/photoCache'
import { createContext, useContext, useCallback, useEffect, useRef } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import useAuthStore from '../store/authStore'
@@ -67,6 +68,7 @@ export function LiveProvider({ children }) {
])
await db.products.bulkPut(prodRes.data)
await db.categories.bulkPut(catRes.data)
warmPhotos(prodRes.data, catRes.data) // the menu's photos kept for offline (5.2)
// Also update React Query cache so AddItemsPage gets it immediately
queryClient.setQueryData(['products'], { products: prodRes.data, categories: catRes.data })
} catch { /* offline — snapshot stays as-is */ }
+2
View File
@@ -2,6 +2,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useEffect, useState } from 'react'
import client from '../api/client'
import db from '../db/posdb'
import { warmPhotos } from '../services/photoCache'
export const PRODUCTS_KEY = ['products']
@@ -15,6 +16,7 @@ async function fetchAndCacheProducts() {
db.products.bulkPut(products).catch(() => {})
db.categories.bulkPut(categories).catch(() => {})
warmPhotos(products, categories) // kept for offline (5.2)
return { products, categories }
}
+30
View File
@@ -0,0 +1,30 @@
// The menu's photos kept for offline (plan step 5.2). Photos come from the media library at
// /api/media/file/<hash>/card: an address that never changes meaning, served with a year-long
// `immutable` cache header. So once a photo is downloaded the phone keeps it (the browser's
// cache; in the installed PWA also the service worker's `xenia-media` cache) and shows it
// without a connection. warmPhotos() downloads the menu's photos ahead, a few at a time, right
// after the menu is read, so a product never shown before still has its photo offline.
import { assetUrl } from '../config/server'
const warmed = new Set()
const AT_ONCE = 4
function load(url) {
return new Promise(resolve => {
const img = new Image()
img.onload = img.onerror = () => resolve()
img.decoding = 'async'
img.src = url
})
}
/** Download the photos of these products (and categories) that this app run hasn't fetched yet. */
export async function warmPhotos(...lists) {
if (typeof Image === 'undefined' || (typeof navigator !== 'undefined' && navigator.onLine === false)) return
const urls = [...new Set(lists.flat().map(x => x?.image_url || x?.image?.card).filter(Boolean).map(u => assetUrl(u)))]
.filter(u => !warmed.has(u))
urls.forEach(u => warmed.add(u))
const queue = [...urls]
const worker = async () => { while (queue.length) await load(queue.shift()) }
await Promise.all(Array.from({ length: Math.min(AT_ONCE, queue.length) }, worker))
}