"use client"; import { useState } from "react"; import { Button } from "./Button"; import { Field } from "./Field"; import { Note } from "./Panel"; import { ApiError, type RenameResult } from "@/lib/api"; import { baseSlug, hostFor, slugError } from "@/lib/slug"; /* * The rename control, and only the control — the same shape as RelinkPanel: an * input that expands in place rather than a modal, because this app has no modal * and one action with one field does not need one. * * The host preview is drawn from lib/slug.ts, a mirror of the Go rules. It can * disagree with the server; the 409 that comes back is the answer that counts. */ export function RenamePanel({ currentName, currentSlug, onRename, }: { currentName: string; currentSlug: string; onRename: (name: string) => Promise; }) { const [open, setOpen] = useState(false); const [value, setValue] = useState(currentName); const [error, setError] = useState(); const [busy, setBusy] = useState(false); const [done, setDone] = useState(); const name = value.trim(); const derived = baseSlug(name); const invalid = slugError(name); // A cosmetic edit that lands on the same slug is still a rename worth doing — // the name is what the customer reads. Only an empty or unchanged name is // nothing to submit. const unchanged = name === currentName.trim(); async function submit() { setError(undefined); setBusy(true); try { const res = await onRename(name); setDone(res); setOpen(false); } catch (err) { setError(err instanceof ApiError ? err.message : "Rename failed. Try again."); } finally { setBusy(false); } } if (done) { const host = done.login_url || `https://${hostFor(done.slug)}`; return ( This instance is now {done.name}, at{" "} {hostFor(done.slug)}. The old address has stopped working, and your sign-in does not follow it — you will need to sign in again there. Open {hostFor(done.slug)} → ); } return (
{open && ( setValue(e.target.value)} error={error ?? (name ? invalid : undefined)} hint={ name && !invalid ? ( <> Moves to {hostFor(derived)} {derived === currentSlug && " — the address does not change"} ) : ( "Letters and digits; everything else becomes a hyphen." ) } /> )}
{open && ( Anyone signed in will need to sign in again at the new address, and links to the old one stop working. )}
); }