Fix ReferenceError crash: Move velocityMap calculation to top of App component

This commit is contained in:
Christian Vidal Wolf
2026-01-28 11:39:17 +01:00
parent d286b848a0
commit 2affd868db
2 changed files with 23 additions and 20 deletions
+22 -19
View File
@@ -67,6 +67,27 @@ const App: React.FC = () => {
vendorStock: [], vendorStock: [],
}); });
const top50Mode = useMemo(() => {
const hasUK = filters.customer.includes('Amazon UK');
const hasEU = filters.customer.some(c => ['Amazon DE', 'Amazon IT', 'Amazon FR', 'Amazon ES'].includes(c));
if (hasUK) return 'uk';
return 'eu';
}, [filters.customer]);
// Calculate 4-week Sales Velocity Map (Context-Aware)
// MOVED HERE TO AVOID REFERENCE ERROR in handleForecastFetch
const velocityMap = useMemo(() => {
// Determine which dataset to use for velocity calculation based on region filter
// If 'uk', use UK data. If 'eu', use EU data.
// IMPOTANT: We do NOT filter by Week/Month here, so we get the full history for velocity calculation
const regionData = rawData.filter(r => {
if (top50Mode === 'uk') return r.customer.toLowerCase().includes('uk');
// for EU, exclude UK
return !r.customer.toLowerCase().includes('uk');
});
return calculateVelocityMap(regionData);
}, [rawData, top50Mode]);
// Handle Data Fetch (Simplified) // Handle Data Fetch (Simplified)
const handleDataFetch = useCallback(async () => { const handleDataFetch = useCallback(async () => {
setSyncing(true); setSyncing(true);
@@ -407,13 +428,6 @@ const App: React.FC = () => {
return metaMap; return metaMap;
}, [rawData]); }, [rawData]);
const top50Mode = useMemo(() => {
const hasUK = filters.customer.includes('Amazon UK');
const hasEU = filters.customer.some(c => ['Amazon DE', 'Amazon IT', 'Amazon FR', 'Amazon ES'].includes(c));
if (hasUK) return 'uk';
return 'eu';
}, [filters.customer]);
const filteredData = useMemo(() => filterData(rawData, filters, stockMap, vendorStockMap, top50Mode), [rawData, filters, stockMap, vendorStockMap, top50Mode]); const filteredData = useMemo(() => filterData(rawData, filters, stockMap, vendorStockMap, top50Mode), [rawData, filters, stockMap, vendorStockMap, top50Mode]);
const filteredAdsData = useMemo(() => filterAdsData(adsData, filters, globalAsinMetadata, stockMap, vendorStockMap, top50Mode), [adsData, filters, globalAsinMetadata, stockMap, vendorStockMap, top50Mode]); const filteredAdsData = useMemo(() => filterAdsData(adsData, filters, globalAsinMetadata, stockMap, vendorStockMap, top50Mode), [adsData, filters, globalAsinMetadata, stockMap, vendorStockMap, top50Mode]);
const aggregatedData = useMemo(() => aggregateData(filteredData), [filteredData]); const aggregatedData = useMemo(() => aggregateData(filteredData), [filteredData]);
@@ -450,18 +464,7 @@ const App: React.FC = () => {
}; };
}, [rawData]); }, [rawData]);
// Calculate 4-week Sales Velocity Map (Context-Aware) // Determine which dataset to use for velocity calculation based on top50Mode
const velocityMap = useMemo(() => {
// Determine which dataset to use for velocity calculation based on top50Mode (which reflects region filter)
// If 'uk', use UK data. If 'eu', use EU data.
// IMPOTANT: We do NOT filter by Week/Month here, so we get the full history for velocity calculation
const regionData = rawData.filter(r => {
if (top50Mode === 'uk') return r.customer.toLowerCase().includes('uk');
// for EU, exclude UK
return !r.customer.toLowerCase().includes('uk');
});
return calculateVelocityMap(regionData);
}, [rawData, top50Mode]);
// Combine Sales & Ads Data dynamically based on current filters // Combine Sales & Ads Data dynamically based on current filters
const combinedAdsData = useMemo(() => { const combinedAdsData = useMemo(() => {
+1 -1
View File
@@ -73,7 +73,7 @@ const ForecastRow: React.FC<{
{stockMap && ( {stockMap && (
<StockBadge stock={stockMap.get(item.sku?.replace(/(DE|EN)$/i, ''))} /> <StockBadge stock={stockMap.get(item.sku?.replace(/(DE|EN)$/i, ''))} />
)} )}
{/* <VendorStockBadge asin={item.asin} vendorStockMap={vendorStockMap} mode={top50Mode} avgWeeklySales={item.avgWeeklySales} /> */} <VendorStockBadge asin={item.asin} vendorStockMap={vendorStockMap} mode={top50Mode} avgWeeklySales={item.avgWeeklySales} />
</div> </div>
</div> </div>
</td> </td>