import { useRouter } from 'next/router' import { RACCOON_DEV_ACCOUNT, RACCOON_DEV_WORKSPACE } from './dev' import { publishMessage } from './message' import { isStandalone } from './utils/env' /** * This hook exposes the `navigation` method responsible * for enabling navigation between Next.js and Render pages. * * It triggers the triggerNextJSNavigation method from admin-shell. * * Read more about Raccoon's navigation architecture under the * navigation section of its docs. */ export function useNavigation() { const { push } = useRouter() /** * The `navigate` method is responsible for enabling navigation between Next.js and Render pages. */ const navigate = (path: string, options?: NavigateOptions) => { if (!path.startsWith('/')) { console.warn('The path must start with /') return } if (isStandalone()) { console.warn( 'The navigation method is not available in the Raccoon Dev environment, falling back to the default Next.js router.', ) if (options?.type === 'adminRelativeNavigation') { return window.open( `https://${RACCOON_DEV_WORKSPACE}--${RACCOON_DEV_ACCOUNT}.myvtex.com${path}`, '_blank', ) } return push(path) } const { type = 'navigation' } = options ?? {} publishMessage({ type, data: { path, }, }) } return { navigate, } } interface NavigateOptions { /** * Use the default `navigation` option when navigating to a page within your Next.js app. * Under the hood, this `navigation` type delegates the navigation to the Next.js router. * * Use the `adminRelativeNavigation` option when navigating to a Render app page or a Next.js page * outside your Next.js app. * Under the hood, the `adminRelativeNavigation` type delegates the navigation entirely to Render. */ type: 'navigation' | 'adminRelativeNavigation' }