import { useState } from "react"; import type { MonthViewModel } from "../../domain/views.ts"; import { monthPieData, type MonthPieMode } from "../monthPie.ts"; import { useMoneyFormat } from "../hooks/useMoneyFormat.ts"; import { Section } from "./Section.tsx"; import { Segmented } from "./Segmented.tsx"; import { useDisplayPreference } from "../hooks/useDisplayPreference.ts"; const COLORS = ["#0d6b60", "#cc8050", "#537ba8", "#9a6bb1", "#9b8b39", "#ca6574", "#4a9b93", "#816eaf"]; const OPTIONS = [ { value: "spending", label: "Spent" }, { value: "allocation", label: "Allocated" }, ] as const; function point(degrees: number): [number, number] { const radians = ((degrees - 90) * Math.PI) / 180; return [100 + 88 * Math.cos(radians), 100 + 88 * Math.sin(radians)]; } function wedge(start: number, end: number): string { const [startX, startY] = point(start); const [endX, endY] = point(end); return `M 100 100 L ${startX} ${startY} A 88 88 0 ${end - start > 180 ? 1 : 0} 1 ${endX} ${endY} Z`; } export function MonthPieShowcase({ view, baseCurrency }: { view: MonthViewModel; baseCurrency: string }) { const [mode, setMode] = useState("spending"); const [visible, setVisible] = useDisplayPreference("budget2:show-month-breakdown", true); const fmt = useMoneyFormat(); const chart = monthPieData(view, mode); let angle = 0; const label = mode === "spending" ? "spending" : "allocation"; return (
{visible && } } > {visible &&
{chart.positiveTotal === 0 ? ( ) : chart.positive.length === 1 ? ( ) : chart.positive.map((item, index) => { const start = angle; angle += (item.amount / chart.positiveTotal) * 360; return ( {`${item.label}: ${fmt.money(item.amount, baseCurrency)}`} ); })}
{mode === "spending" ? "Net spent" : "Total allocated"} {fmt.money(chart.netTotal, baseCurrency)}
{chart.positive.length === 0 ? (

{mode === "spending" ? "No positive spending to chart." : "No positive allocations to chart."}

) : (
    {chart.positive.map((item, index) => (
  1. ))}
)} {chart.adjustments.length > 0 && (

Refunds and negative adjustments are excluded from pie slices.

    {chart.adjustments.map((item) => (
  • {item.label} {fmt.money(item.amount, baseCurrency)}
  • ))}
)}
}
); }