Spaces:
Sleeping
Sleeping
Download Frontend/src/pages/Generate/GenerateContext.jsx from smit9604/ExamCraftAI: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/spaces/smit9604/ExamCraftAI/resolve/main/Frontend/src/pages/Generate/GenerateContext.jsx
- Command line
-
hf download hf://spaces/smit9604/ExamCraftAI/Frontend/src/pages/Generate/GenerateContext.jsx
-
curl -L -o GenerateContext.jsx https://huggingface.co/spaces/smit9604/ExamCraftAI/resolve/main/Frontend/src/pages/Generate/GenerateContext.jsx
4.24 kB
| import React, { createContext, useContext, useState, useEffect, useMemo } from 'react'; | |
| const GenerateContext = createContext(); | |
| export const useGenerate = () => useContext(GenerateContext); | |
| const STORAGE_KEY = 'examcraft_generate_state'; | |
| export const GenerateProvider = ({ children }) => { | |
| // Initial State | |
| const initialState = { | |
| step: 1, | |
| materials: [], | |
| configure: { | |
| quickTemplate: 'Custom', | |
| bloomsDistribution: { remember: 20, understand: 20, apply: 20, analyze: 15, evaluate: 15, create: 10 }, | |
| questionTypes: [ | |
| { id: 'mcq', label: 'Multiple Choice (MCQ)', count: 10, marks: 1, selected: true, topic: '', internalChoices: 0 }, | |
| { id: 'short', label: 'Short Answer', count: 5, marks: 5, selected: true, topic: '', internalChoices: 0 }, | |
| { id: 'long', label: 'Long Answer', count: 2, marks: 10, selected: false, topic: '', internalChoices: 0 }, | |
| { id: 'case', label: 'Case Study', count: 1, marks: 15, selected: false, topic: '', internalChoices: 0 } | |
| ], | |
| difficulty: 50, | |
| advancedSettings: { | |
| answerKey: true, | |
| markingScheme: true, | |
| timeEstimate: true, | |
| competencyMapping: false, | |
| chapterWeightage: false, | |
| avoidRepetition: true, | |
| randomizeOptions: false, | |
| hots: false, | |
| } | |
| }, | |
| finalDetails: { | |
| title: '', | |
| classGrade: '', | |
| subject: '', | |
| institution: '', | |
| duration: 60, | |
| totalMarks: 100, | |
| instructions: '' | |
| } | |
| }; | |
| // Load from localStorage on initialization with a safe merge against initialState | |
| const [state, setState] = useState(() => { | |
| try { | |
| const saved = localStorage.getItem(STORAGE_KEY); | |
| if (!saved) return initialState; | |
| const parsed = JSON.parse(saved); | |
| // Deep merge logic simplified for this structure | |
| return { | |
| ...initialState, | |
| ...parsed, | |
| configure: { | |
| ...initialState.configure, | |
| ...(parsed.configure || {}) | |
| }, | |
| finalDetails: { | |
| ...initialState.finalDetails, | |
| ...(parsed.finalDetails || {}) | |
| } | |
| }; | |
| } catch (e) { | |
| console.error("Failed to load state from localStorage", e); | |
| return initialState; | |
| } | |
| }); | |
| // Stable setters to prevent infinite loops in downstream useEffect hooks | |
| const setStep = React.useCallback((step) => setState(prev => ({ ...prev, step })), []); | |
| const setMaterials = React.useCallback((materials) => setState(prev => ({ ...prev, materials })), []); | |
| const setConfigure = React.useCallback((configure) => setState(prev => { | |
| if (typeof configure === 'function') { | |
| return { ...prev, configure: configure(prev.configure) }; | |
| } | |
| return { ...prev, configure }; | |
| }), []); | |
| const setFinalDetails = React.useCallback((finalDetails) => setState(prev => { | |
| if (typeof finalDetails === 'function') { | |
| return { ...prev, finalDetails: finalDetails(prev.finalDetails) }; | |
| } | |
| return { ...prev, finalDetails }; | |
| }), []); | |
| const resetState = React.useCallback(() => { | |
| setState(initialState); | |
| localStorage.removeItem(STORAGE_KEY); | |
| }, []); | |
| // Save to localStorage whenever state changes | |
| useEffect(() => { | |
| try { | |
| localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); | |
| } catch (e) { | |
| console.error("Failed to save state to localStorage", e); | |
| } | |
| }, [state]); | |
| // Optimize context value to prevent unnecessary re-renders | |
| const contextValue = useMemo(() => ({ | |
| ...state, | |
| setStep, | |
| setMaterials, | |
| setConfigure, | |
| setFinalDetails, | |
| resetState | |
| }), [state]); | |
| return ( | |
| <GenerateContext.Provider value={contextValue}> | |
| {children} | |
| </GenerateContext.Provider> | |
| ); | |
| }; | |