/** * @file components/sidebar/ChannelSidebar.tsx * @description Channel sidebar component rendering categories, channels, and modals for creation and editing. */ "use client"; import { useState } from "react"; import { useParams } from "next/navigation"; import { useActiveServer } from "@/lib/context/ServerContext"; import { useSidebarStore } from "@/lib/stores/useSidebarStore"; import { CreateChannelModal } from "@/components/modals/CreateChannelModal"; import { CreateCategoryModal } from "@/components/modals/CreateCategoryModal"; import { EditChannelModal } from "@/components/modals/EditChannelModal"; import { EditCategoryModal } from "@/components/modals/EditCategoryModal"; import { CategorySection } from "./CategorySection"; import { FolderPlus, PanelLeftClose } from "lucide-react"; import type { Category, Channel } from "@/db/schema"; /** Renders the channel sidebar navigation for the active server. */ export function ChannelSidebar() { const [isCreateChannelOpen, setIsCreateChannelOpen] = useState(false); const [isCreateCategoryOpen, setIsCreateCategoryOpen] = useState(false); const [editingChannel, setEditingChannel] = useState(null); const [editingCategory, setEditingCategory] = useState(null); const [selectedCategoryId, setSelectedCategoryId] = useState( null, ); const params = useParams(); const currentChannelId = params?.channelId as string; const { activeServer } = useActiveServer(); const { closeNav, toggleNav } = useSidebarStore(); if (!activeServer) return null; /** Opens the create channel modal for an optional category. */ const openCreateChannel = (catId: string | null = null) => { setSelectedCategoryId(catId); setIsCreateChannelOpen(true); }; const uncategorizedChannels = activeServer.channels.filter( (c) => !c.categoryId, ); const categories = (activeServer.categories || []).map((cat) => ({ ...cat, channels: activeServer.channels.filter((c) => c.categoryId === cat.id), })); return ( <>
{/* Header */}
{activeServer.name}
{/* Content */}
openCreateChannel(null)} onEditChannel={setEditingChannel} /> {categories.map((category) => ( openCreateChannel(category.id)} onEditChannel={setEditingChannel} onEditCategory={() => setEditingCategory(category)} /> ))}
{/* Modals */} setIsCreateCategoryOpen(false)} serverId={activeServer.id} /> setEditingCategory(null)} category={editingCategory} /> setIsCreateChannelOpen(false)} serverId={activeServer.id} defaultCategoryId={selectedCategoryId} /> setEditingChannel(null)} channel={editingChannel} /> ); }