import { useCurrentRoute } from '@kbn/typed-react-router-config' ; import { useContext , useEffect , useRef } from 'react' ; import { castArray } from 'lodash' ; import { Breadcrumb , BreadcrumbsContext } from './context' ; export function useBreadcrumb ( breadcrumb ) { const api = useContext ( BreadcrumbsContext ) ; if ( ! api ) { throw new Error ( 'Missing Breadcrumb API in context' ) ; } const { match } = useCurrentRoute ( ) ; const matchedRoute = useRef ( match ?. route ) ; if ( matchedRoute . current && matchedRoute . current !== match ?. route ) { api . unset ( matchedRoute . current ) ; } matchedRoute . current = match ?. route ; if ( matchedRoute . current ) { api . set ( matchedRoute . current , castArray ( breadcrumb ) ) ; } useEffect ( ( ) => { return ( ) => { if ( matchedRoute . current ) { api . unset ( matchedRoute . current ) ; } } ; } , [ ] ) ; }