2026-02-02 20:34:35 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* ComparisonView Component
|
|
|
|
|
|
* Client-side comparison interface with charts and tables
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
import { useRouter, usePathname, useSearchParams } from 'next/navigation';
|
|
|
|
|
|
import { useComparison } from '@/hooks/useComparison';
|
|
|
|
|
|
import { ComparisonChart } from './ComparisonChart';
|
|
|
|
|
|
import { SchoolSearchModal } from './SchoolSearchModal';
|
|
|
|
|
|
import { EmptyState } from './EmptyState';
|
2026-03-23 21:31:28 +00:00
|
|
|
|
import { LoadingSkeleton } from './LoadingSkeleton';
|
2026-02-02 20:34:35 +00:00
|
|
|
|
import type { ComparisonData, MetricDefinition } from '@/lib/types';
|
2026-03-23 21:31:28 +00:00
|
|
|
|
import { formatPercentage, formatProgress, CHART_COLORS } from '@/lib/utils';
|
2026-02-03 10:27:45 +00:00
|
|
|
|
import { fetchComparison } from '@/lib/api';
|
2026-02-02 20:34:35 +00:00
|
|
|
|
import styles from './ComparisonView.module.css';
|
|
|
|
|
|
|
|
|
|
|
|
interface ComparisonViewProps {
|
|
|
|
|
|
initialData: Record<string, ComparisonData> | null;
|
|
|
|
|
|
initialUrns: number[];
|
|
|
|
|
|
metrics: MetricDefinition[];
|
|
|
|
|
|
selectedMetric: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function ComparisonView({
|
|
|
|
|
|
initialData,
|
|
|
|
|
|
initialUrns,
|
|
|
|
|
|
metrics,
|
|
|
|
|
|
selectedMetric: initialMetric,
|
|
|
|
|
|
}: ComparisonViewProps) {
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
const pathname = usePathname();
|
|
|
|
|
|
const searchParams = useSearchParams();
|
2026-03-23 21:31:28 +00:00
|
|
|
|
const { selectedSchools, removeSchool, addSchool, isInitialized } = useComparison();
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
|
|
|
|
|
const [selectedMetric, setSelectedMetric] = useState(initialMetric);
|
|
|
|
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
|
|
|
|
const [comparisonData, setComparisonData] = useState(initialData);
|
2026-03-23 21:31:28 +00:00
|
|
|
|
const [shareConfirm, setShareConfirm] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
// Seed context from initialData when component mounts and localStorage is empty
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!isInitialized) return;
|
|
|
|
|
|
if (selectedSchools.length === 0 && initialUrns.length > 0 && initialData) {
|
|
|
|
|
|
initialUrns.forEach(urn => {
|
|
|
|
|
|
const data = initialData[String(urn)];
|
|
|
|
|
|
if (data?.school_info) {
|
|
|
|
|
|
addSchool(data.school_info);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [isInitialized]); // eslint-disable-line react-hooks/exhaustive-deps
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
|
|
|
|
|
// Sync URL with selected schools
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const urns = selectedSchools.map((s) => s.urn).join(',');
|
|
|
|
|
|
const params = new URLSearchParams(searchParams);
|
|
|
|
|
|
|
|
|
|
|
|
if (urns) {
|
|
|
|
|
|
params.set('urns', urns);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
params.delete('urns');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params.set('metric', selectedMetric);
|
|
|
|
|
|
|
|
|
|
|
|
const newUrl = `${pathname}?${params.toString()}`;
|
|
|
|
|
|
router.replace(newUrl, { scroll: false });
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch comparison data
|
|
|
|
|
|
if (selectedSchools.length > 0) {
|
2026-02-03 10:27:45 +00:00
|
|
|
|
fetchComparison(urns, { cache: 'no-store' })
|
2026-02-02 22:47:37 +00:00
|
|
|
|
.then((data) => {
|
|
|
|
|
|
setComparisonData(data.comparison);
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
|
console.error('Failed to fetch comparison:', err);
|
|
|
|
|
|
setComparisonData(null);
|
|
|
|
|
|
});
|
2026-02-02 20:34:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
setComparisonData(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [selectedSchools, selectedMetric, pathname, searchParams, router]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleMetricChange = (metric: string) => {
|
|
|
|
|
|
setSelectedMetric(metric);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleRemoveSchool = (urn: number) => {
|
|
|
|
|
|
removeSchool(urn);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-23 21:31:28 +00:00
|
|
|
|
const handleShare = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await navigator.clipboard.writeText(window.location.href);
|
|
|
|
|
|
setShareConfirm(true);
|
|
|
|
|
|
setTimeout(() => setShareConfirm(false), 2000);
|
|
|
|
|
|
} catch { /* fallback: do nothing */ }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-02 20:34:35 +00:00
|
|
|
|
// Get metric definition
|
|
|
|
|
|
const currentMetricDef = metrics.find((m) => m.key === selectedMetric);
|
|
|
|
|
|
const metricLabel = currentMetricDef?.label || selectedMetric;
|
|
|
|
|
|
|
|
|
|
|
|
// No schools selected
|
|
|
|
|
|
if (selectedSchools.length === 0) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={styles.container}>
|
|
|
|
|
|
<header className={styles.header}>
|
|
|
|
|
|
<h1>Compare Schools</h1>
|
|
|
|
|
|
<p className={styles.subtitle}>
|
|
|
|
|
|
Add schools to your comparison basket to see side-by-side performance data
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
|
|
<EmptyState
|
|
|
|
|
|
title="No schools selected"
|
|
|
|
|
|
message="Add schools from the home page or search to start comparing."
|
|
|
|
|
|
action={{
|
2026-03-23 21:31:28 +00:00
|
|
|
|
label: '+ Add Schools to Compare',
|
|
|
|
|
|
onClick: () => setIsModalOpen(true),
|
2026-02-02 20:34:35 +00:00
|
|
|
|
}}
|
|
|
|
|
|
/>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
|
|
|
|
|
|
<SchoolSearchModal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)} />
|
2026-02-02 20:34:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get years for table
|
|
|
|
|
|
const years =
|
|
|
|
|
|
comparisonData && Object.keys(comparisonData).length > 0
|
|
|
|
|
|
? comparisonData[Object.keys(comparisonData)[0]].yearly_data.map((d) => d.year)
|
|
|
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={styles.container}>
|
|
|
|
|
|
{/* Header */}
|
|
|
|
|
|
<header className={styles.header}>
|
|
|
|
|
|
<div className={styles.headerContent}>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<h1>Compare Schools</h1>
|
|
|
|
|
|
<p className={styles.subtitle}>
|
|
|
|
|
|
Comparing {selectedSchools.length} school{selectedSchools.length !== 1 ? 's' : ''}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
<div style={{ display: 'flex', gap: '0.75rem', alignItems: 'center', flexWrap: 'wrap' }}>
|
|
|
|
|
|
<button onClick={() => setIsModalOpen(true)} className={styles.addButton}>
|
|
|
|
|
|
+ Add School
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button onClick={handleShare} className={styles.shareButton} title="Copy comparison link">
|
|
|
|
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="16" height="16"><path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"/><polyline points="16 6 12 2 8 6"/><line x1="12" y1="2" x2="12" y2="15"/></svg>
|
|
|
|
|
|
{shareConfirm ? 'Copied!' : 'Share'}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Metric Selector */}
|
|
|
|
|
|
<section className={styles.metricSelector}>
|
|
|
|
|
|
<label htmlFor="metric-select" className={styles.metricLabel}>
|
|
|
|
|
|
Select Metric:
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<select
|
|
|
|
|
|
id="metric-select"
|
|
|
|
|
|
value={selectedMetric}
|
|
|
|
|
|
onChange={(e) => handleMetricChange(e.target.value)}
|
|
|
|
|
|
className={styles.metricSelect}
|
|
|
|
|
|
>
|
2026-02-04 11:50:13 +00:00
|
|
|
|
<optgroup label="Expected Standard">
|
|
|
|
|
|
{metrics.filter(m => m.category === 'expected').map((metric) => (
|
|
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="Higher Standard">
|
|
|
|
|
|
{metrics.filter(m => m.category === 'higher').map((metric) => (
|
|
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="Progress Scores">
|
|
|
|
|
|
{metrics.filter(m => m.category === 'progress').map((metric) => (
|
|
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="Average Scores">
|
|
|
|
|
|
{metrics.filter(m => m.category === 'average').map((metric) => (
|
|
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="Gender Performance">
|
|
|
|
|
|
{metrics.filter(m => m.category === 'gender').map((metric) => (
|
|
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="Equity (Disadvantaged)">
|
2026-02-04 11:53:26 +00:00
|
|
|
|
{metrics.filter(m => m.category === 'disadvantaged').map((metric) => (
|
2026-02-04 11:50:13 +00:00
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="School Context">
|
|
|
|
|
|
{metrics.filter(m => m.category === 'context').map((metric) => (
|
|
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
|
|
|
|
|
<optgroup label="3-Year Trends">
|
2026-02-04 11:53:26 +00:00
|
|
|
|
{metrics.filter(m => m.category === '3yr').map((metric) => (
|
2026-02-04 11:50:13 +00:00
|
|
|
|
<option key={metric.key} value={metric.key}>{metric.label}</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</optgroup>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
</select>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{currentMetricDef?.description && (
|
|
|
|
|
|
<p className={styles.metricDescription}>{currentMetricDef.description}</p>
|
|
|
|
|
|
)}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
</section>
|
|
|
|
|
|
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{/* Progress score explanation */}
|
|
|
|
|
|
{selectedMetric.includes('progress') && (
|
|
|
|
|
|
<p className={styles.progressNote}>
|
|
|
|
|
|
Progress scores measure pupils' progress from KS1 to KS2. A score of 0 equals the national average; positive scores are above average.
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-02-02 20:34:35 +00:00
|
|
|
|
{/* School Cards */}
|
|
|
|
|
|
<section className={styles.schoolsSection}>
|
|
|
|
|
|
<div className={styles.schoolsGrid}>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
{selectedSchools.map((school, index) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
key={school.urn}
|
|
|
|
|
|
className={styles.schoolCard}
|
|
|
|
|
|
style={{ borderLeft: `3px solid ${CHART_COLORS[index % CHART_COLORS.length]}` }}
|
|
|
|
|
|
>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => handleRemoveSchool(school.urn)}
|
|
|
|
|
|
className={styles.removeButton}
|
|
|
|
|
|
aria-label="Remove school"
|
2026-03-23 21:31:28 +00:00
|
|
|
|
title="Remove from comparison"
|
2026-02-02 20:34:35 +00:00
|
|
|
|
>
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<h3 className={styles.schoolName}>
|
|
|
|
|
|
<a href={`/school/${school.urn}`}>{school.school_name}</a>
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
<div className={styles.schoolMeta}>
|
|
|
|
|
|
{school.local_authority && (
|
2026-02-02 22:34:14 +00:00
|
|
|
|
<span className={styles.metaItem}>{school.local_authority}</span>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
)}
|
|
|
|
|
|
{school.school_type && (
|
2026-02-02 22:34:14 +00:00
|
|
|
|
<span className={styles.metaItem}>{school.school_type}</span>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Latest metric value */}
|
|
|
|
|
|
{comparisonData && comparisonData[school.urn] && (
|
|
|
|
|
|
<div className={styles.latestValue}>
|
|
|
|
|
|
<div className={styles.latestLabel}>{metricLabel}</div>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
<div className={styles.latestNumber} style={{ color: CHART_COLORS[index % CHART_COLORS.length] }}>
|
|
|
|
|
|
<span
|
|
|
|
|
|
style={{
|
|
|
|
|
|
display: 'inline-block',
|
|
|
|
|
|
width: '10px',
|
|
|
|
|
|
height: '10px',
|
|
|
|
|
|
borderRadius: '50%',
|
|
|
|
|
|
background: CHART_COLORS[index % CHART_COLORS.length],
|
|
|
|
|
|
marginRight: '0.4rem',
|
|
|
|
|
|
verticalAlign: 'middle',
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
2026-02-02 20:34:35 +00:00
|
|
|
|
{(() => {
|
|
|
|
|
|
const yearlyData = comparisonData[school.urn].yearly_data;
|
|
|
|
|
|
if (yearlyData.length === 0) return '-';
|
|
|
|
|
|
|
|
|
|
|
|
const latestData = yearlyData[yearlyData.length - 1];
|
|
|
|
|
|
const value = latestData[selectedMetric as keyof typeof latestData];
|
|
|
|
|
|
|
|
|
|
|
|
if (value === null || value === undefined) return '-';
|
|
|
|
|
|
|
|
|
|
|
|
// Format based on metric type
|
|
|
|
|
|
if (selectedMetric.includes('progress')) {
|
|
|
|
|
|
return formatProgress(value as number);
|
|
|
|
|
|
} else if (selectedMetric.includes('pct') || selectedMetric.includes('rate')) {
|
|
|
|
|
|
return formatPercentage(value as number);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return typeof value === 'number' ? value.toFixed(1) : String(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
})()}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Comparison Chart */}
|
2026-02-02 22:47:37 +00:00
|
|
|
|
{comparisonData && Object.keys(comparisonData).length > 0 ? (
|
2026-02-02 20:34:35 +00:00
|
|
|
|
<section className={styles.chartSection}>
|
|
|
|
|
|
<h2 className={styles.sectionTitle}>Performance Over Time</h2>
|
|
|
|
|
|
<div className={styles.chartContainer}>
|
|
|
|
|
|
<ComparisonChart
|
|
|
|
|
|
comparisonData={comparisonData}
|
|
|
|
|
|
metric={selectedMetric}
|
|
|
|
|
|
metricLabel={metricLabel}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</section>
|
2026-02-02 22:47:37 +00:00
|
|
|
|
) : selectedSchools.length > 0 ? (
|
|
|
|
|
|
<section className={styles.chartSection}>
|
2026-03-23 21:31:28 +00:00
|
|
|
|
<LoadingSkeleton type="list" />
|
2026-02-02 22:47:37 +00:00
|
|
|
|
</section>
|
|
|
|
|
|
) : null}
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
|
|
|
|
|
{/* Comparison Table */}
|
|
|
|
|
|
{comparisonData && Object.keys(comparisonData).length > 0 && years.length > 0 && (
|
|
|
|
|
|
<section className={styles.tableSection}>
|
|
|
|
|
|
<h2 className={styles.sectionTitle}>Detailed Comparison</h2>
|
|
|
|
|
|
<div className={styles.tableWrapper}>
|
|
|
|
|
|
<table className={styles.comparisonTable}>
|
|
|
|
|
|
<thead>
|
|
|
|
|
|
<tr>
|
|
|
|
|
|
<th>Year</th>
|
|
|
|
|
|
{selectedSchools.map((school) => (
|
|
|
|
|
|
<th key={school.urn}>{school.school_name}</th>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</tr>
|
|
|
|
|
|
</thead>
|
|
|
|
|
|
<tbody>
|
|
|
|
|
|
{years.map((year) => (
|
|
|
|
|
|
<tr key={year}>
|
|
|
|
|
|
<td className={styles.yearCell}>{year}</td>
|
|
|
|
|
|
{selectedSchools.map((school) => {
|
|
|
|
|
|
const schoolData = comparisonData[school.urn];
|
|
|
|
|
|
if (!schoolData) return <td key={school.urn}>-</td>;
|
|
|
|
|
|
|
|
|
|
|
|
const yearData = schoolData.yearly_data.find((d) => d.year === year);
|
|
|
|
|
|
if (!yearData) return <td key={school.urn}>-</td>;
|
|
|
|
|
|
|
|
|
|
|
|
const value = yearData[selectedMetric as keyof typeof yearData];
|
|
|
|
|
|
|
|
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
|
|
return <td key={school.urn}>-</td>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Format based on metric type
|
|
|
|
|
|
let displayValue: string;
|
|
|
|
|
|
if (selectedMetric.includes('progress')) {
|
|
|
|
|
|
displayValue = formatProgress(value as number);
|
|
|
|
|
|
} else if (selectedMetric.includes('pct') || selectedMetric.includes('rate')) {
|
|
|
|
|
|
displayValue = formatPercentage(value as number);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
displayValue = typeof value === 'number' ? value.toFixed(1) : String(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return <td key={school.urn}>{displayValue}</td>;
|
|
|
|
|
|
})}
|
|
|
|
|
|
</tr>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</tbody>
|
|
|
|
|
|
</table>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</section>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* School Search Modal */}
|
|
|
|
|
|
<SchoolSearchModal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|