47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
'use client';
|
|
|
|
import * as React from 'react';
|
|
|
|
// import type { LessonCategory } from '@/types/lesson-type';
|
|
import { useSelection } from '@/hooks/use-selection';
|
|
import type { Selection } from '@/hooks/use-selection';
|
|
|
|
import type { MfQuestion } from './type';
|
|
|
|
function noop(): void {
|
|
return undefined;
|
|
}
|
|
|
|
export interface MfQuestionsSelectionContextValue extends Selection {}
|
|
|
|
export const MfQuestionsSelectionContext = React.createContext<MfQuestionsSelectionContextValue>({
|
|
deselectAll: noop,
|
|
deselectOne: noop,
|
|
selectAll: noop,
|
|
selectOne: noop,
|
|
selected: new Set(),
|
|
selectedAny: false,
|
|
selectedAll: false,
|
|
});
|
|
|
|
interface MfQuestionsSelectionProviderProps {
|
|
children: React.ReactNode;
|
|
lessonQuestions: MfQuestion[];
|
|
}
|
|
|
|
export function MfQuestionsSelectionProvider({
|
|
children,
|
|
lessonQuestions = [],
|
|
}: MfQuestionsSelectionProviderProps): React.JSX.Element {
|
|
const customerIds = React.useMemo(() => lessonQuestions.map((customer) => customer.id), [lessonQuestions]);
|
|
const selection = useSelection(customerIds);
|
|
|
|
return (
|
|
<MfQuestionsSelectionContext.Provider value={{ ...selection }}>{children}</MfQuestionsSelectionContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useMfQuestionsSelection(): MfQuestionsSelectionContextValue {
|
|
return React.useContext(MfQuestionsSelectionContext);
|
|
}
|