import React, { useState, useMemo } from 'react' import { useChannels } from '../contexts/ChannelContext' import { useKeyboard } from '../hooks/useKeyboard' import { Search, Heart, Tv, RefreshCw } from 'lucide-react' import { Channel } from '../types/channel' interface ChannelListProps { onChannelSelect?: () => void } export default function ChannelList({ onChannelSelect }: ChannelListProps) { const { channels, categories, currentChannel, favorites, searchTerm, selectedCategory, setCurrentChannel, setSearchTerm, setSelectedCategory, toggleFavorite, refreshChannels, isLoading } = useChannels() const [selectedIndex, setSelectedIndex] = useState(0) const filteredChannels = useMemo(() => { let filtered = channels // Filtrar por categoría if (selectedCategory === 'favorites') { filtered = filtered.filter(channel => favorites.includes(channel.id)) } else if (selectedCategory !== 'all') { filtered = filtered.filter(channel => channel.category === selectedCategory) } // Filtrar por búsqueda if (searchTerm) { filtered = filtered.filter(channel => channel.name.toLowerCase().includes(searchTerm.toLowerCase()) ) } return filtered }, [channels, selectedCategory, favorites, searchTerm]) useKeyboard({ onArrowUp: () => setSelectedIndex(prev => Math.max(0, prev - 1)), onArrowDown: () => setSelectedIndex(prev => Math.min(filteredChannels.length - 1, prev + 1)), onEnter: () => { if (filteredChannels[selectedIndex]) { handleChannelSelect(filteredChannels[selectedIndex]) } } }) const handleChannelSelect = (channel: Channel) => { setCurrentChannel(channel) onChannelSelect?.() } const handleCategoryChange = (category: string) => { setSelectedCategory(category) setSelectedIndex(0) } return (
{/* Búsqueda */}
setSearchTerm(e.target.value)} placeholder="Buscar canales..." className="input-field w-full pl-10 text-sm" />
{/* Categorías */}

Categorías

{categories.map(category => ( ))}
{/* Lista de canales */}
{filteredChannels.length === 0 ? (

No se encontraron canales

) : (
{filteredChannels.map((channel, index) => (
handleChannelSelect(channel)} className={`channel-item ${ currentChannel?.id === channel.id ? 'active' : '' } ${index === selectedIndex ? 'ring-2 ring-primary-500' : ''}`} >
{channel.logo && ( {channel.name} { e.currentTarget.style.display = 'none' }} /> )}

{channel.name}

{channel.category}

))}
)}
) }