import { useState } from "react"; import { useServerFn } from "@tanstack/react-start"; import { Globe, Loader2, X } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { connectCustomDomain } from "@/lib/codehub.functions"; type Result = { domain: string; target: string; status: string; dns: "added" | "exists" | "manual" }; export function ConnectDomainButton({ kind, id, disabled }: { kind: "echo" | "hub"; id: string; disabled?: boolean }) { const connect = useServerFn(connectCustomDomain); const [open, setOpen] = useState(false), [domain, setDomain] = useState(""), [busy, setBusy] = useState(false); const [result, setResult] = useState(null), [error, setError] = useState(null); const submit = async () => { setBusy(true); setError(null); setResult(null); try { const r = await connect({ data: { kind, id, domain } }); if ("error" in r) throw new Error(r.error); setResult(r as Result); toast.success(`${r.domain} connected.`); } catch (e) { setError(e instanceof Error ? e.message.replace(/^.*"message":\s*"([^"]+)".*$/s, "$1") : "Could not connect the domain."); } finally { setBusy(false); } }; return <> {open &&

Connect your domain

Links a domain you own to this published site in your Cloudflare account.

setDomain(e.target.value)} onKeyDown={e => e.key === "Enter" && domain && void submit()} />
{error &&

{error}

} {result &&

{result.domain} is linked. Status: {result.status}.

{result.dns === "manual" ?

Add this record where you bought the domain: CNAME {result.domain} → {result.target}. It usually goes live within an hour.

:

{result.dns === "added" ? "DNS record added automatically." : "A DNS record already exists for this name."} It usually goes live within a few minutes.

}
}
} ; }