import { ACTIONS } from './core' import { shell } from './shell' export interface BreadcrumbParams { /** * ID of the the new breadcrumb level that must be inserted. * @default `${index}-${newLevelName}` * */ newLevelId?: string /** * Name of the new breadcrumb level that must be inserted. * */ newLevelName: string /** * The url that corresponds to this breadcrumb item. * @default window.location.pathname * */ newLevelUrl: string } export interface Breadcrumbs { breadcrumbs?: BreadcrumbParams[] } /** * Function to remove every previously set breadcrumb extra level by * the `insertNewBreadcrumbLevels` function. This will maintain * the levels not set by the `insertNewBreadcrumbLevels`. Events * that happen within the admin-shell's iframe might pass unperceived by * its eyes, so you will need to clear extra breadcrumb * levels manually by using this function. * * # @example * ```jsx * // other necessary imports up here... * import { clearBreadcrumbExtraLevels } from `admin-shell-utils` * * const App = () => { * const { navigate } = useRuntime() * * return ( *
* { * clearBreadcrumbExtraLevels() * navigate({ * page: 'admin.my-example-app.previous-page-url' * }) * }} * /> *
* ) * } * ``` */ export function clearBreadcrumbExtraLevels() { shell.postMessage(ACTIONS.CLEAR_BREADCRUMB_EXTRA_LEVELS(), '*') } /** * Function to insert new breadcrumb levels. * It sends a message to the admin-shell which * then appends each item to the breadcrumb. * * * # @example * ```jsx * // other necessary imports up here... * import { insertNewBreadcrumbLevels } from `admin-shell-utils` * * const App = () => { * const { navigate } = useRuntime() * const { formatMessage } = useIntl() * * return ( *
* { * insertNewBreadcrumbLevels([{ * newLevelId: 'next-page-1' * newLevelName: formatMessage({ id: 'admin/my-example-app.next-page-title' }), * newLevelUrl: 'admin.my-example-app.next-page-url', * }]) * navigate({ * page: 'admin.my-example-app.next-page-url' * }) * }} * /> *
* ) * } * ``` */ export function insertNewBreadcrumbLevels(params: BreadcrumbParams[]) { shell.postMessage(ACTIONS.INSERT_BREADCRUMB_EXTRA_LEVELS(params), '*') }