Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import { 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<LoginDraft>( 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<string | null>(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 ( <div class="login-overlay"> <form class="login-card" onSubmit={submit}> <div class="login-brand"> <span class="login-brand-name">hackermusic</span> </div>
<div class="login-field"> <input id="login-host" ref={hostRef} type="text" aria-label="host" placeholder="host" defaultValue={draftStore.peek().host} onInput={(e) => draftStore.set((d) => ({ ...d, host: e.currentTarget.value }))} autocomplete="off" spellcheck="false" disabled={busy()} autofocus // password managers ignore autocomplete="off" for fields they // heuristically classify as credential-adjacent; each vendor // honors its own opt-out attribute. data-bwignore is verified // against bitwarden/clients collect-autofill-content.service.ts // (inputQuery excludes :not([data-bwignore])); presence alone // matters, value is cosmetic. data-bwignore="true" data-1p-ignore="true" data-lpignore="true" data-protonpass-ignore="true" data-form-type="other" /> </div>
<div class="login-field"> <input id="login-user" ref={userRef} type="text" aria-label="username" placeholder="username" defaultValue={draftStore.peek().username} onInput={(e) => draftStore.set((d) => ({ ...d, username: e.currentTarget.value }))} autocomplete="username" spellcheck="false" disabled={busy()} /> </div>
<div class="login-field"> <input id="login-pass" ref={passRef} type="password" aria-label="password" placeholder="password" autocomplete="current-password" disabled={busy()} /> </div>
{error() && ( <div class="login-error" role="alert"> {error()} </div> )}
<button type="submit" class="btn btn-primary" disabled={busy()}> {busy() ? "connecting…" : "connect"} </button> </form> </div> );};
export default LoginDialog;