import { useMemo, useState } from "react"; import Link from "next/link"; export default function AppealStatusDashboard({ statusObj = [] }) { const [openSections, setOpenSections] = useState({}); const sortedAppeals = useMemo( () => [...statusObj].sort( (a, b) => (b.caseCount || 0) - (a.caseCount || 0) ), [statusObj] ); const percentageOf = (value, total) => total ? `${(((value || 0) / total) * 100).toFixed(1)}%` : "0.0%"; const totalCases = sortedAppeals.reduce( (sum, appeal) => sum + (appeal.caseCount || 0), 0 ); const largestAppeal = sortedAppeals[0]; const allStatuses = sortedAppeals.flatMap((appeal) => (appeal.statuses || []).map((status) => ({ ...status, appealType: appeal.value })) ); const statusTotals = allStatuses.reduce((acc, status) => { if (!acc[status.statuscode]) { acc[status.statuscode] = { value: status.value, statuscode: status.statuscode, caseCount: 0 }; } acc[status.statuscode].caseCount += status.caseCount || 0; return acc; }, {}); const mostCommonStatus = Object.values(statusTotals).sort( (a, b) => b.caseCount - a.caseCount )[0]; const maxAppealCount = largestAppeal?.caseCount || 1; const allExpanded = sortedAppeals.length > 0 && sortedAppeals.every((_, index) => openSections[index]); const toggleSection = (index) => { setOpenSections((current) => ({ ...current, [index]: !current[index] })); }; const showAll = () => { const allOpen = {}; sortedAppeals.forEach((_, index) => { allOpen[index] = true; }); setOpenSections(allOpen); }; const hideAll = () => { setOpenSections({}); }; return (

Appeal case status dashboard

Case counts and stage breakdown by appeal type

Select an appeal type row to show the case stage breakdown.

{sortedAppeals.map((appeal, index) => { const isOpen = !!openSections[index]; const barWidth = ((appeal.caseCount || 0) / maxAppealCount) * 100; const sortedStatuses = [...(appeal.statuses || [])] .filter((status) => (status.caseCount || 0) > 0) .sort( (a, b) => (b.caseCount || 0) - (a.caseCount || 0) ); return (

); })}
); } function DashboardCard({ title, value, caption }) { return (

{title}

{value}

{caption && (

{caption}

)}
); }