From 898b540b3f47b455115f5c9a65aa93c334e565d8 Mon Sep 17 00:00:00 2001 From: Carlos Escalante Date: Wed, 1 Apr 2026 10:06:08 -0600 Subject: [PATCH] Fix roiEarned crash when chart data has fewer than 12 entries Co-Authored-By: Claude Opus 4.6 (1M context) --- frontend/src/pages/Pensions.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/frontend/src/pages/Pensions.tsx b/frontend/src/pages/Pensions.tsx index 8a9bfe7..29d3043 100644 --- a/frontend/src/pages/Pensions.tsx +++ b/frontend/src/pages/Pensions.tsx @@ -324,15 +324,18 @@ export default function Pensions() { // Use real rendimientos from the API acc[key] = Math.round(snap.rendimientos); } else { - // Fallback: approximate from hardcoded data + // Fallback: approximate from chart data const fund = FUNDS[key]; - if (fund.isDividend) { + const len = chartData.length; + if (len >= 2 && fund.isDividend) { acc[key] = Math.max(0, Math.round( - chartData[11][key] - chartData[10][key] - fund.monthlyContribution, + chartData[len - 1][key] - chartData[len - 2][key] - fund.monthlyContribution, )); - } else { + } else if (len > 0) { const activeMonths = chartData.filter((d) => d[key] > 0).length; acc[key] = Math.round(fund.startBalance * (fund.annualRate / 100) * (activeMonths / 12)); + } else { + acc[key] = 0; } } return acc;