'use client'; import { useEffect, useState, useRef } from 'react'; export default function ProgrammingSchedule({ matrixData }: { matrixData: Record }) { const [currentTime, setCurrentTime] = useState(new Date()); const [viewMode, setViewMode] = useState<'day' | 'week'>('day'); const containerRef = useRef(null); useEffect(() => { // Actualizar cada minuto cada que cambie la hora const interval = setInterval(() => setCurrentTime(new Date()), 60000); return () => clearInterval(interval); }, []); const allDays = [ { key: 'mon', label: 'Lunes', index: 1 }, { key: 'tue', label: 'Martes', index: 2 }, { key: 'wed', label: 'Miércoles', index: 3 }, { key: 'thu', label: 'Jueves', index: 4 }, { key: 'fri', label: 'Viernes', index: 5 }, { key: 'sat', label: 'Sábado', index: 6 }, { key: 'sun', label: 'Domingo', index: 0 } ]; const currentDayIndex = currentTime.getDay(); // Si estamos en modo día, solo filtramos el día actual const days = viewMode === 'day' ? allDays.filter(d => d.index === currentDayIndex) : allDays; const slots = Array.from({length: 48}).map((_, i) => { const h = Math.floor(i/2).toString().padStart(2, '0'); const m = i % 2 === 0 ? '00' : '30'; return `${h}:${m}`; }); const currentHour = currentTime.getHours(); const currentMinute = currentTime.getMinutes(); const currentSlotIndex = currentHour * 2 + (currentMinute >= 30 ? 1 : 0); // Calc rowSpans to unify cells visually const spans: Record = {}; allDays.forEach(d => { const list = slots.map(time => matrixData[`${time}_${d.key}`] || ''); const spanList = new Array(48).fill(1); for(let i=0; i<48; i++){ if (spanList[i] === 0) continue; let count = 1; const val = list[i]; while(i + count < 48 && list[i + count] === val && val !== '') { spanList[i + count] = 0; count++; } spanList[i] = count; } spans[d.key] = spanList; }); // Auto-scroll a la hora actual al cargar useEffect(() => { if (containerRef.current) { const rowHeight = 40; // approx const scrollAmnt = Math.max(0, (currentSlotIndex - 3) * rowHeight); containerRef.current.scrollTop = scrollAmnt; } }, [currentSlotIndex, viewMode]); return (

Parrilla de Programación

{/* Switch Futurista Día / Semana */}
{days.map(d => { const isToday = d.index === currentDayIndex; return ( ) })} {slots.map((time, idx) => { const isHalfHour = time.endsWith('30'); const isCurrentSlot = idx === currentSlotIndex; return ( {/* Indicador de Línea de Tiempo Roja */} {isCurrentSlot && ( )} {days.map(d => { const span = spans[d.key][idx]; if (span === 0) return null; // Es parte de un bloque unificado, no se renderiza const program = matrixData[`${time}_${d.key}`]; const isToday = d.index === currentDayIndex; // Es en vivo si la ranura actual recae DENTRO del span unificado const isLiveNow = isToday && currentSlotIndex >= idx && currentSlotIndex < idx + span; return ( ) })} ) })}
Hora {d.label} {isToday && viewMode === 'week' &&
HOY
}
{time} {isLiveNow && (
Al Aire
)}
{program || '-'} {span > 1 && program && ({span * 30} MINS)}
); }