Files
school_compare/nextjs-app/context/ComparisonContext.tsx
T

38 lines
1.2 KiB
TypeScript
Raw Normal View History

/**
* ComparisonContext
* Global state for school comparison basket
*/
'use client';
import { createContext, useContext } from 'react';
import type { CompareGroup } from '@/lib/compareLogic';
import type { School } from '@/lib/types';
interface ComparisonContextType {
selectedSchools: School[];
comparisonData: any;
isLoading: boolean;
error: any;
addSchool: (school: School) => void;
removeSchool: (urn: number) => void;
replaceSchools: (schools: School[]) => void;
/** Fill in phases missing from stored entries; never overwrites one. */
backfillPhases: (phases: Record<number, string | null | undefined>) => void;
clearAll: () => void;
isSelected: (urn: number) => boolean;
/** The comparison group with no room for this school, or null. */
fullGroupFor: (school: Pick<School, 'phase'>) => CompareGroup | null;
isInitialized: boolean;
mutate: () => void;
}
export const ComparisonContext = createContext<ComparisonContextType | undefined>(undefined);
export function useComparisonContext() {
const context = useContext(ComparisonContext);
if (!context) {
throw new Error('useComparisonContext must be used within ComparisonProvider');
}
return context;
}