import { useState } from "react"; import { Globe, Loader2, Plus, Power, PowerOff, Trash2 } from "lucide-react"; import { toast } from "@/client/components/toast"; import { Badge, Button, Card, PageHeader } from "@/client/components/ui"; import { useAdminSessionGuard } from "@/client/hooks/useAdminSessionGuard"; import { addAdminDomain, deleteAdminDomain, updateAdminDomain, type AdminDomain } from "@/client/lib/api"; import { fullDate } from "@/client/lib/time"; interface DomainManagerProps { domains: AdminDomain[]; loading: boolean; onAdminSessionError: (message?: string) => void; onReload: () => Promise; } export function DomainManager({ domains, loading, onAdminSessionError, onReload }: DomainManagerProps) { const [newDomain, setNewDomain] = useState(""); const [newDomainActive, setNewDomainActive] = useState(true); const [busy, setBusy] = useState(null); const handleAdminError = useAdminSessionGuard(onAdminSessionError); const handleAdd = async (event: React.FormEvent) => { event.preventDefault(); setBusy("add"); try { await addAdminDomain(newDomain, newDomainActive); setNewDomain(""); setNewDomainActive(true); await onReload(); toast.success(`Domain ${newDomain} added`); } catch (error) { handleAdminError(error, toast.error); } finally { setBusy(null); } }; const handleToggle = async (domain: AdminDomain) => { setBusy(domain.domain); try { await updateAdminDomain(domain.domain, !domain.isActive); await onReload(); toast.success(`${domain.domain} ${domain.isActive ? "disabled" : "enabled"}`); } catch (error) { handleAdminError(error, toast.error); } finally { setBusy(null); } }; const handleDelete = async (domain: AdminDomain) => { if (!domain.canDelete) { return; } const confirmed = window.confirm(`Delete ${domain.domain} from the domain pool?`); if (!confirmed) { return; } setBusy(domain.domain); try { await deleteAdminDomain(domain.domain); await onReload(); toast.success(`${domain.domain} deleted`); } catch (error) { handleAdminError(error, toast.error); } finally { setBusy(null); } }; return (
} heading="Manage inbound domains" description="Active domains are available for public inbox creation. Disabled domains no longer accept new temporary inboxes or inbound mail." /> {domains.length} domain{domains.length !== 1 ? "s" : ""}
setNewDomain(event.target.value)} placeholder="example.com" disabled={busy === "add"} className="w-full rounded-lg border border-zinc-700/60 bg-zinc-800 px-4 py-2.5 text-sm text-zinc-100 placeholder-zinc-500 outline-none transition-colors focus:border-accent-500/50 focus:ring-1 focus:ring-accent-500/30 disabled:opacity-50" />
{loading ? (

Loading domains...

) : null} {!loading && domains.length === 0 ?

No domains configured yet.

: null} {domains.map((domain) => { const isBusy = busy === domain.domain; return (
{domain.domain} {domain.isActive ? "active" : "disabled"}

Added {fullDate(domain.createdAt)} ยท {domain.inboxCount} inbox {domain.inboxCount !== 1 ? "es" : ""}

); })}
); }