import { PUBLIC_API_URL } from "$env/static/public"; /** Same-origin empty base uses Vite `/healthz` proxy; otherwise hit the API host. */ const API_BASE = (PUBLIC_API_URL ?? "").replace(/\/$/, ""); let maintenanceState = $state(false); let readOnlyState = $state(false); let hypercareState = $state(false); let polledState = $state(false); function healthzUrl(): string { return API_BASE ? `${API_BASE}/healthz` : "/healthz"; } function asBool(value: unknown): boolean { return value === true || value === "true" || value === 1; } /** Shared API / health flags for cutover maintenance and read-only mode. */ export const systemMode = { get maintenance(): boolean { return maintenanceState; }, get readOnly(): boolean { return readOnlyState; }, /** Tenant hypercare window (P1-17 report-missing CTA). */ get hypercare(): boolean { return hypercareState; }, /** True after at least one health/API signal updated flags. */ get known(): boolean { return polledState; }, /** Mutations should be blocked in the UI (matches API MaintenanceGate). */ get mutationsBlocked(): boolean { return maintenanceState || readOnlyState; }, get bannerTitle(): string { if (maintenanceState) return "Maintenance in progress"; if (readOnlyState) return "Read-only mode"; return ""; }, get bannerMessage(): string { if (maintenanceState) { return "The API is temporarily unavailable while we perform maintenance. You can browse when it returns; changes are disabled."; } if (readOnlyState) { return "The system is in read-only mode. Viewing data is fine; creating, updating, and deleting are disabled until this is lifted."; } return ""; }, applyFlags(flags: { maintenance?: boolean; readOnly?: boolean; hypercare?: boolean }) { if (flags.maintenance !== undefined) maintenanceState = flags.maintenance; if (flags.readOnly !== undefined) readOnlyState = flags.readOnly; if (flags.hypercare !== undefined) hypercareState = flags.hypercare; polledState = true; }, /** Pull `maintenance` / `read_only` / `hypercare` from an API JSON body when present. */ applyFromBody(body: unknown) { if (!body || typeof body !== "object") return; const record = body as Record; const hasMaintenance = "maintenance" in record; const hasReadOnly = "read_only" in record; const hasHypercare = "hypercare" in record; if (!hasMaintenance && !hasReadOnly && !hasHypercare) return; if (hasMaintenance) maintenanceState = asBool(record.maintenance); if (hasReadOnly) readOnlyState = asBool(record.read_only); if (hasHypercare) hypercareState = asBool(record.hypercare); polledState = true; }, clear() { maintenanceState = false; readOnlyState = false; hypercareState = false; polledState = false; } }; /** * Probe `/healthz` (exempt from the maintenance gate) so the banner can show * even when every other API route returns 503. */ export async function refreshSystemMode(signal?: AbortSignal): Promise { try { const res = await fetch(healthzUrl(), { method: "GET", credentials: "include", headers: { Accept: "application/json" }, signal }); const text = await res.text(); if (!text) return; try { systemMode.applyFromBody(JSON.parse(text) as unknown); } catch { /* ignore non-JSON */ } } catch { /* network blip — keep last known flags */ } }