From 0d12ab9a47c5bcd5e50dcb55d41bfd65a00fff91 Mon Sep 17 00:00:00 2001 From: Felix Schulze Date: Thu, 1 May 2025 15:56:01 +0200 Subject: [PATCH] break out functions from export --- src/app/components/FireCalculatorForm.tsx | 58 +++++++++++++---------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/app/components/FireCalculatorForm.tsx b/src/app/components/FireCalculatorForm.tsx index d36f1d9..2ff4824 100644 --- a/src/app/components/FireCalculatorForm.tsx +++ b/src/app/components/FireCalculatorForm.tsx @@ -31,9 +31,14 @@ import { XAxis, YAxis, ReferenceLine, + type TooltipProps, } from "recharts"; import { Slider } from "@/components/ui/slider"; import assert from "assert"; +import type { + NameType, + ValueType, +} from "recharts/types/component/DefaultTooltipContent"; // Schema for form validation const formSchema = z.object({ @@ -79,6 +84,33 @@ interface CalculationResult { error?: string; } +// Helper function to format currency without specific symbols +const formatNumber = (value: number | null) => { + if (!value) return "N/A"; + return new Intl.NumberFormat("en", { + maximumFractionDigits: 0, + }).format(value); +}; + +// Helper function to render tooltip for chart +const tooltipRenderer = ({ + active, + payload, +}: TooltipProps) => { + if (active && payload?.[0]?.payload) { + const data = payload[0].payload as YearlyData; + return ( +
+

{`Year: ${data.year.toString()} (Age: ${data.age.toString()})`}

+

{`Balance: ${formatNumber(data.balance)}`}

+

{`Monthly allowance: ${formatNumber(data.monthlyAllowance)}`}

+

{`Phase: ${data.phase === "accumulation" ? "Accumulation" : "Retirement"}`}

+
+ ); + } + return null; +}; + export default function FireCalculatorForm() { const [result, setResult] = useState(null); const irlYear = new Date().getFullYear(); @@ -176,14 +208,6 @@ export default function FireCalculatorForm() { } } - // Helper function to format currency without specific symbols - const formatNumber = (value: number | null) => { - if (value === null) return "N/A"; - return new Intl.NumberFormat("en", { - maximumFractionDigits: 0, - }).format(value); - }; - return ( <> @@ -398,23 +422,7 @@ export default function FireCalculatorForm() { }} width={25} /> - { - if (active && payload?.[0]?.payload) { - const data = payload[0] - .payload as (typeof result.yearlyData)[0]; - return ( -
-

{`Year: ${data.year.toString()} (Age: ${data.age.toString()})`}

-

{`Balance: ${formatNumber(data.balance)}`}

-

{`Monthly allowance: ${formatNumber(data.monthlyAllowance)}`}

-

{`Phase: ${data.phase === "accumulation" ? "Accumulation" : "Retirement"}`}

-
- ); - } - return null; - }} - /> +