'use client'; /** * AgentHeatmap — Recharts BarChart showing agent activity grouped by day-of-week. * * The API returns daily aggregates (no hour granularity), so this component * aggregates event counts per day-of-week across all agents and renders a * grouped bar chart (one bar per day, Mon–Sun). * * This component is designed to be lazy-loaded via `next/dynamic`. Do NOT * import it directly from a page; use dynamic(() => import('./AgentHeatmap')) * so that recharts stays out of the main bundle. * * @module components/charts/AgentHeatmap */ import React, { useMemo } from 'react'; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from 'recharts'; /** A single activity bucket from the API response. */ export interface AgentActivityBucket { /** The agent's unique identifier. */ agent_id: string; /** Day-of-week as an integer (0 = Sunday … 6 = Saturday). */ dow: number; /** Total event count for this agent on this day-of-week. */ count: number; } /** Props for the AgentHeatmap component. */ export interface AgentHeatmapProps { /** * Array of per-agent, per-day-of-week activity buckets from * `GET /api/analytics/agents/activity`. */ data: AgentActivityBucket[]; } /** Display labels for days of the week, ordered Mon–Sun (dow 1–0). */ const DOW_LABELS: Record = { 0: 'Sun', 1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat', }; /** Ordered day-of-week values for Mon → Sun display. */ const DOW_ORDER = [1, 2, 3, 4, 5, 6, 0]; /** Aggregated count per day-of-week for chart rendering. */ interface DowAggregate { day: string; count: number; } /** * Aggregates raw per-agent activity buckets into per-day-of-week totals * suitable for the bar chart. * * @param data - Raw activity buckets from the API * @returns Array of { day, count } sorted Mon → Sun */ function aggregateByDow(data: AgentActivityBucket[]): DowAggregate[] { const totals: Record = {}; for (const dow of DOW_ORDER) { totals[dow] = 0; } for (const bucket of data) { if (bucket.dow in totals) { totals[bucket.dow] += bucket.count; } } return DOW_ORDER.map((dow) => ({ day: DOW_LABELS[dow], count: totals[dow], })); } /** * Renders a responsive bar chart of agent activity grouped by day-of-week * using recharts. Data is aggregated across all agents. * * @param props - AgentHeatmapProps * @returns JSX element */ export default function AgentHeatmap({ data, }: AgentHeatmapProps): React.ReactElement { const chartData = useMemo(() => aggregateByDow(data), [data]); return ( [value.toLocaleString(), 'Events']} contentStyle={{ borderRadius: '8px', border: '1px solid #e2e8f0', fontSize: '13px', }} /> ); }