mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
The deploy form asked the gateway whether a name was free, which is not the question it needed answered. A name somebody recovered in the migration is theirs, is already serving their site, and came back "taken" — so the button went dead with "That name is taken on shakespeare.wtf. Try another." about their own site. Every name anyone took back was in this state: a hundred and four of them for one person. The form had an escape hatch for exactly this, and it was too narrow. It knew a name was the user's own only if this project had deployed to it before, read out of the project's own deploy record. A name recovered through the migration dialog has no such record — the site was published, the hostname repointed, and nothing about it ever went through this form. So the question is asked properly instead. The anonymous ask happens first and is usually the end of it, because a name somebody is picking is usually free. Only a name that comes back taken is asked about again with a signature, which is the one branch where the distinction exists and the one place worth spending a remote signer on. A gateway too old to answer the second question leaves the first answer standing, which is the behavior as it was. The deploy itself never needed fixing: bindHostname asks for the host, is given it, finds it already points where it is about to point, and returns. The form was the only thing in the way. Also compares the saved name trimmed, since that is what gets deployed — a trailing space is not a different name, and typing one told people their own site was somebody else's.
230 lines
7.7 KiB
TypeScript
230 lines
7.7 KiB
TypeScript
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<NameState>({ 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 (
|
|
<div className="space-y-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="npanel-subdomain">Address</Label>
|
|
<div className="flex items-center gap-2">
|
|
<Input
|
|
id="npanel-subdomain"
|
|
value={subdomain}
|
|
onChange={(e) => handleSubdomainChange(e.target.value)}
|
|
placeholder={projectId}
|
|
className={nameState.kind === 'unavailable' ? 'border-destructive' : ''}
|
|
/>
|
|
<span className="text-sm text-muted-foreground font-mono whitespace-nowrap">
|
|
.{domain}
|
|
</span>
|
|
</div>
|
|
|
|
{nameState.kind === 'checking' && (
|
|
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
<Loader2 className="h-3 w-3 animate-spin" />
|
|
<span>Checking availability…</span>
|
|
</div>
|
|
)}
|
|
|
|
{nameState.kind === 'free' && (
|
|
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
<Check className="h-3 w-3" />
|
|
<span>
|
|
<span className="font-mono">{subdomain.trim()}.{domain}</span> is available.
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{nameState.kind === 'mine' && (
|
|
<p className="text-xs text-muted-foreground">
|
|
Updating <span className="font-mono">{subdomain.trim()}.{domain}</span>.
|
|
</p>
|
|
)}
|
|
|
|
{nameState.kind === 'unavailable' && (
|
|
<div className="flex items-start gap-2 text-sm text-destructive">
|
|
<AlertCircle className="h-4 w-4 mt-0.5 flex-shrink-0" />
|
|
<span>{nameState.message}</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="npanel-title">Site Title</Label>
|
|
<Input
|
|
id="npanel-title"
|
|
value={siteTitle}
|
|
onChange={(e) => handleSiteTitleChange(e.target.value)}
|
|
placeholder="My Nostr Site"
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="npanel-description">
|
|
Description <span className="text-muted-foreground font-normal">(optional)</span>
|
|
</Label>
|
|
<Textarea
|
|
id="npanel-description"
|
|
value={siteDescription}
|
|
onChange={(e) => handleSiteDescriptionChange(e.target.value)}
|
|
placeholder="A short description of this site…"
|
|
rows={3}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|