|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { GuideKetContext } from "../hooks/useGuideKitContext"; |
| 4 | +import { GuideKitContextType, GuideKitCustomTypes, GuideType } from "../types"; |
| 5 | + |
| 6 | +/** |
| 7 | + * GuideKitProvider |
| 8 | + **/ |
| 9 | +export function GuideKitProvider({ children }: { children: React.ReactNode }) { |
| 10 | + const onCompleteRef = React.useRef<() => void>(); |
| 11 | + const onCloseRef = React.useRef<() => void>(); |
| 12 | + const guideKeyListRef = React.useRef<GuideKitCustomTypes["guideKeyType"][]>( |
| 13 | + [] |
| 14 | + ); |
| 15 | + |
| 16 | + const [currentGuideKey, setCurrentGuideKey] = React.useState< |
| 17 | + GuideKitCustomTypes["guideKeyType"] | null |
| 18 | + >(null); |
| 19 | + |
| 20 | + const [guideInfoMap, setGuideInfoMap] = React.useState< |
| 21 | + Map<GuideKitCustomTypes["guideKeyType"], GuideType> |
| 22 | + >(new Map()); |
| 23 | + |
| 24 | + const currentGuideInfo = React.useMemo(() => { |
| 25 | + if (!currentGuideKey) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + if (guideInfoMap.has(currentGuideKey)) { |
| 30 | + return guideInfoMap.get(currentGuideKey) ?? null; |
| 31 | + } |
| 32 | + return null; |
| 33 | + }, [currentGuideKey, guideInfoMap]); |
| 34 | + |
| 35 | + const startGuide = React.useCallback( |
| 36 | + ({ |
| 37 | + guideKeyList, |
| 38 | + onComplete, |
| 39 | + onClose, |
| 40 | + }: { |
| 41 | + guideKeyList: GuideKitCustomTypes["guideKeyType"][]; |
| 42 | + onComplete?: () => void; |
| 43 | + onClose?: () => void; |
| 44 | + }) => { |
| 45 | + onCompleteRef.current = onComplete; |
| 46 | + onCloseRef.current = onClose; |
| 47 | + guideKeyListRef.current = guideKeyList; |
| 48 | + |
| 49 | + if (guideKeyList.length > 0) { |
| 50 | + setCurrentGuideKey(guideKeyList[0]); |
| 51 | + } |
| 52 | + }, |
| 53 | + [] |
| 54 | + ); |
| 55 | + |
| 56 | + const closeGuide = React.useCallback(() => { |
| 57 | + onCompleteRef.current = undefined; |
| 58 | + onCloseRef.current = undefined; |
| 59 | + guideKeyListRef.current = []; |
| 60 | + setCurrentGuideKey(null); |
| 61 | + setGuideInfoMap(new Map()); |
| 62 | + }, []); |
| 63 | + |
| 64 | + const goNextStep = React.useCallback(() => { |
| 65 | + if (!currentGuideKey) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const currentIndex = guideKeyListRef.current.indexOf(currentGuideKey); |
| 70 | + const nextIndex = currentIndex + 1; |
| 71 | + |
| 72 | + if (nextIndex < guideKeyListRef.current.length) { |
| 73 | + setCurrentGuideKey(guideKeyListRef.current[nextIndex]); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + onCompleteRef.current?.(); |
| 78 | + |
| 79 | + closeGuide(); |
| 80 | + }, [currentGuideKey, guideInfoMap]); |
| 81 | + |
| 82 | + const defaultContextValue = React.useMemo( |
| 83 | + () => |
| 84 | + ({ |
| 85 | + currentGuideInfo, |
| 86 | + currentGuideKey, |
| 87 | + setGuideInfoMap, |
| 88 | + startGuide, |
| 89 | + onCompleteRef, |
| 90 | + onCloseRef, |
| 91 | + goNextStep, |
| 92 | + closeGuide, |
| 93 | + } satisfies GuideKitContextType<GuideKitCustomTypes["guideKeyType"]>), |
| 94 | + [currentGuideInfo, currentGuideKey, setGuideInfoMap, startGuide] |
| 95 | + ); |
| 96 | + |
| 97 | + return ( |
| 98 | + <GuideKetContext.Provider value={defaultContextValue}> |
| 99 | + {children} |
| 100 | + </GuideKetContext.Provider> |
| 101 | + ); |
| 102 | +} |
0 commit comments