import { useState, useEffect } from 'react'; import { getHeatmap, type HeatmapEntry } from '../services/attendanceService'; function HeatmapGrid({ data, year }: { data: HeatmapEntry[]; year: number }) { const dayCounts: Record = {}; data.forEach((d) => { dayCounts[d.date] = d.count; }); const startDate = new Date(year, 0, 1); const endDate = new Date(year, 11, 31); const cells: { date: string; count: number; day: number }[] = []; const current = new Date(startDate); while (current <= endDate) { const key = current.toISOString().split('T')[0]; cells.push({ date: key, count: dayCounts[key] || 0, day: current.getDay() }); current.setDate(current.getDate() + 1); } const weeks: typeof cells[] = []; let week: typeof cells = []; cells.forEach((c) => { week.push(c); if (c.day === 6) { weeks.push(week); week = []; } }); if (week.length) weeks.push(week); const getColor = (count: number) => { if (count === 0) return '#ebedf0'; if (count <= 2) return '#9be9a8'; if (count <= 5) return '#40c463'; if (count <= 10) return '#30a14e'; return '#216e39'; }; return (
{weeks.map((w, wi) => (
{Array.from({ length: 7 }).map((_, di) => { const c = w.find((x) => x.day === di); return (
); })}
))}
Menos {[0, 2, 5, 10, 15].map((v) => (
))} Más
); } interface Props { studentId: number; studentName: string; onClose: () => void; } export default function HeatmapModal({ studentId, studentName, onClose }: Props) { const [data, setData] = useState([]); const [year, setYear] = useState(new Date().getFullYear()); const [loading, setLoading] = useState(false); useEffect(() => { setLoading(true); getHeatmap(studentId, year).then(setData).catch(() => setData([])).finally(() => setLoading(false)); }, [studentId, year]); return (
e.stopPropagation()} style={{ maxWidth: 700 }}>

Mapa de Calor: {studentName}

setYear(Number(e.target.value))} className="w-full border border-gray-300 rounded-lg px-3 py-2" style={{ width: 100 }} />
{loading ? (

Cargando...

) : data.length > 0 ? ( ) : (

Sin datos de asistencia para este año

)}
); }