'use client' import { useEffect, useState } from 'react' import { isSuppressed, type KpiData, type SuppressedPayload } from '@/lib/dashboard-types' import { queries } from '@/lib/dashboard-queries' import type { DashboardFilters } from '@/lib/dashboard-types' const KPI_DEFS = [ { title: 'CUIDADORES ACTIVOS', subtitle: '1.6 + 1.13 · % de la plantilla con responsabilidades de cuidado de otros', getValue: (d: KpiData) => `${d.pct_cuidadores}%`, getN: (d: KpiData) => `n = ${d.n}`, }, { title: 'FRECUENCIA DE AUSENTISMO', subtitle: '5.2 · % con ausencias laborales recurrentes (4 o más veces al año)', getValue: (d: KpiData) => `${d.pct_ausencias}%`, getN: () => 'ausencias frecuentes', }, { title: 'BIENESTAR', subtitle: '4.3 · autorreporte de bienestar general · escala 1–5', getValue: (d: KpiData) => d.score_bienestar !== null ? d.score_bienestar.toFixed(1) : '—', getN: () => '/ 5', }, ] as const interface KpiCardProps { title: string subtitle: string value: string valueNote: string suppressed: boolean loading: boolean } function KpiCard({ title, subtitle, value, valueNote, suppressed, loading }: KpiCardProps) { return (
{title} {subtitle} {loading && (
)} {!loading && suppressed && (
n < 5
)} {!loading && !suppressed && ( <> {value} {valueNote} )}
) } interface Props { surveyId: string; filters: DashboardFilters } export function KpiCards({ surveyId, filters }: Props) { const [data, setData] = useState(null) const [error, setError] = useState(false) useEffect(() => { setData(null); setError(false) queries.kpis(surveyId, filters) .then(d => setData(d as KpiData | SuppressedPayload)) .catch(() => setError(true)) }, [surveyId, JSON.stringify(filters)]) if (error) return

Error al cargar KPIs

const loading = data === null const suppressed = !loading && isSuppressed(data) const ok = !loading && !suppressed return (
{KPI_DEFS.map((def) => ( ))}
) }