UNPKG

@bluvo/react

Version:

React hooks for Bluvo SDK - Framework-agnostic state machine for crypto withdrawals

34 lines (33 loc) 1.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFlowMachine = useFlowMachine; const react_1 = require("react"); function useFlowMachine(machine) { const [state, setState] = (0, react_1.useState)(() => machine?.getState() || null); const machineRef = (0, react_1.useRef)(machine); machineRef.current = machine; (0, react_1.useEffect)(() => { if (!machine) { setState(null); return; } // Set initial state setState(machine.getState()); // Subscribe to state changes const unsubscribe = machine.subscribe((newState) => { setState(newState); }); return unsubscribe; }, [machine]); const send = (0, react_1.useCallback)((action) => { machineRef.current?.send(action); }, []); return { state, send, isInState: (stateType) => state?.type === stateType, hasError: state?.error !== null, error: state?.error, context: state?.context }; }