Files
lettersoup-online/002_source/cms/src/components/dashboard/mf/questions/mf-questions-selection-context.tsx
2025-04-22 13:19:12 +08:00

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);
}