import React, { useState, useRef } from 'react'; export default function CreateServerModal({ onClose, onCreated, token }) { const [name, setName] = useState(''); const [icon, setIcon] = useState(null); const [iconPreview, setIconPreview] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const fileRef = useRef(null); const handleIconChange = (e) => { const file = e.target.files?.[0]; if (file) { setIcon(file); const reader = new FileReader(); reader.onloadend = () => setIconPreview(reader.result); reader.readAsDataURL(file); } }; const handleCreate = async (e) => { e.preventDefault(); if (!name.trim()) { setError('Server name is required'); return; } setLoading(true); setError(''); try { // Upload icon first if provided let iconUrl = null; if (icon) { const form = new FormData(); form.append('file', icon); const uploadRes = await fetch('/api/upload', { method: 'POST', headers: { 'Authorization': `Bearer ${token}` }, body: form, }); if (uploadRes.ok) { const uploadData = await uploadRes.json(); iconUrl = uploadData.url; } } const body = { name: name.trim() }; if (iconUrl) body.icon = iconUrl; const res = await fetch('/api/servers', { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error || 'Failed to create server'); } const data = await res.json(); onCreated(data.server || data); onClose(); } catch (err) { setError(err.message); } finally { setLoading(false); } }; return (
e.stopPropagation()}>

Create Your Server

{/* Icon upload */}
{/* Server name */}
setName(e.target.value)} placeholder="My Awesome Server" className="w-full bg-[#111114] border border-[#3a3a42] rounded-lg px-4 py-2.5 text-gray-100 placeholder-gray-500 outline-none focus:border-[#FFD700]/50 transition-colors duration-200" autoFocus maxLength={100} />
{error &&

{error}

} {/* Buttons */}
); }