File size: 1,363 Bytes
1cacda5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import { createContext, useContext, useState, useEffect } from 'react'
const TutorialContext = createContext()
export function TutorialProvider({ children }) {
const [isTutorialActive, setIsTutorialActive] = useState(false)
const [currentStep, setCurrentStep] = useState(0)
const [highlightedElement, setHighlightedElement] = useState(null)
useEffect(() => {
const handleStartTutorial = () => {
setIsTutorialActive(true)
setCurrentStep(0)
}
window.addEventListener('start-tutorial', handleStartTutorial)
return () => window.removeEventListener('start-tutorial', handleStartTutorial)
}, [])
const nextStep = () => {
setCurrentStep(prev => prev + 1)
}
const previousStep = () => {
setCurrentStep(prev => Math.max(0, prev - 1))
}
const endTutorial = () => {
setIsTutorialActive(false)
setCurrentStep(0)
setHighlightedElement(null)
}
return (
<TutorialContext.Provider value={{
isTutorialActive,
currentStep,
nextStep,
previousStep,
endTutorial,
highlightedElement,
setHighlightedElement
}}>
{children}
</TutorialContext.Provider>
)
}
export function useTutorial() {
const context = useContext(TutorialContext)
if (!context) {
throw new Error('useTutorial must be used within TutorialProvider')
}
return context
} |