Files
lettersoup-online/002_source/cms/src/components/dashboard/student/students-selection-context.tsx
louiscklaw 5640d4d4f6 update,
2025-05-08 17:19:43 +08:00

44 lines
1.2 KiB
TypeScript

'use client';
import * as React from 'react';
import { useSelection } from '@/hooks/use-selection';
import type { Selection } from '@/hooks/use-selection';
import type { Student } from './type.d';
function noop(): void {
return undefined;
}
export interface StudentsSelectionContextValue extends Selection {}
export const StudentsSelectionContext = React.createContext<StudentsSelectionContextValue>({
deselectAll: noop,
deselectOne: noop,
selectAll: noop,
selectOne: noop,
selected: new Set(),
selectedAny: false,
selectedAll: false,
});
interface StudentsSelectionProviderProps {
children: React.ReactNode;
students: Student[];
}
export function StudentsSelectionProvider({
children,
students: customers = [],
}: StudentsSelectionProviderProps): React.JSX.Element {
const customerIds = React.useMemo(() => customers.map((customer) => customer.id), [customers]);
const selection = useSelection(customerIds);
return <StudentsSelectionContext.Provider value={{ ...selection }}>{children}</StudentsSelectionContext.Provider>;
}
export function useStudentsSelection(): StudentsSelectionContextValue {
return React.useContext(StudentsSelectionContext);
}