"use client"; import { useState, useMemo } from "react"; import { Wrench, LayoutGrid, Sparkles, ChevronRight } from "lucide-react"; import { Sparkline } from "@/components/Sparkline"; import { MONTH_NAMES } from "@/lib/data"; import type { RunningRow } from "@/lib/rows"; import { ZAR, ZAR2 } from "@/lib/utils"; export function RunningView({ rows, palette, monthTotals, }: { rows: RunningRow[]; palette: { income: string; profit: string; loss: string }; monthTotals: number[]; }) { const [view, setView] = useState<"cards" | "grid">("cards"); const [sort, setSort] = useState<"ytd" | "code">("ytd"); const [expanded, setExpanded] = useState(null); const sorted = useMemo(() => { return [...rows].sort((a, b) => sort === "ytd" ? b.ytd - a.ytd : a.code.localeCompare(b.code) ); }, [rows, sort]); const grandTotal = monthTotals.reduce((s, v) => s + v, 0); return ( <>
{rows.filter((r) => r.ytd > 0).length} machines with costs · sorted by {sort === "ytd" ? "total spend" : "code"}
Sort: {[ { id: "ytd", label: "Total" }, { id: "code", label: "Code" }, ].map((s) => ( ))}
{view === "cards" ? ( ) : ( )} ); } function CardsView({ rows, expanded, setExpanded, palette, }: { rows: RunningRow[]; expanded: string | null; setExpanded: (id: string | null) => void; palette: { income: string; profit: string; loss: string }; }) { return (
{rows.map((r) => { const isOpen = expanded === r.id; const hasData = r.ytd > 0; return (
{isOpen && (
{/* Mobile: 3 cols (4 rows × 3 cols = 12 months). md+: 6 cols. lg+: 12 cols. */}
{r.months.map((v, i) => (
{MONTH_NAMES[i]}
0 ? "bg-[#EDAE49]/5 text-[#EDAE49] dark:bg-[#EDAE49]/15" : "text-gray-300 dark:text-text-soft"}`}> {v > 0 ? ZAR2(v) : "—"}
))}
YTD total {ZAR(r.ytd)}
)}
); })}
); } function GridView({ rows, monthTotals, grandTotal, }: { rows: RunningRow[]; monthTotals: number[]; grandTotal: number; }) { return (
{MONTH_NAMES.map((m) => ( ))} {rows.map((r) => ( {r.months.map((v, i) => ( ))} ))} {monthTotals.map((v, i) => ( ))}
Code Machine{m}YTD
{r.code}
{r.name}
0 ? "text-gray-700 dark:text-gray-300" : "text-gray-300 dark:text-text-soft"}`}> {v > 0 ? ZAR2(v) : "—"} 0 ? "text-[#EDAE49]" : "text-gray-300 dark:text-text-soft"}`}> {r.ytd > 0 ? ZAR(r.ytd) : "—"}
Total {v > 0 ? ZAR(v) : "—"} {ZAR(grandTotal)}
); }