feat(dashboard-v2): TanStack Query plus session state
This commit is contained in:
9 files changed
+174
-75
No files matched your search
@@ -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"
|
||||
},
|
||||
|
||||
Generated
+19
-19
@@ -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: {}
|
||||
|
||||
|
||||
@@ -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 <LoginPage onSignedIn={showStatus} />;
|
||||
return <StatusPage onAuthRequired={showLogin} />;
|
||||
if (state === "unknown") {
|
||||
return (
|
||||
<Center axis="both" padding={6}>
|
||||
<Text type="body">Loading…</Text>
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
// Fresh installs set up through the Vue dashboard until v2 grows a
|
||||
// setup page.
|
||||
if (state === "setup") {
|
||||
return (
|
||||
<Center axis="both" padding={6}>
|
||||
<Text type="body">Complete initial setup before signing in.</Text>
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
if (state === "guest") return <LoginPage />;
|
||||
return <StatusPage />;
|
||||
}
|
||||
@@ -90,6 +90,7 @@ export interface ComponentVersion {
|
||||
}
|
||||
|
||||
export const api = {
|
||||
authStatus: () => request<{ setup_required?: boolean }>("/api/auth/status"),
|
||||
services: () => request<Service[]>("/api/services"),
|
||||
version: () => request<{ components: ComponentVersion[] }>("/api/version"),
|
||||
signIn: (email: string, password: string) =>
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -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(
|
||||
<StrictMode>
|
||||
<Theme theme={themes[defaultThemeSlug]}>
|
||||
<App />
|
||||
</Theme>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<Theme theme={themes[defaultThemeSlug]}>
|
||||
<SessionProvider>
|
||||
<App />
|
||||
</SessionProvider>
|
||||
</Theme>
|
||||
</QueryClientProvider>
|
||||
</StrictMode>,
|
||||
);
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<VersionRow>[] = [
|
||||
},
|
||||
];
|
||||
|
||||
export default function StatusPage({
|
||||
onAuthRequired,
|
||||
}: {
|
||||
onAuthRequired: () => void;
|
||||
}) {
|
||||
const [services, setServices] = useState<Service[]>([]);
|
||||
const [components, setComponents] = useState<ComponentVersion[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(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={<Icon icon={ArrowPathIcon} size="sm" />}
|
||||
onClick={() => void load()}
|
||||
onClick={reload}
|
||||
/>
|
||||
</HStack>
|
||||
{loading ? (
|
||||
@@ -228,12 +229,14 @@ export default function StatusPage({
|
||||
) : error ? (
|
||||
<Card>
|
||||
<VStack gap={4}>
|
||||
<Text type="body">{error}</Text>
|
||||
<Text type="body">
|
||||
{error instanceof Error ? error.message : String(error)}
|
||||
</Text>
|
||||
<Button
|
||||
label="Retry"
|
||||
variant="secondary"
|
||||
size="md"
|
||||
onClick={() => void load()}
|
||||
onClick={reload}
|
||||
/>
|
||||
</VStack>
|
||||
</Card>
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
|
||||
import { api } from "./lib/api.ts";
|
||||
|
||||
export type SessionState = "unknown" | "authed" | "guest" | "setup";
|
||||
|
||||
const SessionContext = createContext<{
|
||||
state: SessionState;
|
||||
signIn: (email: string, password: string) => Promise<void>;
|
||||
expire: () => void;
|
||||
} | null>(null);
|
||||
|
||||
// The only client state in the app: who is signed in. Everything else is
|
||||
// server state behind useQuery.
|
||||
export function SessionProvider({ children }: { children: ReactNode }) {
|
||||
const [state, setState] = useState<SessionState>("unknown");
|
||||
|
||||
useEffect(() => {
|
||||
let live = true;
|
||||
api
|
||||
.authStatus()
|
||||
.then((status) => {
|
||||
if (live) setState(status.setup_required ? "setup" : "authed");
|
||||
})
|
||||
.catch(() => {
|
||||
// Any failure to prove a session means no session: the login page
|
||||
// then shows the server's own message when sign-in is attempted.
|
||||
if (live) setState("guest");
|
||||
});
|
||||
return () => {
|
||||
live = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const signIn = useCallback(async (email: string, password: string) => {
|
||||
await api.signIn(email, password);
|
||||
setState("authed");
|
||||
}, []);
|
||||
|
||||
const expire = useCallback(() => setState("guest"), []);
|
||||
|
||||
return (
|
||||
<SessionContext.Provider value={{ state, signIn, expire }}>
|
||||
{children}
|
||||
</SessionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useSession() {
|
||||
const ctx = useContext(SessionContext);
|
||||
if (!ctx) throw new Error("useSession needs SessionProvider");
|
||||
return ctx;
|
||||
}
|
||||
Reference in new issue
Block a user