- Move comparison state from hook to shared context provider - All components now share the same state instance - Badge count updates immediately when schools are added/removed - Add key prop to badge to re-trigger animation on count change - Add storage event listener for cross-tab synchronization Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
49 lines
1.0 KiB
TypeScript
49 lines
1.0 KiB
TypeScript
/**
|
|
* Custom hook for managing school comparison state
|
|
* Uses shared context for real-time updates across components
|
|
*/
|
|
|
|
'use client';
|
|
|
|
import useSWR from 'swr';
|
|
import { fetcher } from '@/lib/api';
|
|
import { useComparisonContext } from '@/context/ComparisonContext';
|
|
import type { ComparisonResponse } from '@/lib/types';
|
|
|
|
export function useComparison() {
|
|
const {
|
|
selectedSchools,
|
|
addSchool,
|
|
removeSchool,
|
|
clearAll,
|
|
isSelected,
|
|
canAddMore,
|
|
isInitialized,
|
|
} = useComparisonContext();
|
|
|
|
// 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,
|
|
dedupingInterval: 10000,
|
|
}
|
|
);
|
|
|
|
return {
|
|
selectedSchools,
|
|
comparisonData: data?.comparison,
|
|
isLoading,
|
|
error,
|
|
addSchool,
|
|
removeSchool,
|
|
clearAll,
|
|
isSelected,
|
|
canAddMore,
|
|
isInitialized,
|
|
mutate,
|
|
};
|
|
}
|