diff --git a/components/WeeklyGrid.tsx b/components/WeeklyGrid.tsx index 6f7123f..8b50292 100644 --- a/components/WeeklyGrid.tsx +++ b/components/WeeklyGrid.tsx @@ -9,9 +9,9 @@ interface WeeklyGridProps { } type SortConfig = { - key: string; // weekKey + key: string; // weekKey or 'rank' direction: 'asc' | 'desc'; - metric: 'units' | 'spend'; + metric: 'units' | 'spend' | 'rank'; } | null; const ROWS_PER_PAGE = 50; @@ -64,12 +64,12 @@ const WeeklyGrid: React.FC = ({ data, top50Ranking }) => { setCurrentPage(1); }, [debouncedSearch, showOnlyTop50]); - const handleSort = useCallback((weekKey: string, metric: 'units' | 'spend') => { + const handleSort = useCallback((weekKey: string, metric: 'units' | 'spend' | 'rank') => { setSortConfig(prev => { if (prev?.key === weekKey && prev.metric === metric) { return { key: weekKey, direction: prev.direction === 'asc' ? 'desc' : 'asc', metric }; } - return { key: weekKey, direction: 'desc', metric }; + return { key: weekKey, direction: metric === 'rank' ? 'asc' : 'desc', metric }; }); }, []); @@ -149,6 +149,11 @@ const WeeklyGrid: React.FC = ({ data, top50Ranking }) => { const direction = sortConfig.direction; result.sort((a, b) => { + if (sortConfig.metric === 'rank') { + const rankA = top50Ranking?.get(a.asin.trim().toUpperCase()) || 999; + const rankB = top50Ranking?.get(b.asin.trim().toUpperCase()) || 999; + return direction === 'asc' ? rankA - rankB : rankB - rankA; + } const valA = a[metricKey][weekKey] || 0; const valB = b[metricKey][weekKey] || 0; return direction === 'asc' ? valA - valB : valB - valA; @@ -248,7 +253,13 @@ const WeeklyGrid: React.FC = ({ data, top50Ranking }) => { {/* Top 50 Filter Toggle */} {top50Ranking && top50Ranking.size > 0 && (