@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
64 lines • 3.33 kB
JavaScript
import { getOrAddWebNavAPI } from './getOrAddWebNavAPI';
import { startPerformOpV2, updatePerformanceEndV2 } from '../../../components/molecules/Performance/functions';
/**
* Can be used instead of getOrAddWebNavAPI to get all levels of the nav heirarchy for non-hub nav like TopLinks and QuickLaunch
*
* @param sourceProps
* @param method
* @param location
* @param alertMe
* @param consoleLog
* @param maxDepth
* @returns
*/
export async function getFullNavHierarchy(sourceProps, method, location, alertMe, consoleLog, maxDepth = 3) {
/**
* Flattens a two-dimensional array into a one-dimensional array.
* @param arr - The nested array to flatten.
* @returns A flattened array containing all elements.
*/
function flattenArray(arr) {
return arr.reduce((acc, val) => acc.concat(val), []);
}
/**
* Recursively fetches navigation items up to the specified depth.
* @param parentNodeId - The ID of the parent node whose children should be fetched.
* @param depth - The current depth level in the hierarchy.
* @returns A promise resolving to an array of navigation links.
*/
async function fetchNavLevel(parentNodeId, depth) {
if (depth > maxDepth)
return [];
const result = await getOrAddWebNavAPI(sourceProps, method, location, alertMe, consoleLog, null, parentNodeId);
const navItems = result.navItem;
// If there are no children or the max depth is reached, return the fetched items
if (navItems.length === 0 || depth === maxDepth) {
return navItems;
}
// Recursively fetch children for each navigation item
const childFetches = navItems.map(async (item) => {
const children = await fetchNavLevel(item.Id, depth + 1);
return [item, ...children];
});
// Resolve all child fetches concurrently
const resolvedChildren = await Promise.all(childFetches);
return flattenArray(resolvedChildren);
}
const performanceSettings = { label: 'FetchCheck', includeMsStr: true, updateMiliseconds: true, op: 'fetch' };
const fetchOp = startPerformOpV2(performanceSettings);
// Fetch the initial set of top-level navigation items (parentNodeId is null)
const initialResult = await getOrAddWebNavAPI(sourceProps, method, location, alertMe, consoleLog, null);
// Fetch the first level of children concurrently
const childFetches = initialResult.navItem.map(async (item) => {
const children = await fetchNavLevel(item.Id, 2);
return [item, ...children];
});
// Resolve all child fetches and merge them into a single array
const resolvedChildren = await Promise.all(childFetches);
const mergedNavItems = flattenArray(resolvedChildren);
initialResult.unifiedPerformanceOps.fetch = updatePerformanceEndV2({ op: fetchOp, updateMiliseconds: performanceSettings.updateMiliseconds, count: mergedNavItems.length });
initialResult.fetchOp = initialResult.unifiedPerformanceOps.fetch;
// Return the original response structure, but with the fully resolved navigation items
return { ...initialResult, navItem: mergedNavItems, items: mergedNavItems };
}
//# sourceMappingURL=getFullNavHierarchy.js.map