From 1807262cdbe784ef4bc500484ba4b7eccfcac4f2 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 26 Oct 2026 23:53:48 +1100 Subject: [PATCH] feat(dashboard-v2): TanStack Query plus session state --- dashboard-v2/package.json | 1 + dashboard-v2/pnpm-lock.yaml | 38 ++++++------- dashboard-v2/src/App.tsx | 31 ++++++++--- dashboard-v2/src/lib/api.ts | 1 + dashboard-v2/src/lib/query.ts | 16 ++++++ dashboard-v2/src/main.tsx | 13 +++-- dashboard-v2/src/pages/LoginPage.tsx | 12 ++--- dashboard-v2/src/pages/StatusPage.tsx | 77 ++++++++++++++------------- dashboard-v2/src/session.tsx | 60 +++++++++++++++++++++ 9 files changed, 174 insertions(+), 75 deletions(-) create mode 100644 dashboard-v2/src/lib/query.ts create mode 100644 dashboard-v2/src/session.tsx diff --git a/dashboard-v2/package.json b/dashboard-v2/package.json index e05372f5b..7e334ff9b 100644 --- a/dashboard-v2/package.json +++ b/dashboard-v2/package.json @@ -14,6 +14,7 @@ "@astryxdesign/theme-neutral": "0.7.4", "@heroicons/react": "^2.2.0", "@stylexjs/stylex": "^0.19.0", + "@tanstack/react-query": "^5.0.0", "react": "^19.1.0", "react-dom": "^19.1.0" }, diff --git a/dashboard-v2/pnpm-lock.yaml b/dashboard-v2/pnpm-lock.yaml index 654a473d5..e26a1cae8 100644 --- a/dashboard-v2/pnpm-lock.yaml +++ b/dashboard-v2/pnpm-lock.yaml @@ -168,16 +168,16 @@ importers: dependencies: '@astryxdesign/core': specifier: 0.7.4 - version: 0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1) + version: 0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1) '@astryxdesign/theme-neutral': specifier: 0.7.4 - version: 0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1) + version: 0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1) '@heroicons/react': specifier: ^2.2.0 version: 2.2.0(react@19.3.1) '@stylexjs/stylex': - specifier: ^0.8.0 - version: 0.8.0 + specifier: ^0.19.0 + version: 0.19.1 react: specifier: ^19.1.0 version: 19.3.1 @@ -187,7 +187,7 @@ importers: devDependencies: '@astryxdesign/cli': specifier: 0.7.4 - version: 0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@astryxdesign/theme-neutral@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1)) + version: 0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@astryxdesign/theme-neutral@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1)) '@types/react': specifier: ^19.1.0 version: 19.3.0 @@ -571,8 +571,8 @@ packages: '@rolldown/pluginutils@1.0.1': resolution: {integrity: sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==} - '@stylexjs/stylex@0.8.0': - resolution: {integrity: sha512-b9W6tFkX7RRo08UeY25cE5zhRREbg/8tj7oQNJPTvZaYZOWIj1ij3VTwNwI7bYG1RdNJO6bi2mjar9wsEV/Szw==} + '@stylexjs/stylex@0.19.1': + resolution: {integrity: sha512-JwNFm0MOYPqi+M4Ax5Un4ZxfZp4HEAX2sAWetZ88KALFcv1eEiHNJs75oO3V3U7RVWnzfkiM8i9eBowC3hqgUw==} '@types/babel__core@7.20.5': resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==} @@ -972,8 +972,8 @@ packages: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} - styleq@0.1.3: - resolution: {integrity: sha512-3ZUifmCDCQanjeej1f6kyl/BeP/Vae5EYkQ9iJfUm/QwZvlgnZzyflqAsAWYURdtea8Vkvswu2GrC57h3qffcA==} + styleq@0.2.1: + resolution: {integrity: sha512-L0TR0NQb+X4/ktDEKmjWyp27gla+LUYi/by5k5SjKXf6/pvZP7wbwEB5J+tqxdFVPgzbsuz+d4RTScO/QZquBw==} tiny-invariant@1.3.3: resolution: {integrity: sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==} @@ -1058,7 +1058,7 @@ packages: snapshots: - '@astryxdesign/cli@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@astryxdesign/theme-neutral@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1))': + '@astryxdesign/cli@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@astryxdesign/theme-neutral@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1))': dependencies: '@babel/core': 7.29.7 '@babel/parser': 7.29.8 @@ -1075,22 +1075,22 @@ snapshots: recast: 0.23.21 zod: 4.6.7 optionalDependencies: - '@astryxdesign/core': 0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1) - '@astryxdesign/theme-neutral': 0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1) + '@astryxdesign/core': 0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1) + '@astryxdesign/theme-neutral': 0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1) transitivePeerDependencies: - '@babel/preset-env' - supports-color - '@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1)': + '@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1)': dependencies: - '@stylexjs/stylex': 0.8.0 + '@stylexjs/stylex': 0.19.1 intl-messageformat: 11.2.15 react: 19.3.1 react-dom: 19.3.1(react@19.3.1) - '@astryxdesign/theme-neutral@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1)': + '@astryxdesign/theme-neutral@0.7.4(@astryxdesign/core@0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1)': dependencies: - '@astryxdesign/core': 0.7.4(@stylexjs/stylex@0.8.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1) + '@astryxdesign/core': 0.7.4(@stylexjs/stylex@0.19.1)(react-dom@19.3.1(react@19.3.1))(react@19.3.1) lucide-react: 1.61.0(@types/react@19.3.0)(react@19.3.1) react: 19.3.1 transitivePeerDependencies: @@ -1439,11 +1439,11 @@ snapshots: '@rolldown/pluginutils@1.0.1': {} - '@stylexjs/stylex@0.8.0': + '@stylexjs/stylex@0.19.1': dependencies: css-mediaquery: 0.1.2 invariant: 2.2.4 - styleq: 0.1.3 + styleq: 0.2.1 '@types/babel__core@7.20.5': dependencies: @@ -1790,7 +1790,7 @@ snapshots: source-map@0.6.1: {} - styleq@0.1.3: {} + styleq@0.2.1: {} tiny-invariant@1.3.3: {} diff --git a/dashboard-v2/src/App.tsx b/dashboard-v2/src/App.tsx index 826d38fa4..1e5ca4643 100644 --- a/dashboard-v2/src/App.tsx +++ b/dashboard-v2/src/App.tsx @@ -1,14 +1,29 @@ -import { useCallback, useState } from "react"; +import { Center } from "@astryxdesign/core/Center"; +import { Text } from "@astryxdesign/core/Text"; -import LoginPage from "./pages/LoginPage.tsx"; import StatusPage from "./pages/StatusPage.tsx"; +import LoginPage from "./pages/LoginPage.tsx"; +import { useSession } from "./session.tsx"; export default function App() { - const [needsAuth, setNeedsAuth] = useState(false); - const showLogin = useCallback(() => setNeedsAuth(true), []); - const showStatus = useCallback(() => setNeedsAuth(false), []); + const { state } = useSession(); - // The status page remounts after sign-in, so it loads with the session. - if (needsAuth) return ; - return ; + if (state === "unknown") { + return ( +
+ Loading… +
+ ); + } + // Fresh installs set up through the Vue dashboard until v2 grows a + // setup page. + if (state === "setup") { + return ( +
+ Complete initial setup before signing in. +
+ ); + } + if (state === "guest") return ; + return ; } diff --git a/dashboard-v2/src/lib/api.ts b/dashboard-v2/src/lib/api.ts index 8fb68fc6c..c522e9f5f 100644 --- a/dashboard-v2/src/lib/api.ts +++ b/dashboard-v2/src/lib/api.ts @@ -90,6 +90,7 @@ export interface ComponentVersion { } export const api = { + authStatus: () => request<{ setup_required?: boolean }>("/api/auth/status"), services: () => request("/api/services"), version: () => request<{ components: ComponentVersion[] }>("/api/version"), signIn: (email: string, password: string) => diff --git a/dashboard-v2/src/lib/query.ts b/dashboard-v2/src/lib/query.ts new file mode 100644 index 000000000..10df49bab --- /dev/null +++ b/dashboard-v2/src/lib/query.ts @@ -0,0 +1,16 @@ +import { QueryClient } from "@tanstack/react-query"; + +// Server state lives here. Pages read through useQuery and never cache +// responses in useState. +export const queryClient = new QueryClient({ + defaultOptions: { + queries: { + // Status data goes stale fast, but a focus storm on every alt-tab is + // worse: pages that need live data poll or refetch explicitly. + staleTime: 10_000, + refetchOnWindowFocus: false, + // Fail fast to the page's error state; the operator retries there. + retry: 1, + }, + }, +}); diff --git a/dashboard-v2/src/main.tsx b/dashboard-v2/src/main.tsx index c0ce98fb6..4b447e39e 100644 --- a/dashboard-v2/src/main.tsx +++ b/dashboard-v2/src/main.tsx @@ -2,18 +2,25 @@ import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; import { Theme } from "@astryxdesign/core/theme"; +import { QueryClientProvider } from "@tanstack/react-query"; import { defaultThemeSlug, themes } from "./astryx-themes.ts"; import App from "./App.tsx"; import "./globals.css"; +import { queryClient } from "./lib/query.ts"; +import { SessionProvider } from "./session.tsx"; const root = document.getElementById("root"); if (!root) throw new Error("missing #root element"); createRoot(root).render( - - - + + + + + + + , ); diff --git a/dashboard-v2/src/pages/LoginPage.tsx b/dashboard-v2/src/pages/LoginPage.tsx index 2f04cf42c..741380833 100644 --- a/dashboard-v2/src/pages/LoginPage.tsx +++ b/dashboard-v2/src/pages/LoginPage.tsx @@ -10,7 +10,7 @@ import { Heading, Text } from "@astryxdesign/core/Text"; import { TextInput } from "@astryxdesign/core/TextInput"; import { CubeIcon } from "@heroicons/react/24/outline"; -import { api } from "../lib/api.ts"; +import { useSession } from "../session.tsx"; // Standalone auth page paints its own body background (no host shell). const pageStyle: CSSProperties = { @@ -24,11 +24,8 @@ const contentStyle: CSSProperties = { maxWidth: 400, }; -export default function LoginPage({ - onSignedIn, -}: { - onSignedIn: () => void; -}) { +export default function LoginPage() { + const { signIn } = useSession(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); @@ -42,8 +39,7 @@ export default function LoginPage({ } setIsLoading(true); try { - await api.signIn(email, password); - onSignedIn(); + await signIn(email, password); } catch (err) { setError(err instanceof Error ? err.message : "Sign-in failed"); } finally { diff --git a/dashboard-v2/src/pages/StatusPage.tsx b/dashboard-v2/src/pages/StatusPage.tsx index 5e4c3b473..579c9e857 100644 --- a/dashboard-v2/src/pages/StatusPage.tsx +++ b/dashboard-v2/src/pages/StatusPage.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useState } from "react"; +import { useEffect } from "react"; import { Button } from "@astryxdesign/core/Button"; import { Card } from "@astryxdesign/core/Card"; @@ -19,6 +19,7 @@ import { type TableColumn, } from "@astryxdesign/core/Table"; import { Heading, Text } from "@astryxdesign/core/Text"; +import { useQuery } from "@tanstack/react-query"; import { ArrowPathIcon } from "@heroicons/react/24/outline"; import { @@ -27,9 +28,10 @@ import { type ComponentVersion, type Service, } from "../lib/api.ts"; +import { useSession } from "../session.tsx"; -// Mirror of the Vue dashboard's SystemStatusPage: service health plus -// per-component versions, in the dashboard template's frame. +// Service health plus per-component versions, in the dashboard template's +// frame. Server state comes from useQuery; the session owns the 401 bounce. function uptime(startedAt: string, state: Service["state"]): string { if (!startedAt || state === "down") return "not reported"; @@ -146,40 +148,39 @@ const versionColumns: TableColumn[] = [ }, ]; -export default function StatusPage({ - onAuthRequired, -}: { - onAuthRequired: () => void; -}) { - const [services, setServices] = useState([]); - const [components, setComponents] = useState([]); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(null); +function isAuthError(err: unknown): boolean { + return err instanceof ApiError && err.status === 401; +} - const load = useCallback(async () => { - setLoading(true); - setError(null); - try { - const [fetchedServices, report] = await Promise.all([ - api.services(), - api.version(), - ]); - setServices(fetchedServices); - setComponents(report.components); - } catch (err) { - if (err instanceof ApiError && err.status === 401) { - onAuthRequired(); - return; - } - setError(err instanceof Error ? err.message : "Could not load status"); - } finally { - setLoading(false); - } - }, [onAuthRequired]); +export default function StatusPage() { + const { expire } = useSession(); + const servicesQuery = useQuery({ + queryKey: ["services"], + queryFn: api.services, + }); + const versionQuery = useQuery({ + queryKey: ["version"], + queryFn: api.version, + }); + // A mid-session 401 bounces to the login page; anything else renders where + // it failed. + const authed = [servicesQuery.error, versionQuery.error].some(isAuthError); useEffect(() => { - void load(); - }, [load]); + if (authed) expire(); + }, [authed, expire]); + + const loading = servicesQuery.isPending || versionQuery.isPending; + const error = [servicesQuery.error, versionQuery.error].find( + (err) => err && !isAuthError(err), + ); + const services = servicesQuery.data ?? []; + const components = versionQuery.data?.components ?? []; + + const reload = () => { + void servicesQuery.refetch(); + void versionQuery.refetch(); + }; const up = services.filter((s) => s.state === "up").length; const attention = services.filter( @@ -218,7 +219,7 @@ export default function StatusPage({ variant="secondary" size="md" icon={} - onClick={() => void load()} + onClick={reload} /> {loading ? ( @@ -228,12 +229,14 @@ export default function StatusPage({ ) : error ? ( - {error} + + {error instanceof Error ? error.message : String(error)} +