import { useEffect, useMemo, useState } from 'react'; import { useServerStore } from '../stores/server.ts'; import { useChannelStore } from '../stores/channel.ts'; import { VoiceChannel } from './VoiceChannel.tsx'; import { CreateChannelModal } from './CreateChannelModal.tsx'; export function ChannelList() { const activeServerId = useServerStore((state) => state.activeServerId); const servers = useServerStore((state) => state.servers); const channelsByServer = useChannelStore((state) => state.channelsByServer); const fetchChannels = useChannelStore((state) => state.fetchChannels); const activeChannelId = useChannelStore((state) => state.activeChannelId); const setActiveChannel = useChannelStore((state) => state.setActiveChannel); const [showCreate, setShowCreate] = useState(false); useEffect(() => { if (activeServerId) { fetchChannels(activeServerId); } }, [activeServerId, fetchChannels]); const channels = useMemo(() => { return activeServerId ? channelsByServer[activeServerId] || [] : []; }, [activeServerId, channelsByServer]); const activeServer = useMemo(() => { if (!activeServerId) return null; return servers.find((s) => s.id === activeServerId) || null; }, [servers, activeServerId]); const categories = useMemo(() => { const map = new Map(); for (const channel of channels) { const category = channel.category || 'TEXT CHANNELS'; const list = map.get(category) || []; list.push(channel); map.set(category, list); } for (const list of map.values()) { list.sort((a, b) => a.position - b.position); } return Array.from(map.entries()); }, [channels]); return (
{activeServerId ? `[SERVER ${activeServer?.name ?? activeServerId}]` : '[NO SERVER]'} {activeServerId && ( )}
{categories.length === 0 && (

[no channels]

)} {categories.map(([category, list]) => (
{category}
---
{list.map((channel) => channel.type === 'voice' ? ( ) : ( ) )}
))}
{showCreate && activeServerId && ( setShowCreate(false)} /> )}
); }