import { Entity , EntityCollection , EntityCustomView , FireCMSContext } from "../models" ; import { useEffect , useState } from "react" ; import { getNavigationEntriesFromPathInternal } from "../core/util/navigation_from_path" ; import { useFireCMSContext } from "./useFireCMSContext" ; see resolveNavigationFrom category export type ResolvedNavigationEntry < M > = ResolvedNavigationEntity < M > | ResolvedNavigationCollection < M > | ResolvedNavigationEntityCustom < M > ; see resolveNavigationFrom category export interface ResolvedNavigationEntity < M > { type : "entity" ; entity : Entity < M > ; entityId : string ; path : string ; parentCollection : EntityCollection < M > ; } see resolveNavigationFrom category export interface ResolvedNavigationCollection < M > { type : "collection" ; path : string ; collection : EntityCollection < M > ; } see resolveNavigationFrom category interface ResolvedNavigationEntityCustom < M > { type : "custom_view" ; path : string ; view : EntityCustomView < M > ; } param path param context category export function resolveNavigationFrom < M , UserType > ( { path , context } ) { const dataSource = context . dataSource ; const navigationContext = context . navigationContext ; const navigation = context . navigationContext . navigation ; if ( ! navigation ) { throw Error ( "Calling getNavigationFrom, but main navigation has not yet been initialised" ) ; } const navigationEntries = getNavigationEntriesFromPathInternal ( { path , collections : navigation . collections } ) ; const resultPromises = navigationEntries . map ( ( entry ) => { if ( entry . type === "collection" ) { return Promise . resolve ( entry ) ; } else if ( entry . type === "entity" ) { const entityCollectionResolver = navigationContext . getCollectionResolver ( entry . path , entry . entityId ) ; if ( ! entityCollectionResolver ?. schemaResolver ) { throw Error ( `No schema defined in the navigation for the entity with path ${ entry . path }` ) ; } return dataSource . fetchEntity ( { path : entry . path , entityId : entry . entityId , schema : entityCollectionResolver ?. schemaResolver } ) . then ( ( entity ) => { return { ... entry , entity } ; } ) ; } else if ( entry . type === "custom_view" ) { return Promise . resolve ( entry ) ; } else { throw Error ( "Unmapped element in useEntitiesFromPath" ) ; } } ) ; return Promise . all ( resultPromises ) ; } category export interface NavigationFromProps { path : string ; } category export interface NavigationFrom < M > { data ? : ResolvedNavigationEntry < M > [ ] ; dataLoading : boolean ; dataLoadingError ? : Error ; } category export function useResolvedNavigationFrom < M , UserType > ( { path } ) { const context = useFireCMSContext ( ) ; const [ data , setData ] = useState < ResolvedNavigationEntry < M > [ ] | undefined > ( ) ; const [ dataLoading , setDataLoading ] = useState < boolean > ( false ) ; const [ dataLoadingError , setDataLoadingError ] = useState < Error | undefined > ( ) ; useEffect ( ( ) => { const navigation = context . navigationContext . navigation ; if ( navigation ) { setDataLoading ( true ) ; setDataLoadingError ( undefined ) ; resolveNavigationFrom < M , UserType > ( { path , context } ) . then ( ( res ) => { setData ( res ) ; } ) . catch ( ( e ) => setDataLoadingError ( e ) ) . finally ( ( ) => setDataLoading ( false ) ) ; } } , [ path , context ] ) ; if ( ! context . navigationContext . navigation ) { return { dataLoading : true } ; } return { data , dataLoading , dataLoadingError } ; }