diff --git a/nextjs-app/components/ComparisonView.tsx b/nextjs-app/components/ComparisonView.tsx index 9a7df1b..6d3bcd8 100644 --- a/nextjs-app/components/ComparisonView.tsx +++ b/nextjs-app/components/ComparisonView.tsx @@ -118,14 +118,22 @@ export function ComparisonView({ const primarySchools = selectedSchools.filter(s => classifySchool(s) === 'primary'); const secondarySchools = selectedSchools.filter(s => classifySchool(s) === 'secondary'); - // Auto-select tab with more schools + // Auto-select tab with more schools and sync the metric to match the detected phase. + // This fixes the case where the URL carries a primary metric (e.g. rwm_expected_pct) + // but the shortlisted schools are secondary — the phase tab switches but the metric + // needs to follow, otherwise all secondary cards show "–" for a primary-only field. useEffect(() => { - if (comparisonData && selectedSchools.length > 0) { - if (secondarySchools.length > primarySchools.length) { - setComparePhase('secondary'); - } else { - setComparePhase('primary'); - } + if (!comparisonData || selectedSchools.length === 0) return; + const newPhase = secondarySchools.length > primarySchools.length ? 'secondary' : 'primary'; + setComparePhase(newPhase); + // Only reset the metric when it doesn't belong to the newly detected phase. + // This preserves a correct metric that came from the URL (e.g. metric=attainment_8_score). + const phaseCategories = newPhase === 'secondary' ? SECONDARY_CATEGORIES : PRIMARY_CATEGORIES; + const metricFitsPhase = metrics.some( + (m) => m.key === selectedMetric && phaseCategories.includes(m.category) + ); + if (!metricFitsPhase) { + setSelectedMetric(newPhase === 'secondary' ? 'attainment_8_score' : 'rwm_expected_pct'); } }, [comparisonData]); // eslint-disable-line react-hooks/exhaustive-deps