import React, { useState } from "react"; import { differenceInDays } from "date-fns"; import { cn } from "@/lib/utils"; import { formatCurrency } from "@/lib/format-currency"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "./dialog"; import { Button } from "./button"; import { DatePicker } from "./date-picker"; import { ToggleGroup, ToggleGroupItem } from "./toggle-group"; import { MoneyInputWithSlider } from "./money-input-with-slider"; type GoalPeriod = "90d" | "180d" | "360d" | "custom"; const PERIOD_OPTIONS: { id: GoalPeriod; label: string; days: number }[] = [ { id: "90d", label: "90 Days", days: 90 }, { id: "180d", label: "180 Days", days: 180 }, { id: "360d", label: "360 Days", days: 360 }, { id: "custom", label: "Custom", days: 0 }, ]; const PERIOD_DAYS: Partial> = Object.fromEntries( PERIOD_OPTIONS.map((p) => [p.id, p.days]), ); function SummaryRow({ label, value }: { label: string; value: number }) { return (
{label} {formatCurrency(value)}
); } export interface SavingsGoalModalProps { open: boolean; onOpenChange: (open: boolean) => void; /** Current monthly savings trend (pre-calculated) in dollars */ currentMonthlyTrend?: number; /** Initial goal amount in dollars */ defaultGoalAmount?: number; /** Called when user confirms the goal */ onSave?: (goalAmount: number, periodDays: number) => void; className?: string; } export function SavingsGoalModal({ open, onOpenChange, currentMonthlyTrend = 0, defaultGoalAmount = 0, onSave, className, }: SavingsGoalModalProps) { const [goalAmount, setGoalAmount] = useState(defaultGoalAmount); const [selectedPeriod, setSelectedPeriod] = useState("90d"); const [customStart, setCustomStart] = useState(undefined); const [customEnd, setCustomEnd] = useState(undefined); const isCustom = selectedPeriod === "custom"; const today = new Date(); const periodDays = isCustom ? customStart && customEnd && customEnd > customStart ? differenceInDays(customEnd, customStart) : 0 : (PERIOD_DAYS[selectedPeriod] ?? 90); const monthlyTarget = periodDays > 0 ? Math.round((goalAmount / periodDays) * 30) : 0; const canSave = goalAmount > 0 && (!isCustom || periodDays > 0); const handleSave = () => { onSave?.(goalAmount, periodDays); onOpenChange(false); }; return ( Savings Target Setter

End Goal Date

{ const next = values[0] as GoalPeriod | undefined; if (next) setSelectedPeriod(next); }} > {PERIOD_OPTIONS.map((option) => ( {option.label} ))} {isCustom && (

Start Date

End Date

)}
); }