fix: replace Math.max(...spread) with loop to prevent call stack overflow

Math.max(...largeArray) crashes when combinedAdsData has thousands of
records. Replaced with a simple for-loop to find the max year safely.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Christian Vidal Wolf
2026-03-09 11:03:56 +01:00
co-authored by Claude Sonnet 4.6
parent 3e3975cd2b
commit f1e11ca79a
+6 -3
View File
@@ -65,10 +65,13 @@ export const BSRUnitsCorrelationChart: React.FC<Props> = ({ bsrData, combinedSal
? activeMarket ? activeMarket
: (availableMarkets[0] ?? 'Amazon DE'); : (availableMarkets[0] ?? 'Amazon DE');
// Current year from sales data // Current year from sales data — use reduce to avoid call stack overflow with large arrays
const currentYear = useMemo(() => { const currentYear = useMemo(() => {
const years = combinedSalesData.map(r => r.year).filter(Boolean); let max = 0;
return years.length > 0 ? Math.max(...years) : new Date().getFullYear(); for (const r of combinedSalesData) {
if (r.year && r.year > max) max = r.year;
}
return max > 0 ? max : new Date().getFullYear();
}, [combinedSalesData]); }, [combinedSalesData]);
// Per-market chart data: BSR avg + units sum aligned by week // Per-market chart data: BSR avg + units sum aligned by week