feat(waiter): the waiter app on the shared foundations (api client, access, i18n), lint clean, starts offline
Plan step 4.1. No visible change to layout or flows. - api/client.js on @xenia/api createApiClient (CS-8 baseURL, 401 → sign out, no answer → backend-offline, err.xenia, the manager-approval retry); the PIN login handles its own 401 (before, a wrong PIN reloaded the page). - <AccessProvider> at the root with an i18n instance: modules, permission keys, venue, language from bootstrap; who is signed in comes with it (no more /api/auth/me polling). packages/access: AccessProvider gains `cache` (the last bootstrap per session; used when the server can't be reached, `offline` in the context, keeps retrying) and `failed` (the app's own screen when there's nothing cached). The waiter caches per venue, shows its offline page (server search) on a first start without a server, retries every 5 s; ServerRediscovery and BundleSync run outside the bootstrap. - Role names → keys: the shift gate and the user menu use core.dashboard.access. - One shared, offline-cached read of the venue settings (api/venueSettings.js) instead of seven GET /api/settings/ calls; apiError() shows backend error codes in the user's language (17 places). - Lint: 78 errors → 0 (unused code, empty catches, two duplicate style keys, a component created during render, refs read during render → state, hooks after an early return); set-state-in-effect is a warning like the manager/terminal; lint in run-all. - Tests: tests/bootstrapCache.test.mjs; e2e browser: an offline restart stays signed in. Android rediscovery waits for what the screen shows instead of fixed sleeps: 10/10 three runs in a row (KI-009). Terminal 3.4 waits for both displays before the sale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,32 +22,56 @@ import { OverrideHost } from './OverrideHost.jsx'
|
||||
* default); if it fails, an error screen with Retry. Nothing below ever sees a half-loaded state.
|
||||
* autoRetryMs (unattended screens, e.g. the terminal): when the server can't be reached
|
||||
* (offline or 5xx), retry by itself every autoRetryMs as well.
|
||||
* cache (apps that must start offline, CS-3: the waiter, plan step 4.1): { load(token) → data
|
||||
* | null, save(token, data) }. Every bootstrap that loads is saved; when the server can't be
|
||||
* reached, the last one saved for the same token is used (`offline: true` in the context)
|
||||
* and the provider keeps retrying (autoRetryMs, or every 15 s) until the server answers.
|
||||
* failed (optional): (error, retry) → what to show when the bootstrap can't load and there is
|
||||
* no cached one (the waiter shows its offline screen with the server search); default: an
|
||||
* error with Retry.
|
||||
*/
|
||||
const CACHE_RETRY_MS = 15_000
|
||||
|
||||
export function AccessProvider({
|
||||
client, token = null, i18n, deviceLanguage, override = true, loading, autoRetryMs = 0, children,
|
||||
client, token = null, i18n, deviceLanguage, override = true, loading, autoRetryMs = 0, cache, failed, children,
|
||||
}) {
|
||||
const [state, setState] = useState({ data: null, forToken: undefined, error: null })
|
||||
const [state, setState] = useState({ data: null, forToken: undefined, error: null, offline: false })
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const { data } = await client.get('/api/platform/bootstrap', { xenia: { ignoreUnauthorized: true, noOverride: true } })
|
||||
const apply = async data => {
|
||||
if (i18n) {
|
||||
await i18n.changeLanguage(resolveLanguage({ user: data.user?.language, device: deviceLanguage, venue: data.venue?.language }))
|
||||
}
|
||||
setState({ data, forToken: token, error: null })
|
||||
} catch (e) {
|
||||
setState(s => ({ ...s, forToken: token, error: parseError(e) }))
|
||||
}
|
||||
}, [client, token, i18n, deviceLanguage])
|
||||
try {
|
||||
const { data } = await client.get('/api/platform/bootstrap', { xenia: { ignoreUnauthorized: true, noOverride: true } })
|
||||
await apply(data)
|
||||
try { cache?.save(token, data) } catch { /* storage full or blocked: still works online */ }
|
||||
setState({ data, forToken: token, error: null, offline: false })
|
||||
} catch (e) {
|
||||
const error = parseError(e)
|
||||
let cached = null
|
||||
if (cache && (error.offline || error.status >= 500)) {
|
||||
try { cached = cache.load(token) } catch { cached = null }
|
||||
}
|
||||
if (cached) {
|
||||
await apply(cached)
|
||||
setState({ data: cached, forToken: token, error: null, offline: true })
|
||||
} else {
|
||||
setState(s => ({ ...s, forToken: token, error, offline: false }))
|
||||
}
|
||||
}
|
||||
}, [client, token, i18n, deviceLanguage, cache])
|
||||
|
||||
useEffect(() => { load() }, [load])
|
||||
|
||||
const unreachable = !!state.error && (state.error.offline || state.error.status >= 500)
|
||||
useEffect(() => {
|
||||
if (!autoRetryMs || !unreachable) return undefined
|
||||
const id = setTimeout(load, autoRetryMs)
|
||||
const every = state.offline ? (autoRetryMs || CACHE_RETRY_MS) : unreachable ? autoRetryMs : 0
|
||||
if (!every) return undefined
|
||||
const id = setTimeout(load, every)
|
||||
return () => clearTimeout(id)
|
||||
}, [autoRetryMs, unreachable, state.error, load])
|
||||
}, [autoRetryMs, unreachable, state.offline, state.error, load])
|
||||
|
||||
const value = useMemo(() => {
|
||||
const d = state.data
|
||||
@@ -62,9 +86,10 @@ export function AccessProvider({
|
||||
language: d.language,
|
||||
modules: new Set((d.modules || []).map(m => m.key)),
|
||||
permissions: new Set(d.permissions || []),
|
||||
offline: state.offline, // running on the cached bootstrap (cache prop)
|
||||
refresh: load,
|
||||
}
|
||||
}, [state.data, load])
|
||||
}, [state.data, state.offline, load])
|
||||
|
||||
const current = state.forToken === token
|
||||
let content
|
||||
@@ -76,7 +101,7 @@ export function AccessProvider({
|
||||
</AccessContext.Provider>
|
||||
)
|
||||
} else if (state.error && current) {
|
||||
content = <LoadFailed error={state.error} retry={load} />
|
||||
content = failed?.(state.error, load) ?? <LoadFailed error={state.error} retry={load} />
|
||||
} else {
|
||||
content = loading === undefined ? <DefaultLoading /> : loading
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user