import { useState, type FormEvent } from "react"; import { api } from "../api"; import { errorMessage, useAuth } from "../state"; import { Field } from "../components/ui"; import { Mark } from "../components/Mark"; import { Legal } from "../components/Legal"; export function Setup() { const { refresh } = useAuth(); const [username, setUsername] = useState("admin"); const [password, setPassword] = useState(""); const [confirm, setConfirm] = useState(""); const [endpointHost, setEndpointHost] = useState(window.location.hostname === "localhost" ? "" : window.location.hostname); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); async function submit(e: FormEvent) { e.preventDefault(); setError(""); if (password !== confirm) { setError("The passwords do not match."); return; } setBusy(true); try { await api.setup({ username, password, endpointHost }); await refresh(); } catch (err) { setError(errorMessage(err)); } finally { setBusy(false); } } return (