Implement Excel-style stock filtering with Smart Filters and Range support

This commit is contained in:
Christian Vidal Wolf
2026-01-27 19:03:42 +01:00
parent 6541f6655a
commit c4213acb3d
4 changed files with 72 additions and 8 deletions
+12 -4
View File
@@ -62,6 +62,7 @@ const App: React.FC = () => {
asin: [], asin: [],
sku: [], sku: [],
title: [], title: [],
stock: [],
}); });
// Handle Data Fetch (Simplified) // Handle Data Fetch (Simplified)
@@ -386,8 +387,8 @@ const App: React.FC = () => {
return metaMap; return metaMap;
}, [rawData]); }, [rawData]);
const filteredData = useMemo(() => filterData(rawData, filters), [rawData, filters]); const filteredData = useMemo(() => filterData(rawData, filters, stockMap), [rawData, filters, stockMap]);
const filteredAdsData = useMemo(() => filterAdsData(adsData, filters, globalAsinMetadata), [adsData, filters, globalAsinMetadata]); const filteredAdsData = useMemo(() => filterAdsData(adsData, filters, globalAsinMetadata, stockMap), [adsData, filters, globalAsinMetadata, stockMap]);
const aggregatedData = useMemo(() => aggregateData(filteredData), [filteredData]); const aggregatedData = useMemo(() => aggregateData(filteredData), [filteredData]);
// Calculate country-aware Top 50 Best Sellers for 2025 // Calculate country-aware Top 50 Best Sellers for 2025
@@ -445,7 +446,8 @@ const App: React.FC = () => {
line: activeLines, // Force these lines line: activeLines, // Force these lines
sku: [], // Clear specific item filters sku: [], // Clear specific item filters
asin: [], asin: [],
title: [] title: [],
stock: []
}; };
// 3. Process this broader dataset // 3. Process this broader dataset
@@ -466,8 +468,14 @@ const App: React.FC = () => {
sku: getUniqueValues(rawData, 'sku'), sku: getUniqueValues(rawData, 'sku'),
title: getUniqueValues(rawData, 'title'), title: getUniqueValues(rawData, 'title'),
week: Array.from(new Set(rawData.map(r => r.week).filter(w => w !== undefined))).sort((a, b) => (a as number) - (b as number)).map(w => `W${w}`), week: Array.from(new Set(rawData.map(r => r.week).filter(w => w !== undefined))).sort((a, b) => (a as number) - (b as number)).map(w => `W${w}`),
stock: [
'Out of Stock (0)',
'In Stock (>0)',
'Low Stock (<10)',
...Array.from(new Set(Array.from(stockMap.values()).map(String))).sort((a, b) => parseFloat(a) - parseFloat(b))
]
}; };
}, [rawData]); }, [rawData, stockMap]);
const handleFilterChange = (key: keyof FilterState, value: string[]) => { const handleFilterChange = (key: keyof FilterState, value: string[]) => {
setFilters(prev => ({ ...prev, [key]: value })); setFilters(prev => ({ ...prev, [key]: value }));
+9
View File
@@ -15,6 +15,7 @@ interface FilterBarProps {
sku: string[]; sku: string[];
title: string[]; title: string[];
week: string[]; week: string[];
stock: string[];
}; };
} }
@@ -79,6 +80,14 @@ const FilterBar: React.FC<FilterBarProps> = ({ filters, onFilterChange, options
onChange={(v) => onFilterChange('asin', v)} onChange={(v) => onFilterChange('asin', v)}
className="flex-1" className="flex-1"
/> />
<MultiSelectDropdown
label="Stock"
selected={filters.stock}
options={options.stock}
onChange={(v) => onFilterChange('stock', v)}
className="flex-1"
enableRangeSelect={true}
/>
</div> </div>
</div> </div>
); );
+50 -4
View File
@@ -701,11 +701,51 @@ export const mergeSalesAndAdsData = (
// --- EXISTING HELPERS --- // --- EXISTING HELPERS ---
// Helper to check stock filter
const checkStockFilter = (sku: string, filters: string[], stockMap?: Map<string, number>): boolean => {
if (!filters || filters.length === 0) return true;
if (!stockMap) return true;
// Normalize SKU (remove DE/EN) to match stock map
const baseSku = sku?.replace(/(DE|EN)$/i, '');
const stockValue = stockMap.get(baseSku) || 0;
return filters.some(f => {
// Smart Filters
if (f === 'Out of Stock (0)') return stockValue === 0;
if (f === 'In Stock (>0)') return stockValue > 0;
if (f === 'Low Stock (<10)') return stockValue < 10;
// Numeric Range (e.g. ">10", "1-50")
const input = f.trim().toLowerCase();
if (input.includes('-')) {
const [start, end] = input.split('-').map(s => parseFloat(s.trim()));
if (!isNaN(start) && !isNaN(end)) return stockValue >= start && stockValue <= end;
} else if (input.startsWith('<=')) {
const val = parseFloat(input.substring(2).trim());
if (!isNaN(val)) return stockValue <= val;
} else if (input.startsWith('>=')) {
const val = parseFloat(input.substring(2).trim());
if (!isNaN(val)) return stockValue >= val;
} else if (input.startsWith('<')) {
const val = parseFloat(input.substring(1).trim());
if (!isNaN(val)) return stockValue < val;
} else if (input.startsWith('>')) {
const val = parseFloat(input.substring(1).trim());
if (!isNaN(val)) return stockValue > val;
}
// Exact Match
return stockValue.toString() === f;
});
};
// Filter Ads Data by Country, Year, Week, ASIN, SKU, and Product Line // Filter Ads Data by Country, Year, Week, ASIN, SKU, and Product Line
export const filterAdsData = ( export const filterAdsData = (
adsData: AdsRecord[], adsData: AdsRecord[],
filters: FilterState, filters: FilterState,
asinMetadata?: Map<string, { sku: string; line: string }> asinMetadata?: Map<string, { sku: string; line: string }>,
stockMap?: Map<string, number>
): AdsRecord[] => { ): AdsRecord[] => {
return adsData.filter(ad => { return adsData.filter(ad => {
const asin = ad.asin.trim().toUpperCase(); const asin = ad.asin.trim().toUpperCase();
@@ -740,11 +780,14 @@ export const filterAdsData = (
lineMatch = meta ? filters.line.includes(meta.line) : false; lineMatch = meta ? filters.line.includes(meta.line) : false;
} }
return countryMatch && yearMatch && weekMatch && asinMatch && skuMatch && lineMatch; // Stock match
const stockMatch = checkStockFilter(meta?.sku || '', filters.stock, stockMap);
return countryMatch && yearMatch && weekMatch && asinMatch && skuMatch && lineMatch && stockMatch;
}); });
}; };
export const filterData = (data: SalesRecord[], filters: FilterState): SalesRecord[] => { export const filterData = (data: SalesRecord[], filters: FilterState, stockMap?: Map<string, number>): SalesRecord[] => {
return data.filter(item => { return data.filter(item => {
// 1. Month Logic: Handle "Apr-23" matching "Apr" filter // 1. Month Logic: Handle "Apr-23" matching "Apr" filter
const recordMonth = item.month; // e.g. "Apr-23" const recordMonth = item.month; // e.g. "Apr-23"
@@ -769,7 +812,10 @@ export const filterData = (data: SalesRecord[], filters: FilterState): SalesReco
const weekStr = item.week ? `W${item.week}` : ''; const weekStr = item.week ? `W${item.week}` : '';
const weekMatch = filters.week.length === 0 || (weekStr !== '' && filters.week.includes(weekStr)); const weekMatch = filters.week.length === 0 || (weekStr !== '' && filters.week.includes(weekStr));
return customerMatch && yearMatch && monthMatch && lineMatch && asinMatch && skuMatch && titleMatch && weekMatch; // Stock match
const stockMatch = checkStockFilter(item.sku, filters.stock, stockMap);
return customerMatch && yearMatch && monthMatch && lineMatch && asinMatch && skuMatch && titleMatch && weekMatch && stockMatch;
}); });
}; };
+1
View File
@@ -23,6 +23,7 @@ export interface FilterState {
sku: string[]; sku: string[];
title: string[]; // Added Title filter title: string[]; // Added Title filter
week: string[]; // Added Week filter week: string[]; // Added Week filter
stock: string[]; // Added Stock filter
} }
export interface GrowthMetric { export interface GrowthMetric {