import React, { useState, useEffect } from 'react'; import { useRemoteControl } from '../hooks/useRemoteControl'; import { fetchChannels } from '../api'; export default function Guide({ onClose, onSelectChannel }) { const [channels, setChannels] = useState([]); const [selectedIndex, setSelectedIndex] = useState(0); useRemoteControl({ onUp: () => setSelectedIndex(prev => (prev - 1 + channels.length) % channels.length), onDown: () => setSelectedIndex(prev => (prev + 1) % channels.length), onSelect: () => onSelectChannel(channels[selectedIndex].id), onBack: onClose }); const [currentTime, setCurrentTime] = useState(new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })); useEffect(() => { fetchChannels().then(data => { // Map channels securely, providing a fallback block if properties are missing const mapped = data.map(ch => ({ ...ch, currentlyPlaying: ch.currentlyPlaying || { title: 'Live Broadcast', time: 'Now Playing' } })); if (mapped.length > 0) { setChannels(mapped); } else { setChannels([{id: 99, channel_number: '99', name: 'No Channels Found', currentlyPlaying: {title: 'Empty Database', time: '--'}}]); } }).catch(err => { console.error(err); setChannels([{id: 99, channel_number: '99', name: 'Network Error', currentlyPlaying: {title: 'Could not reach PyTV server', time: '--'}}]); }); }, []); useEffect(() => { const timer = setInterval(() => { setCurrentTime(new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })); }, 60000); return () => clearInterval(timer); }, []); return (

PYTV Guide

{currentTime}
{channels.length === 0 ?

Loading TV Guide...

: channels.map((chan, idx) => (
{chan.channel_number}
{chan.name} - {chan.currentlyPlaying.title}
{chan.currentlyPlaying.time}
))}
Press Enter to tune to the selected channel. Press Escape to exit guide.
); }