2026-02-02 20:34:35 +00:00
|
|
|
/**
|
|
|
|
|
* Custom hook for managing school comparison state
|
2026-02-03 14:26:23 +00:00
|
|
|
* Uses shared context for real-time updates across components
|
2026-02-02 20:34:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
import { fetcher } from '@/lib/api';
|
2026-02-03 14:26:23 +00:00
|
|
|
import { useComparisonContext } from '@/context/ComparisonContext';
|
|
|
|
|
import type { ComparisonResponse } from '@/lib/types';
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
|
|
|
export function useComparison() {
|
2026-02-03 14:26:23 +00:00
|
|
|
const {
|
|
|
|
|
selectedSchools,
|
|
|
|
|
addSchool,
|
|
|
|
|
removeSchool,
|
|
|
|
|
clearAll,
|
|
|
|
|
isSelected,
|
|
|
|
|
canAddMore,
|
|
|
|
|
isInitialized,
|
|
|
|
|
} = useComparisonContext();
|
2026-02-02 20:34:35 +00:00
|
|
|
|
|
|
|
|
// Fetch comparison data for selected schools
|
|
|
|
|
const urns = selectedSchools.map((s) => s.urn).join(',');
|
|
|
|
|
const { data, error, isLoading, mutate } = useSWR<ComparisonResponse>(
|
|
|
|
|
selectedSchools.length > 0 ? `/compare?urns=${urns}` : null,
|
|
|
|
|
fetcher,
|
|
|
|
|
{
|
|
|
|
|
revalidateOnFocus: false,
|
2026-02-03 14:26:23 +00:00
|
|
|
dedupingInterval: 10000,
|
2026-02-02 20:34:35 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
selectedSchools,
|
|
|
|
|
comparisonData: data?.comparison,
|
|
|
|
|
isLoading,
|
|
|
|
|
error,
|
|
|
|
|
addSchool,
|
|
|
|
|
removeSchool,
|
|
|
|
|
clearAll,
|
|
|
|
|
isSelected,
|
2026-02-03 14:26:23 +00:00
|
|
|
canAddMore,
|
|
|
|
|
isInitialized,
|
2026-02-02 20:34:35 +00:00
|
|
|
mutate,
|
|
|
|
|
};
|
|
|
|
|
}
|