import { useCallback } from 'react'; import { journeyStep, type JourneyHandle, type JourneyStepOptions, type StepHandle, } from '../../core'; import { useOptionalJourneyName } from './context'; /** * Run a step on the active `` journey, an explicit name, or a handle. * A handle auto-starts when the journey is not open; a bare name does not. * * @example * const runStep = useJourneyStep(); * await runStep('load', async step => { await api.get('/x', { stamp: step.stamp() }); }); */ export function useJourneyStep(journey?: string | JourneyHandle | null) { const fromScope = useOptionalJourneyName(); const resolved = journey !== undefined ? journey : fromScope; return useCallback( ( stepName: string, fn: (step: StepHandle) => T | Promise, opts?: JourneyStepOptions ): Promise => journeyStep(resolved, stepName, fn, opts), [resolved] ); }