import { Entity, EntityCollection, EntityCustomView, FireCMSContext, User } from "../types"; import { useEffect, useState } from "react"; import { getNavigationEntriesFromPathInternal } from "../util/navigation_from_path"; import { useFireCMSContext } from "./useFireCMSContext"; /** * @see resolveNavigationFrom * @group Hooks and utilities */ export type ResolvedNavigationEntry> = | ResolvedNavigationEntity | ResolvedNavigationCollection | ResolvedNavigationEntityCustom; /** * @see resolveNavigationFrom * @group Hooks and utilities */ export interface ResolvedNavigationEntity> { type: "entity"; entity: Entity; entityId: string; path: string; parentCollection: EntityCollection; } /** * @see resolveNavigationFrom * @group Hooks and utilities */ export interface ResolvedNavigationCollection> { type: "collection"; path: string; collection: EntityCollection; } /** * @see resolveNavigationFrom * @group Hooks and utilities */ interface ResolvedNavigationEntityCustom> { type: "custom_view"; path: string; view: EntityCustomView; } /** * Use this function to retrieve an array of navigation entries (resolved * collection, entity or entity custom_view) for the given path. You need to pass the app context * that you receive in different callbacks, such as the save hooks. * * It will take into account the `navigation` provided at the `FireCMS` level. * * @param path * @param context * @group Hooks and utilities */ export function resolveNavigationFrom, UserType extends User>({ path, context }: { path: string, context: FireCMSContext }): Promise[]> { const dataSource = context.dataSource; const navigation = context.navigation; if (!navigation) { throw Error("Calling getNavigationFrom, but main navigation has not yet been initialised"); } const navigationEntries = getNavigationEntriesFromPathInternal({ path, collections: navigation.collections ?? [] }); const resultPromises: Promise>[] = navigationEntries .map((entry) => { if (entry.type === "collection") { return Promise.resolve(entry); } else if (entry.type === "entity") { const collection = navigation.getCollection(entry.path); if (!collection) { throw Error(`No collection defined in the navigation for the entity with path ${entry.path}`); } return dataSource.fetchEntity({ path: entry.path, entityId: entry.entityId, collection }) .then((entity) => { if (!entity) return undefined; return { ...entry, entity }; }); } else if (entry.type === "custom_view") { return Promise.resolve(entry); } else { throw Error("Unmapped element in useEntitiesFromPath"); } }) .filter(v => Boolean(v)) as Promise>[]; return Promise.all(resultPromises); } /** * @group Hooks and utilities */ export interface NavigationFromProps { path: string; } /** * @group Hooks and utilities */ export interface NavigationFrom> { data?: ResolvedNavigationEntry[] dataLoading: boolean, dataLoadingError?: Error } /** * Use this hook to retrieve an array of navigation entries (resolved * collection or entity) for the given path. You can use this hook * in any React component that lives under `FireCMS` * @group Hooks and utilities */ export function useResolvedNavigationFrom, UserType extends User>( { path }: NavigationFromProps): NavigationFrom { const context: FireCMSContext = useFireCMSContext(); const [data, setData] = useState[] | undefined>(); const [dataLoading, setDataLoading] = useState(false); const [dataLoadingError, setDataLoadingError] = useState(); useEffect(() => { const navigation = context.navigation; if (navigation) { setDataLoading(true); setDataLoadingError(undefined); resolveNavigationFrom({ path, context }) .then((res) => { setData(res); }) .catch((e) => setDataLoadingError(e)) .finally(() => setDataLoading(false)); } }, [path, context]); if (!context.navigation) { return { dataLoading: true }; } return { data, dataLoading, dataLoadingError }; }