import { useMemo } from 'react' import { PieChart, Pie, Cell, ResponsiveContainer, Tooltip } from 'recharts' import { useContextSummary } from '../../api/context' import { CATEGORY_COLORS } from '../../lib/constants' interface CategoryBreakdownProps { minutes: number taskId?: number } interface CategoryData { name: string count: number color: string percentage: number } export default function CategoryBreakdown({ minutes: _minutes, taskId: _taskId }: CategoryBreakdownProps) { const { data: summary, isLoading } = useContextSummary() const categories = useMemo(() => { if (!summary) return [] // Aggregate by category const catMap = new Map() for (const item of summary) { const cat = item.category || 'Unknown' catMap.set(cat, (catMap.get(cat) ?? 0) + item.eventCount) } const total = Array.from(catMap.values()).reduce((s, c) => s + c, 0) const result: CategoryData[] = Array.from(catMap.entries()) .map(([name, count]) => ({ name, count, color: CATEGORY_COLORS[name] ?? CATEGORY_COLORS['Unknown'], percentage: total > 0 ? Math.round((count / total) * 100) : 0, })) .sort((a, b) => b.count - a.count) return result }, [summary]) if (isLoading) { return (
Loading category breakdown...
) } if (categories.length === 0) { return (
No category data available.
) } const totalEvents = categories.reduce((s, c) => s + c.count, 0) return (
{/* Left: Donut chart */}
{categories.map((entry, index) => ( ))} { if (!active || !payload || payload.length === 0) return null const d = payload[0].payload as CategoryData return (
{d.name}
{d.count} events ({d.percentage}%)
) }} />
{/* Right: Legend list */}
{categories.map((cat) => (
{/* Colored dot */} {/* Name + bar + stats */}
{cat.name} {cat.count} ({cat.percentage}%)
{/* Progress bar */}
0 ? (cat.count / totalEvents) * 100 : 0}%`, backgroundColor: cat.color, }} />
))}
) }