import { createSignal, type VoidComponent } from "solid-js"; import * as v from "valibot"; import { saveConnection, testConnection } from "../lib/connection"; import { createPersistentSignal } from "../lib/persist"; /** * Centered modal, shown whenever no connection is configured. * Keyboard-first: host autofocuses, Enter submits from any field (native * form submit), the ping runs via testConnection() and we only persist on * success. * * Inputs are uncontrolled refs — submit reads the live DOM values, which * is both the keyboard-first idiom and immune to deferred signal commits. * * The host field opts out of autofill: password managers otherwise try to * "fill" it and mangle the URL. Username/password keep their semantic * autocomplete so managers can still do their actual job. * * host + username survive a page reload via a draft in localStorage; the * password deliberately does not — credentials only reach storage after a * successful ping (see connection.ts). */ interface LoginDraft { host: string; username: string; } const DRAFT_KEY = "hackermusic…t.v1"; /** * Both fields are `string()` and not `v.optional`, because the draft is read as * `draftStore.peek().host` straight into `defaultValue`: a stored `null` or a bare * string used to survive the JSON parse, leave the field undefined, and — in the * `null` case — throw while the dialog rendered. This is the login screen; a crash * here has no recovery path, which is the whole argument for checking the one value * this component reads before reading it. * * The draft is what the form is prefilled with and what a returning user's host is, * so it is shaped like a host without being one: it is typed text, and the store of * record (`connection.ts`) is where the host invariant is enforced. */ const draftSchema = v.object({ host: v.string(), username: v.string(), }); const LoginDialog: VoidComponent = () => { // per-mount store: seeding from localStorage happens at creation, so a // fresh mount always sees the latest draft (and tests see true reloads) const draftStore = createPersistentSignal( DRAFT_KEY, { host: "", username: "", }, { schema: draftSchema }, ); let hostRef: HTMLInputElement | undefined = undefined; let userRef: HTMLInputElement | undefined = undefined; let passRef: HTMLInputElement | undefined = undefined; const [busy, setBusy] = createSignal(false); const [error, setError] = createSignal(null); async function submit(event: SubmitEvent) { event.preventDefault(); if (busy()) return; const candidate = { host: hostRef?.value ?? "", username: userRef?.value ?? "", password: passRef?.value ?? "", }; if (!candidate.host.trim() || !candidate.username.trim()) { setError("host and username are required."); return; } setBusy(true); setError(null); const result = await testConnection(candidate); setBusy(false); if (result.ok) { saveConnection(candidate); // flips the app out of the login gate } else { setError(result.error ?? "connection failed."); hostRef?.focus(); } } return ( ); }; export default LoginDialog;