/** * @file components/modals/CreateServerModal.tsx * @description Modal dialog allowing users to create a new server with custom name and accent color. */ "use client"; import { useState } from "react"; import { X, Check } from "lucide-react"; import { SERVER_COLOR_CLASSES, SERVER_COLOR_OPTIONS, } from "@/lib/constants/server.styles"; import { ActionButton } from "../ui/ActionButton"; /** Props for the CreateServerModal component. */ interface CreateServerModalProps { isOpen: boolean; onClose: () => void; } /** Renders a modal dialog to create a new server with name and accent color selection. */ export function CreateServerModal({ isOpen, onClose }: CreateServerModalProps) { const [name, setName] = useState(""); const [color, setColor] = useState("bg-indigo-500"); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); if (!isOpen) return null; const isValid = name.trim().length !== 0; const canSave = isValid && !isLoading; /** Handles server creation form submission and redirects to the created server's channel. */ const handleSubmit = async (e: React.SubmitEvent) => { e.preventDefault(); if (!name.trim()) return; try { setIsLoading(true); setError(null); const response = await fetch("/api/servers", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, color }), }); if (!response.ok) { throw new Error("Failed to create server."); } const server = await response.json(); setName(""); onClose(); const channelId = server.defaultChannelId || server.channels?.[0]?.id; const targetUrl = channelId ? `/servers/${server.id}/channels/${channelId}` : `/servers/${server.id}`; window.location.href = targetUrl; } catch (err: unknown) { setError(err instanceof Error ? err.message : "Something went wrong."); } finally { setIsLoading(false); } }; return (
e.stopPropagation()} className="bg-surface border border-surface/50 rounded-2xl w-full max-w-md p-6 shadow-2xl relative animate-in fade-in zoom-in-95 duration-150" >

Create Server

Give your new server a name and choose an accent color.

setName(e.target.value)} placeholder="My Awesome Server" disabled={isLoading} className="w-full bg-background border border-surface/80 rounded-xl px-3.5 py-2.5 text-sm text-white placeholder:text-muted focus:outline-none focus:ring-2 focus:ring-accent transition-all" />
{SERVER_COLOR_OPTIONS.map((c) => { const isSelected = color === c; return ( ); })}
{error &&

{error}

}
Cancel Save
); }