import { useEffect, useState } from "react"; import { Dialog } from "@/components/ui/Dialog"; import { Button } from "@/components/ui/Button"; import { Input, Label, Select } from "@/components/ui/Input"; import { useAddAssetValuation, useCreateAsset, useUpdateAsset } from "@/hooks/useAssets"; import { useTranslation, type TranslationKey } from "@/lib/i18n"; import type { Asset, AssetClass, CapitalRole, RiskLevel } from "@/types"; interface AssetFormModalProps { open: boolean; onClose: () => void; asset?: Asset | null; } const ASSET_CLASSES: AssetClass[] = ["investments", "crypto", "real_estate", "vehicles", "precious_metals", "other"]; const CAPITAL_ROLES: CapitalRole[] = ["income", "neutral", "drain"]; const RISK_LEVELS: RiskLevel[] = ["low", "medium", "high"]; function todayIso() { return new Date().toISOString().slice(0, 10); } const EMPTY_FORM = { name: "", asset_class: "investments" as AssetClass, value: "", as_of_date: todayIso(), notes: "", capital_role: "neutral" as CapitalRole, monthly_cash_flow: "", risk_level: "medium" as RiskLevel, }; export function AssetFormModal({ open, onClose, asset }: AssetFormModalProps) { const { t } = useTranslation(); const createAsset = useCreateAsset(); const updateAsset = useUpdateAsset(); const addValuation = useAddAssetValuation(); const [form, setForm] = useState(EMPTY_FORM); const [error, setError] = useState(null); useEffect(() => { if (!open) return; if (asset) { setForm({ name: asset.name, asset_class: asset.asset_class, value: asset.current_value, as_of_date: todayIso(), notes: asset.notes ?? "", capital_role: asset.capital_role, monthly_cash_flow: asset.monthly_cash_flow ?? "", risk_level: asset.risk_level, }); } else { setForm(EMPTY_FORM); } setError(null); }, [open, asset]); const isSaving = createAsset.isPending || updateAsset.isPending || addValuation.isPending; async function handleSubmit(event: React.FormEvent) { event.preventDefault(); setError(null); try { if (asset) { await updateAsset.mutateAsync({ id: asset.id, input: { name: form.name, asset_class: form.asset_class, notes: form.notes || null, capital_role: form.capital_role, monthly_cash_flow: form.monthly_cash_flow || null, risk_level: form.risk_level, }, }); if (form.value !== asset.current_value) { await addValuation.mutateAsync({ id: asset.id, input: { value: form.value, as_of_date: form.as_of_date } }); } } else { await createAsset.mutateAsync({ name: form.name, asset_class: form.asset_class, value: form.value, as_of_date: form.as_of_date, notes: form.notes || null, capital_role: form.capital_role, monthly_cash_flow: form.monthly_cash_flow || null, risk_level: form.risk_level, }); } onClose(); } catch { setError(t("netWorth.form.saveError")); } } return (
setForm((prev) => ({ ...prev, name: event.target.value }))} />
setForm((prev) => ({ ...prev, value: event.target.value }))} />
setForm((prev) => ({ ...prev, as_of_date: event.target.value }))} />

{t(`netWorth.capitalRoleFormHint.${form.capital_role}` as TranslationKey)}

{t(`netWorth.riskLevelFormHint.${form.risk_level}` as TranslationKey)}

setForm((prev) => ({ ...prev, monthly_cash_flow: event.target.value }))} />

{t("netWorth.form.cashFlowHint")}

setForm((prev) => ({ ...prev, notes: event.target.value }))} />
{error &&

{error}

}
); }