ManiCandles/src/pages/ConnectionPage.tsx

129 lines
4.4 KiB
TypeScript

import { useState } from "react";
import { Link } from "wouter";
import Container from "@mui/material/Container";
import Paper from "@mui/material/Paper";
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";
import Alert from "@mui/material/Alert";
import { healthCheck, organizationSchema } from "@/lib/api";
import { usePlatform } from "@/lib/platform";
import { ApiNotice } from "@/components/ApiNotice";
import "./workspace.css";
export default function ConnectionPage() {
const { organization, setOrganization, principal, signOut } = usePlatform();
const [draft, setDraft] = useState(organization);
const [error, setError] = useState<unknown>(null);
const [saved, setSaved] = useState(false);
const [busy, setBusy] = useState(false);
const [checks, setChecks] = useState<PromiseSettledResult<unknown>[] | null>(
null,
);
const [checking, setChecking] = useState(false);
async function check() {
setChecking(true);
setChecks(null);
setChecks(
await Promise.allSettled([healthCheck("live"), healthCheck("ready")]),
);
setChecking(false);
}
return (
<section className="workspace-page">
<Container maxWidth="md">
<h1>Platform Connection</h1>
<p className="workspace-intro">
Connect to your organization on the deployed Mani Candles backend.
</p>
<Paper variant="outlined" className="workspace-panel">
<h2>Organization</h2>
<p>
Use the organization UUID returned by the backend owner bootstrap.
Public account registration is not available.
</p>
<form
className="workspace-form"
onSubmit={async (event) => {
event.preventDefault();
setSaved(false);
setError(null);
const result = organizationSchema.safeParse(draft.trim());
if (!result.success) {
setError(new Error("Enter a valid organization UUID."));
return;
}
setBusy(true);
try {
if (principal && result.data !== organization) await signOut();
setOrganization(result.data);
setSaved(true);
} catch (error) {
setError(error);
} finally {
setBusy(false);
}
}}
>
<TextField
label="Organization UUID"
required
value={draft}
onChange={(event) => {
setDraft(event.target.value);
setSaved(false);
}}
helperText="Only the organization ID is saved locally; credentials are not."
/>
<ApiNotice error={error} />
<Button type="submit" variant="contained" disabled={busy}>
{busy ? "Saving…" : "Save connection"}
</Button>
{saved && (
<Alert severity="success">
Connection saved.{" "}
<Link href="/products">
<u>Browse candles</u>
</Link>{" "}
or{" "}
<Link href="/workspace">
<u>sign in</u>
</Link>
.
</Alert>
)}
</form>
<hr />
<h2>Service checks</h2>
<Button variant="outlined" disabled={checking} onClick={check}>
{checking ? "Checking…" : "Run health checks"}
</Button>
<div aria-live="polite">
{checks?.map((result, index) => (
<div className="health-result" key={index}>
<Alert
severity={result.status === "fulfilled" ? "success" : "error"}
>
{index ? "Database readiness" : "Service liveness"}:{" "}
{result.status === "fulfilled" ? "Passed" : "Failed"}
</Alert>
{result.status === "rejected" && (
<ApiNotice error={result.reason} />
)}
</div>
))}
</div>
<p className="workspace-intro">
<a
href="https://manicandles-backend.midastix.com/api/docs"
target="_blank"
rel="noreferrer"
>
<u>Open API documentation ↗</u>
</a>
</p>
</Paper>
</Container>
</section>
);
}