File size: 6,870 Bytes
84121fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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 (
    <div className="h-full flex flex-col bg-gray-800">
      {/* Búsqueda */}
      <div className="p-4 border-b border-gray-700">
        <div className="relative">
          <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400" />
          <input
            type="text"
            value={searchTerm}
            onChange={(e) => setSearchTerm(e.target.value)}
            placeholder="Buscar canales..."
            className="input-field w-full pl-10 text-sm"
          />
        </div>
      </div>

      {/* Categorías */}
      <div className="p-4 border-b border-gray-700">
        <div className="flex items-center justify-between mb-2">
          <h3 className="text-sm font-medium text-gray-300">Categorías</h3>
          <button
            onClick={refreshChannels}
            disabled={isLoading}
            className="p-1 hover:bg-gray-700 rounded transition-colors"
            title="Actualizar canales"
          >
            <RefreshCw className={`h-4 w-4 text-gray-400 ${isLoading ? 'animate-spin' : ''}`} />
          </button>
        </div>
        
        <div className="space-y-1">
          <button
            onClick={() => handleCategoryChange('all')}
            className={`w-full text-left px-2 py-1 rounded text-sm transition-colors ${
              selectedCategory === 'all' 
                ? 'bg-primary-600 text-white' 
                : 'text-gray-300 hover:bg-gray-700'
            }`}
          >
            Todos ({channels.length})
          </button>
          
          <button
            onClick={() => handleCategoryChange('favorites')}
            className={`w-full text-left px-2 py-1 rounded text-sm transition-colors flex items-center space-x-2 ${
              selectedCategory === 'favorites' 
                ? 'bg-primary-600 text-white' 
                : 'text-gray-300 hover:bg-gray-700'
            }`}
          >
            <Heart className="h-3 w-3" />
            <span>Favoritos ({favorites.length})</span>
          </button>
          
          {categories.map(category => (
            <button
              key={category.name}
              onClick={() => handleCategoryChange(category.name)}
              className={`w-full text-left px-2 py-1 rounded text-sm transition-colors ${
                selectedCategory === category.name 
                  ? 'bg-primary-600 text-white' 
                  : 'text-gray-300 hover:bg-gray-700'
              }`}
            >
              {category.name} ({category.count})
            </button>
          ))}
        </div>
      </div>

      {/* Lista de canales */}
      <div className="flex-1 overflow-y-auto">
        {filteredChannels.length === 0 ? (
          <div className="p-4 text-center text-gray-400">
            <Tv className="h-8 w-8 mx-auto mb-2 opacity-50" />
            <p className="text-sm">No se encontraron canales</p>
          </div>
        ) : (
          <div className="p-2 space-y-1">
            {filteredChannels.map((channel, index) => (
              <div
                key={channel.id}
                onClick={() => handleChannelSelect(channel)}
                className={`channel-item ${
                  currentChannel?.id === channel.id ? 'active' : ''
                } ${index === selectedIndex ? 'ring-2 ring-primary-500' : ''}`}
              >
                <div className="flex-1 min-w-0">
                  <div className="flex items-center space-x-2">
                    {channel.logo && (
                      <img
                        src={channel.logo}
                        alt={channel.name}
                        className="w-8 h-8 rounded object-cover flex-shrink-0"
                        onError={(e) => {
                          e.currentTarget.style.display = 'none'
                        }}
                      />
                    )}
                    <div className="flex-1 min-w-0">
                      <p className="text-sm font-medium text-white truncate">
                        {channel.name}
                      </p>
                      <p className="text-xs text-gray-400 truncate">
                        {channel.category}
                      </p>
                    </div>
                  </div>
                </div>
                
                <button
                  onClick={(e) => {
                    e.stopPropagation()
                    toggleFavorite(channel.id)
                  }}
                  className="p-1 hover:bg-gray-600 rounded transition-colors"
                >
                  <Heart
                    className={`h-4 w-4 ${
                      favorites.includes(channel.id)
                        ? 'text-red-500 fill-current'
                        : 'text-gray-400'
                    }`}
                  />
                </button>
              </div>
            ))}
          </div>
        )}
      </div>
    </div>
  )
}