(props: Props)
| 31 | } |
| 32 | |
| 33 | export default function useTourEngine(props: Props): UseTourEngineReturn { |
| 34 | const mergedProps = useMemoDeepCompare(() => mergeProps(defaultProps, props), [props]); |
| 35 | const { debug, initialStepIndex, onEvent, run, stepIndex, steps } = mergedProps; |
| 36 | |
| 37 | const store = useRef(createStore(mergedProps)); |
| 38 | const state = useSyncExternalStore<StoreState>( |
| 39 | store.current.subscribe, |
| 40 | store.current.getSnapshot, |
| 41 | store.current.getServerSnapshot, |
| 42 | ); |
| 43 | |
| 44 | const [failures, setFailures] = useState<StepFailure[]>([]); |
| 45 | |
| 46 | const addFailure: AddFailure = useCallback((failedStep, reason) => { |
| 47 | setFailures(previous => [...previous, { reason, step: failedStep }]); |
| 48 | }, []); |
| 49 | |
| 50 | const clearFailures = useCallback(() => { |
| 51 | setFailures([]); |
| 52 | }, []); |
| 53 | |
| 54 | useDebugLogger(store, debug); |
| 55 | |
| 56 | const controls = useControls(store, debug, clearFailures); |
| 57 | const emitEvent = useEventEmitter(onEvent, controls, store); |
| 58 | |
| 59 | const { index, size, status } = state; |
| 60 | |
| 61 | const previousState = usePrevious(state); |
| 62 | |
| 63 | const step = useMemo(() => getMergedStep(mergedProps, steps[index]), [index, mergedProps, steps]); |
| 64 | |
| 65 | useMount(() => { |
| 66 | if (run && size && validateSteps(steps, debug)) { |
| 67 | controls.start(stepIndex ?? initialStepIndex); |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | useUpdateEffect(() => { |
| 72 | if (run && size && status === STATUS.IDLE) { |
| 73 | store.current.updateState({ status: STATUS.READY }); |
| 74 | } |
| 75 | }, [run, size, status]); |
| 76 | |
| 77 | usePropSync({ |
| 78 | controls, |
| 79 | emitEvent, |
| 80 | props: mergedProps, |
| 81 | state, |
| 82 | store, |
| 83 | }); |
| 84 | |
| 85 | useLifecycleEffect({ |
| 86 | addFailure, |
| 87 | controls, |
| 88 | emitEvent, |
| 89 | previousState, |
| 90 | props: mergedProps, |
no test coverage detected
searching dependent graphs…