diff --git a/App.tsx b/App.tsx index 09e8590..97821bd 100644 --- a/App.tsx +++ b/App.tsx @@ -514,26 +514,41 @@ const App: React.FC = () => { }, [rawData]); const ytdGridData = useMemo(() => { - // Filter combinedAdsData to only include weeks <= maxWeek - // This ensures we compare "Like for Like" periods across years - // We do strictly LESS THAN OR EQUAL to maxWeek. + // Smart YTD Logic: + // Only apply the "Max Week" cutoff if the user has NOT explicitly selected a specific time period. + // If Month or Week filters are active, we show exactly what was asked (e.g., Full Year 2024). + // If no time filters are active, we default to YTD (Like-for-Like) comparison. + const isTimeFilterActive = filters.month.length > 0 || filters.week.length > 0; + if (!combinedAdsData) return []; + + if (isTimeFilterActive) { + return combinedAdsData; + } + return combinedAdsData.filter(item => { - // CombinedKPIs has 'week' property - if (!item.week) return true; // Keep non-weekly items if any (shouldn't be) + if (!item.week) return true; return item.week <= gridContext.maxWeek; }); - }, [combinedAdsData, gridContext.maxWeek]); + }, [combinedAdsData, gridContext.maxWeek, filters.month, filters.week]); // Derived Data for Dashboard (YTD Filtered - Isolated) // Dashboard needs aggregated data respecting the max week limit of current year. + // Dashboard needs aggregated data respecting the max week limit of current year... + // UNLESS the user explicitly filters for a period. const ytdFilteredData = useMemo(() => { + const isTimeFilterActive = filters.month.length > 0 || filters.week.length > 0; + if (isTimeFilterActive) return filteredData; + return filteredData.filter(r => r.week <= gridContext.maxWeek); - }, [filteredData, gridContext.maxWeek]); + }, [filteredData, gridContext.maxWeek, filters.month, filters.week]); const ytdFilteredAdsData = useMemo(() => { + const isTimeFilterActive = filters.month.length > 0 || filters.week.length > 0; + if (isTimeFilterActive) return filteredAdsData; + return filteredAdsData.filter(r => r.week <= gridContext.maxWeek); - }, [filteredAdsData, gridContext.maxWeek]); + }, [filteredAdsData, gridContext.maxWeek, filters.month, filters.week]); const ytdAggregatedData = useMemo(() => { return aggregateData(ytdFilteredData);