import {useCallback} from 'react' import {useHandleAction} from '../../internal/useHandleAction' import {useShopActions} from '../../internal/useShopActions' import type { IntentDefinitions, IntentKey, IntentResult, } from '@shopify/shop-minis-platform/actions' export interface UseResolveIntentResolver { (result: IntentResult): Promise } export interface UseResolveIntentReturns { /** * Completes the intent and returns the user to Shop. * * Use `{code: 'ok', data: ...}` when the intent succeeds, `{code: 'error', * message}` when your Mini cannot complete it, or `{code: 'closed'}` when * the user exits without finishing. * * The returned promise resolves when the result has been sent, and rejects if * the SDK could not send it. */ resolveIntent: UseResolveIntentResolver } export type UseResolveIntentReturn = Pick< UseResolveIntentReturns, 'resolveIntent' > /** * Completes the intent identified by `intentKey`. TypeScript narrows the * accepted result data shape to that intent's contract. */ export const useResolveIntent = ( intentKey: K ): UseResolveIntentReturns => { const {resolveIntent} = useShopActions() const handleResolve = useHandleAction(resolveIntent) return { resolveIntent: useCallback( (result: IntentResult) => handleResolve({intentKey, result}), [handleResolve, intentKey] ), } } /** * Completes the intent your Mini is handling and returns the user to Shop. Pass the intent key so TypeScript can narrow the accepted result data for that intent. Pair with `useIntent` to read the incoming intent. The returned resolver promise resolves when the result has been sent and rejects if the SDK could not send it. * @publicDocs */ export type UseResolveIntentGeneratedType = ( intentKey: K ) => UseResolveIntentReturn