import { createContext, useContext, useState, ReactNode } from 'react'; interface Question { id: string; content: string; type: 'single' | 'multiple' | 'judgment' | 'text'; options?: string[]; answer: string | string[]; score: number; createdAt: string; category?: string; } interface QuizContextType { questions: Question[]; setQuestions: (questions: Question[]) => void; currentQuestionIndex: number; setCurrentQuestionIndex: (index: number) => void; answers: Record; setAnswer: (questionId: string, answer: string | string[]) => void; clearQuiz: () => void; } const QuizContext = createContext(undefined); export const QuizProvider = ({ children }: { children: ReactNode }) => { const [questions, setQuestions] = useState([]); const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); const [answers, setAnswers] = useState>({}); const setAnswer = (questionId: string, answer: string | string[]) => { setAnswers(prev => ({ ...prev, [questionId]: answer })); }; const clearQuiz = () => { setQuestions([]); setCurrentQuestionIndex(0); setAnswers({}); }; return ( {children} ); }; export const useQuiz = () => { const context = useContext(QuizContext); if (!context) { throw new Error('useQuiz必须在QuizProvider内使用'); } return context; };