import { useState, useEffect, useRef, useCallback } from 'react'; import { AlertCircle, Check, Loader2 } from 'lucide-react'; import { Label } from '@/components/ui/label'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { checkNameAvailable, type NameAvailability } from '@/lib/deploy/NpanelAdapter'; /** How long typing has to stop before the gateway is asked about a name. */ const AVAILABILITY_DEBOUNCE_MS = 400; interface NpanelDeployFormProps { /** The gateway's API origin, e.g. `npanel.shakespeare.to`. */ dashboardHost: string; /** The domain sites are served under, e.g. `shakespeare.wtf`. */ domain: string; /** Falls back to the project id when nothing has been deployed yet. */ projectId: string; projectName: string; savedSubdomain?: string; savedSiteTitle?: string; savedSiteDescription?: string; onSubdomainChange: (subdomain: string) => void; onSiteTitleChange: (title: string) => void; onSiteDescriptionChange: (description: string) => void; onValidationChange?: (isValid: boolean) => void; } /** What the field is currently saying about the name in it. */ type NameState = | { kind: 'empty' } | { kind: 'mine' } | { kind: 'checking' } | { kind: 'free' } | { kind: 'unavailable'; message: string }; function describe(availability: NameAvailability, domain: string): NameState { // A name the gateway recognises as this user's own. It is spoken for, and // they are who it is spoken for by — deploying updates the site already // there rather than taking anything. if (availability.reason === 'mine') return { kind: 'mine' }; if (availability.available) return { kind: 'free' }; if (availability.reason === 'taken') { return { kind: 'unavailable', message: `That name is taken on ${domain}. Try another.` }; } return { kind: 'unavailable', message: availability.error ?? 'That name cannot be used.' }; } export function NpanelDeployForm({ dashboardHost, domain, projectId, projectName, savedSubdomain, savedSiteTitle, savedSiteDescription, onSubdomainChange, onSiteTitleChange, onSiteDescriptionChange, onValidationChange, }: NpanelDeployFormProps) { const { user } = useCurrentUser(); const [subdomain, setSubdomain] = useState(savedSubdomain || projectId); const [siteTitle, setSiteTitle] = useState(savedSiteTitle ?? projectName); const [siteDescription, setSiteDescription] = useState(savedSiteDescription ?? ''); const [nameState, setNameState] = useState({ kind: 'empty' }); // Keyed by provider id in DeploySteps, so this runs once with fresh props. const initializedRef = useRef(false); const syncToParent = useCallback(() => { if (initializedRef.current) return; initializedRef.current = true; onSubdomainChange(savedSubdomain || projectId); onSiteTitleChange(savedSiteTitle ?? projectName); if (savedSiteDescription) onSiteDescriptionChange(savedSiteDescription); }, [ savedSubdomain, savedSiteTitle, savedSiteDescription, projectId, projectName, onSubdomainChange, onSiteTitleChange, onSiteDescriptionChange, ]); useEffect(() => { syncToParent(); }, [syncToParent]); // The name this project already deployed to is not "taken" — it is theirs, // and asking the gateway would truthfully say taken and alarm them about // their own site. Compared trimmed, since that is what gets deployed: a // trailing space is not a different name. const trimmed = subdomain.trim(); const isOwnName = Boolean(savedSubdomain) && trimmed === savedSubdomain; useEffect(() => { if (!trimmed) { setNameState({ kind: 'empty' }); return; } if (isOwnName) { setNameState({ kind: 'mine' }); return; } setNameState({ kind: 'checking' }); const controller = new AbortController(); const timer = setTimeout(async () => { // Signed, where there is someone to sign: a name this user owns but this // project has never deployed to — one they took back through the // migration — is otherwise indistinguishable from a stranger's. const availability = await checkNameAvailable( dashboardHost, `${trimmed}.${domain}`, controller.signal, user?.signer, ); if (!controller.signal.aborted) setNameState(describe(availability, domain)); }, AVAILABILITY_DEBOUNCE_MS); return () => { controller.abort(); clearTimeout(timer); }; }, [trimmed, isOwnName, dashboardHost, domain, user?.signer]); // A name still being checked stays deployable: the gateway decides for real // when the deploy runs, and blocking the button on an in-flight request would // make the form feel stuck on a slow connection. const isValid = subdomain.trim() !== '' && nameState.kind !== 'unavailable'; useEffect(() => { onValidationChange?.(isValid); }, [isValid, onValidationChange]); const handleSubdomainChange = (value: string) => { setSubdomain(value); onSubdomainChange(value); }; const handleSiteTitleChange = (value: string) => { setSiteTitle(value); onSiteTitleChange(value); }; const handleSiteDescriptionChange = (value: string) => { setSiteDescription(value); onSiteDescriptionChange(value); }; return (
handleSubdomainChange(e.target.value)} placeholder={projectId} className={nameState.kind === 'unavailable' ? 'border-destructive' : ''} /> .{domain}
{nameState.kind === 'checking' && (
Checking availability…
)} {nameState.kind === 'free' && (
{subdomain.trim()}.{domain} is available.
)} {nameState.kind === 'mine' && (

Updating {subdomain.trim()}.{domain}.

)} {nameState.kind === 'unavailable' && (
{nameState.message}
)}
handleSiteTitleChange(e.target.value)} placeholder="My Nostr Site" />