'use client' import { useEffect, useState } from 'react' import { isSuppressed, type HeatmapData } from '@/lib/dashboard-types' import { queries } from '@/lib/dashboard-queries' import type { DashboardFilters } from '@/lib/dashboard-types' import { WidgetShell } from './widget-shell' import { HORAS_ORDER, AUSENCIAS_ORDER } from '@/lib/dashboard-utils' const SHORT_AUSENCIAS = ['Nunca','Pocas (1-3)','Varias (4-10)','Con freq.'] export function HeatmapWidget({ surveyId, filters }: { surveyId: string; filters: DashboardFilters }) { const [data, setData] = useState(null) const [sup, setSup] = useState(false) useEffect(() => { setData(null); setSup(false) queries.heatmap(surveyId, filters) .then(d => { if (isSuppressed(d)) setSup(true); else setData(d as HeatmapData) }) .catch(() => {}) }, [surveyId, JSON.stringify(filters)]) const status = !data && !sup ? 'loading' : sup ? 'suppressed' : 'ok' function cellColor(n: number | null): string { if (n === null) return '#1e2d42' const max = Math.max(...(data?.data.map(c => c.n ?? 0) ?? [1]), 1) const t = n / max return `rgba(67, 187, 200, ${0.15 + t * 0.85})` } const cellMap = new Map((data?.data ?? []).map(c => [`${c.horas}|${c.ausencias}`, c])) return ( {data && (
{/* Header row */}
{SHORT_AUSENCIAS.map(h => (
{h}
))} {/* Data rows */} {HORAS_ORDER.map(horas => ( <>
{horas}
{AUSENCIAS_ORDER.map(aus => { const cell = cellMap.get(`${horas}|${aus}`) const n = cell?.n ?? null const sup2 = cell?.suppressed ?? (!cell) return (
{!sup2 && n !== null && {n}}
) })} ))}
)} ) }