"use client"; import { useEffect, useState, useTransition } from "react"; import { createAccount } from "@/app/actions"; import FormDialogShell, { Field, inputClass } from "./form-dialog-shell"; type Props = { open: boolean; onClose: () => void; prefixPattern?: string; }; export default function CreateAccountDialog({ open, onClose, prefixPattern }: Props) { const [pending, startTransition] = useTransition(); const [error, setError] = useState(null); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [status, setStatus] = useState(""); const [link, setLink] = useState(""); // Reset on open. useEffect(() => { if (open) { setUsername(""); setPassword(""); setStatus(""); setLink(""); setError(null); } }, [open]); function submit() { if (!username.trim() || !password) { setError("Username and password are required"); return; } setError(null); startTransition(async () => { const result = await createAccount({ username: username.trim(), password, status, link, }); if (result.ok) { onClose(); } else { setError(result.error); } }); } return ( setUsername(e.target.value)} disabled={pending} autoFocus autoComplete="off" spellCheck={false} placeholder={prefixPattern ? `${prefixPattern}1234` : "username"} className={inputClass} /> setPassword(e.target.value)} disabled={pending} autoComplete="off" spellCheck={false} className={inputClass} /> setStatus(e.target.value)} disabled={pending} autoComplete="off" spellCheck={false} placeholder="(leave blank for available)" className={inputClass} /> setLink(e.target.value)} disabled={pending} autoComplete="off" spellCheck={false} placeholder="https://..." className={inputClass} /> ); }