import {useCallback} from 'react' import {useShopActions} from './useShopActions' import type { IntentQuery, InvokeIntentResponse, } from '@shopify/shop-minis-platform/actions' /** * Internal hook that bridges minis to the host intent system * via the INVOKE_INTENT shop action. */ export function useShopIntent() { const {invokeIntent} = useShopActions() const invokeQuery = useCallback( async (query: IntentQuery): Promise => { // The WebView bridge rejects on `*_ERROR` / `ERROR` responses, so any // host-side failure (unsupported action on older hosts, middleware // exceptions, transport errors) arrives here as a thrown Error rather // than a resolved `{ok: false, ...}`. Catch it so `invokeQuery` always // honors the declared `Promise` contract. try { const result = await invokeIntent(query) if (!result.ok) { return { code: 'error', message: result.error?.message ?? 'Unknown error', } } return result.data } catch (error) { return { code: 'error', message: error instanceof Error ? error.message : 'Unknown error', } } }, [invokeIntent] ) return {invokeQuery} }