import { useCallback, useEffect, useMemo, useState } from 'react' import { parseError } from '@xenia/api' import { I18nProvider, resolveLanguage, useTranslation, useUiLabels } from '@xenia/i18n' import { Button, EmptyState, Spinner, UiLabelsProvider } from '@xenia/ui' import { AccessContext } from './context.js' import { OverrideHost } from './OverrideHost.jsx' /** * The root of every app (plan step 2.5, APPS_PLATFORM §9): * * * * * * - loads GET /api/platform/bootstrap (works signed in or not) and again whenever `token` * changes (login, logout, unlock), so modules and permission keys always match the session * - sets the language: user → device (`deviceLanguage`) → venue → en (D-12), and gives * bootstrap.venue (time zone, currency) to the formatters (@xenia/i18n) * - translates the @xenia/ui component labels and mounts the manager-approval dialog * for `client` (OverrideHost), unless override={false} * Until the bootstrap for the current token has loaded it renders `loading` (a spinner by * 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 const BOOTSTRAP_TIMEOUT_MS = 8_000 export function AccessProvider({ client, token = null, i18n, deviceLanguage, override = true, loading, autoRetryMs = 0, cache, failed, children, }) { const [state, setState] = useState({ data: null, forToken: undefined, error: null, offline: false }) const load = useCallback(async () => { const apply = async data => { if (i18n) { await i18n.changeLanguage(resolveLanguage({ user: data.user?.language, device: deviceLanguage, venue: data.venue?.language })) } } try { // a time limit: a request that never answers (a half-dead link) must not leave the app on // its loading screen for good; it counts as offline and is retried (4.6, Android e2e) const { data } = await client.get('/api/platform/bootstrap', { timeout: BOOTSTRAP_TIMEOUT_MS, 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(() => { 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.offline, state.error, load]) const value = useMemo(() => { const d = state.data if (!d) return null return { bootstrap: d, venue: d.venue, user: d.user, license: d.license, login: d.login, languages: d.languages, language: d.language, modules: new Set((d.modules || []).map(m => m.key)), permissions: new Set(d.permissions || []), approvable: new Set(d.approvable || []), // keys a manager may approve on the spot (4.3) offline: state.offline, // running on the cached bootstrap (cache prop) refresh: load, } }, [state.data, state.offline, load]) const current = state.forToken === token let content if (value && current && !state.error) { content = ( {override && } {children} ) } else if (state.error && current) { content = failed?.(state.error, load) ?? } else { content = loading === undefined ? : loading } if (!i18n) return content return ( {content} ) } function TranslatedLabels({ children }) { return {children} } function DefaultLoading() { return (
) } function LoadFailed({ error, retry }) { const { t } = useTranslation('common') return (
{t('actions.retry')}} />
) }