import React, { useState, useEffect } from 'react'; import { Modal } from '../ui/Modal'; import { useUIStore } from '../../stores/uiStore'; import { useSpaceStore } from '../../stores/spaceStore'; export function CreateChannelModal() { const [name, setName] = useState(''); const [type, setType] = useState<'text' | 'voice'>('text'); const [topic, setTopic] = useState(''); const [categoryId, setCategoryId] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const activeModal = useUIStore((s) => s.activeModal); const modalData = useUIStore((s) => s.modalData); const closeModal = useUIStore((s) => s.closeModal); const createChannel = useSpaceStore((s) => s.createChannel); const currentSpaceId = useSpaceStore((s) => s.currentSpaceId); const categories = useSpaceStore((s) => s.categories); const isOpen = activeModal === 'createChannel'; // Pre-select category when opened from a category's + button useEffect(() => { if (isOpen && modalData.categoryId) { setCategoryId(modalData.categoryId as string); } }, [isOpen, modalData.categoryId]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (!name.trim()) { setError('Channel name is required'); return; } if (!currentSpaceId) { setError('No space selected'); return; } setIsLoading(true); try { await createChannel(currentSpaceId, name.trim(), type, topic.trim() || undefined, categoryId || undefined); closeModal(); setName(''); setTopic(''); setType('text'); setCategoryId(''); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to create channel'); } finally { setIsLoading(false); } }; return (
{error && (
{error}
)}
{(['text', 'voice'] as const).map((t) => ( ))}
setName(e.target.value)} className="input-standard w-full" placeholder="new-channel" autoFocus />
{type === 'text' && (
setTopic(e.target.value)} className="input-standard w-full" placeholder="What's this channel about?" />
)} {categories.length > 0 && (
)}
); }