/** * @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 { useRouter } from "next/navigation"; import { X, Loader2 } from "lucide-react"; import { SERVER_COLOR_CLASSES, SERVER_COLOR_OPTIONS, } from "@/lib/constants/server.styles"; /** * Properties for the CreateServerModal component. * * @interface CreateServerModalProps * @property {boolean} isOpen - Indicates whether the modal dialog is currently visible. * @property {() => void} onClose - Callback function to handle closing the modal dialog. */ interface CreateServerModalProps { isOpen: boolean; onClose: () => void; } /** * Renders the modal dialog for creating a new server with custom properties. * * @param {CreateServerModalProps} props - The component props. * @returns {JSX.Element | null} The rendered modal component or null when hidden. */ export function CreateServerModal({ isOpen, onClose }: CreateServerModalProps) { const router = useRouter(); 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; /** * Handles server creation form submission via API POST request. * * @param {React.FormEvent} e - The form submission event instance. * @returns {Promise} Resolves when request is completed or redirects on success. */ const handleSubmit = async (e: React.FormEvent) => { 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 ( /* Outer Backdrop: Schließt das Modal bei Klick */
{/* Inner Modal Content: Verhindert Event-Bubbling, damit Klicks hier drinnen das Modal NICHT schließen */}
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 cursor-default" >

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) => (
{error &&

{error}

}
); }