talos-access / frontend /src /components /HeatmapModal.tsx
Greg
Claude Opus 4.7 (1M context)
Preparar despliegue en fly.io: monolito dockerizado + seeder demo
6aa6081
Raw History Blame Contribute Delete
3.94 kB
import { useState, useEffect } from 'react';
import { getHeatmap, type HeatmapEntry } from '../services/attendanceService';
function HeatmapGrid({ data, year }: { data: HeatmapEntry[]; year: number }) {
const dayCounts: Record<string, number> = {};
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 (
<div>
<div className="flex gap-0.5 overflow-x-auto">
{weeks.map((w, wi) => (
<div key={wi} className="flex flex-col gap-0.5">
{Array.from({ length: 7 }).map((_, di) => {
const c = w.find((x) => x.day === di);
return (
<div
key={di}
className="rounded-sm"
style={{ width: 12, height: 12, background: c ? getColor(c.count) : 'transparent' }}
title={c ? `${c.date}: ${c.count} asistencias` : ''}
/>
);
})}
</div>
))}
</div>
<div className="flex items-center gap-1 mt-2 text-xs text-gray-400">
<span>Menos</span>
{[0, 2, 5, 10, 15].map((v) => (
<div key={v} className="rounded-sm" style={{ width: 12, height: 12, background: getColor(v) }} />
))}
<span>Más</span>
</div>
</div>
);
}
interface Props {
studentId: number;
studentName: string;
onClose: () => void;
}
export default function HeatmapModal({ studentId, studentName, onClose }: Props) {
const [data, setData] = useState<HeatmapEntry[]>([]);
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 (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4" onClick={onClose}>
<div className="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-[90vh] overflow-y-auto" onClick={(e) => e.stopPropagation()} style={{ maxWidth: 700 }}>
<div className="sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between rounded-t-2xl">
<h2 className="text-xl font-bold">Mapa de Calor: {studentName}</h2>
<button onClick={onClose} className="text-gray-400 hover:text-gray-600 text-2xl leading-none">&times;</button>
</div>
<div className="p-6 space-y-6">
<div className="flex gap-3 items-end flex-wrap">
<input type="number" value={year} onChange={(e) => setYear(Number(e.target.value))} className="w-full border border-gray-300 rounded-lg px-3 py-2" style={{ width: 100 }} />
</div>
{loading ? (
<p className="text-sm text-gray-400 text-center py-8">Cargando...</p>
) : data.length > 0 ? (
<HeatmapGrid data={data} year={year} />
) : (
<p className="text-sm text-gray-400 text-center py-8">Sin datos de asistencia para este año</p>
)}
</div>
</div>
</div>
);
}