|
1 | 1 | import { useMemo } from 'react'; |
2 | 2 | import type { WorkoutSet } from '../../../types'; |
3 | 3 | import { computationCache } from '../../../utils/storage/computationCache'; |
4 | | -import { computeWeeklySetsDashboardData, type WeeklySetsWindow } from '../../../utils/muscle/analytics'; |
| 4 | +import { |
| 5 | + computeWeeklySetsDashboardData, |
| 6 | + type WeeklySetsDashboardResult, |
| 7 | + type WeeklySetsWindow, |
| 8 | +} from '../../../utils/muscle/analytics'; |
5 | 9 | import { toHeadlessVolumeMap } from '../../../utils/muscle/mapping'; |
6 | 10 | import { MUSCLE_GROUP_ORDER, SVG_TO_MUSCLE_GROUP, getHeadlessRadarSeries } from '../../../utils/muscle/mapping'; |
7 | 11 | import type { NormalizedMuscleGroup } from '../../../utils/muscle/analytics'; |
@@ -30,30 +34,49 @@ export const useMuscleHeatmapData = ({ |
30 | 34 | filterCacheKey, |
31 | 35 | secondarySetMultiplier, |
32 | 36 | }: UseMuscleHeatmapDataParams) => { |
| 37 | + const getWeeklySetsDashboard = ( |
| 38 | + cacheKey: string, |
| 39 | + window: WeeklySetsWindow, |
| 40 | + grouping: 'muscles' | 'groups' |
| 41 | + ): WeeklySetsDashboardResult => { |
| 42 | + const compute = () => |
| 43 | + computeWeeklySetsDashboardData(data, assetsMap!, effectiveNow, window, grouping, secondarySetMultiplier); |
| 44 | + |
| 45 | + const cached = computationCache.getOrCompute<WeeklySetsDashboardResult>( |
| 46 | + cacheKey, |
| 47 | + data, |
| 48 | + compute, |
| 49 | + { ttl: 10 * 60 * 1000 } |
| 50 | + ); |
| 51 | + |
| 52 | + // Older dashboard cache entries stored a heatmap-only shape under this key. |
| 53 | + // If we encounter one, force a recompute so tooltip rates are available. |
| 54 | + if (!(cached.weeklyRatesBySubject instanceof Map)) { |
| 55 | + return computationCache.getOrCompute<WeeklySetsDashboardResult>( |
| 56 | + cacheKey, |
| 57 | + data, |
| 58 | + compute, |
| 59 | + { ttl: 10 * 60 * 1000, forceRecompute: true } |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return cached; |
| 64 | + }; |
| 65 | + |
33 | 66 | const weeklySetsDashboardMuscles = useMemo(() => { |
34 | 67 | if (!assetsMap) return null; |
35 | 68 |
|
36 | 69 | const window: WeeklySetsWindow = weeklySetsWindow === 'all' ? 'all' : weeklySetsWindow; |
37 | 70 | const cacheKey = muscleCacheKeys.weeklySets(filterCacheKey, window, 'muscles', secondarySetMultiplier); |
38 | | - return computationCache.getOrCompute( |
39 | | - cacheKey, |
40 | | - data, |
41 | | - () => computeWeeklySetsDashboardData(data, assetsMap, effectiveNow, window, 'muscles', secondarySetMultiplier), |
42 | | - { ttl: 10 * 60 * 1000 } |
43 | | - ); |
| 71 | + return getWeeklySetsDashboard(cacheKey, window, 'muscles'); |
44 | 72 | }, [assetsMap, data, effectiveNow, weeklySetsWindow, filterCacheKey, secondarySetMultiplier]); |
45 | 73 |
|
46 | 74 | const weeklySetsDashboardGroups = useMemo(() => { |
47 | 75 | if (!assetsMap) return null; |
48 | 76 |
|
49 | 77 | const window: WeeklySetsWindow = weeklySetsWindow === 'all' ? 'all' : weeklySetsWindow; |
50 | 78 | const cacheKey = muscleCacheKeys.weeklySets(filterCacheKey, window, 'groups', secondarySetMultiplier); |
51 | | - return computationCache.getOrCompute( |
52 | | - cacheKey, |
53 | | - data, |
54 | | - () => computeWeeklySetsDashboardData(data, assetsMap, effectiveNow, window, 'groups', secondarySetMultiplier), |
55 | | - { ttl: 10 * 60 * 1000 } |
56 | | - ); |
| 79 | + return getWeeklySetsDashboard(cacheKey, window, 'groups'); |
57 | 80 | }, [assetsMap, data, effectiveNow, weeklySetsWindow, filterCacheKey, secondarySetMultiplier]); |
58 | 81 |
|
59 | 82 | const windowedHeatmapData = useMemo(() => { |
|
0 commit comments