import React from 'react'; import { Sidebar } from '@/components/layout/Sidebar'; import { Header } from '@/components/layout/Header'; import { CreateChannelModal } from '@/components/modals/CreateChannelModal'; import { EditChannelModal } from '@/components/modals/EditChannelModal'; import { ChannelCard } from '@/components/cards/ChannelCard'; import { DeleteChannelConfirmationModal } from '@/components/modals/DeleteChannelConfirmationModal'; import { useChannelQuery, Channel } from '@/hooks/use-query/useChannelQuery'; import { useSearch } from '@/hooks/useSearch.ts'; import '@/styles/cards.css'; export const ChannelsPage = () => { const [isCreateModalOpen, setIsCreateModalOpen] = React.useState(false); const [isEditModalOpen, setIsEditModalOpen] = React.useState(false); const [selectedChannel, setSelectedChannel] = React.useState(null); const [deleteConfirmationOpen, setDeleteConfirmationOpen] = React.useState(false); const [channelToDelete, setChannelToDelete] = React.useState<{ id: string; name: string } | null>(null); const [searchTerm, setSearchTerm] = React.useState(''); const [isSidebarOpen, setIsSidebarOpen] = React.useState(false); const openCreateModal = () => setIsCreateModalOpen(true); const closeCreateModal = () => setIsCreateModalOpen(false); const openEditModal = (channel: Channel) => { setSelectedChannel(channel); setIsEditModalOpen(true); }; const closeEditModal = () => { setSelectedChannel(null); setIsEditModalOpen(false); }; const { channels, deleteChannel, isLoading } = useChannelQuery(); const filteredChannels = useSearch(channels, searchTerm) as Channel[]; const handleDelete = async (channelId: string, channelName: string) => { setChannelToDelete({ id: channelId, name: channelName }); setDeleteConfirmationOpen(true); }; const handleConfirmDelete = async () => { if (channelToDelete) { try { await deleteChannel(channelToDelete.id); setDeleteConfirmationOpen(false); setChannelToDelete(null); } catch (error) { console.error('Error deleting channel:', error); throw error; } } }; return (
setIsSidebarOpen(false)} />
setIsSidebarOpen(true)} /> {isLoading ? (
) : filteredChannels.length === 0 ? (
{searchTerm ? 'No channels found matching your search.' : 'No channels have been created yet.'}
) : (
{filteredChannels.map((channel) => ( openEditModal(channel)} onDelete={() => handleDelete(channel.ID, channel.ChannelName)} /> ))}
)}
{isCreateModalOpen && ( )} {isEditModalOpen && selectedChannel && ( )} {deleteConfirmationOpen && channelToDelete && ( { setDeleteConfirmationOpen(false); setChannelToDelete(null); }} onConfirm={handleConfirmDelete} /> )}
); };