Skip to content

Commit

Permalink
Use generated navigation files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Nov 22, 2024
1 parent d99cd66 commit 64ffe00
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
8 changes: 7 additions & 1 deletion src/utils/fetchNavigationFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';
import { BundleNavigation, NavItem, Navigation } from '../@types/types';
import { Required } from 'utility-types';
import { itLessBundles, requiredBundles } from '../components/AppFilter/useAppFilter';
import { ITLess, getChromeStaticPathname } from './common';
import { GENERATED_SEARCH_FLAG, ITLess, getChromeStaticPathname } from './common';

export function isBundleNavigation(item: unknown): item is BundleNavigation {
return typeof item !== 'undefined';
Expand Down Expand Up @@ -38,6 +38,12 @@ const filesCache: {
};

const fetchNavigationFiles = async () => {
if (localStorage.getItem(GENERATED_SEARCH_FLAG) === 'true') {
// aggregate data call
const { data: aggregateData } = await axios.get<BundleNavigation[]>('/api/chrome-service/v1/static/bundles-generated.json');
const bundleNavigation = aggregateData.filter(isBundleNavigation);
return bundleNavigation;
}
const bundles = ITLess() ? itLessBundles : requiredBundles;
if (filesCache.ready && filesCache.expires > Date.now()) {
return filesCache.data;
Expand Down
63 changes: 41 additions & 22 deletions src/utils/useNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import axios from 'axios';
import { useAtomValue, useSetAtom } from 'jotai';
import { useContext, useEffect, useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { BLOCK_CLEAR_GATEWAY_ERROR, getChromeStaticPathname } from './common';
import { BLOCK_CLEAR_GATEWAY_ERROR, GENERATED_SEARCH_FLAG, getChromeStaticPathname } from './common';
import { evaluateVisibility } from './isNavItemVisible';
import { QuickStartContext } from '@patternfly/quickstarts';
import { useFlagsStatus } from '@unleash/proxy-client-react';
import { BundleNavigation, NavItem, Navigation } from '../@types/types';
import { clearGatewayErrorAtom } from '../state/atoms/gatewayErrorAtom';
import { navigationAtom, setNavigationSegmentAtom } from '../state/atoms/navigationAtom';
import fetchNavigationFiles from './fetchNavigationFiles';

function cleanNavItemsHref(navItem: NavItem) {
const result = { ...navItem };
Expand Down Expand Up @@ -100,38 +101,56 @@ const useNavigation = () => {
});
};

async function handleNavigationResponse(data: BundleNavigation) {
let observer: MutationObserver | undefined;
if (observer && typeof observer.disconnect === 'function') {
observer.disconnect();
}

try {
const navItems = await Promise.all(data.navItems.map(cleanNavItemsHref).map(evaluateVisibility));
const schema: any = {
...data,
navItems,
};
observer = registerLocationObserver(pathname, schema);
observer.observe(document.querySelector('body')!, {
childList: true,
subtree: true,
});
} catch (error) {
// Hide nav if an error was encountered. Can happen for non-existing navigation files.
setNoNav(true);
}
}

useEffect(() => {
let observer: MutationObserver | undefined;
// reset no nav flag
setNoNav(false);
if (currentNamespace && (flagsReady || flagsError)) {
if (localStorage.getItem(GENERATED_SEARCH_FLAG) === 'true' && currentNamespace && (flagsReady || flagsError)) {
fetchNavigationFiles()
.then((bundles) => {
const bundle = bundles.find((b) => b.id === currentNamespace);
if (!bundle) {
setNoNav(true);
return;
}

return handleNavigationResponse(bundle);
})
.catch(() => {
setNoNav(true);
});
} else if (currentNamespace && (flagsReady || flagsError)) {
axios
.get(`${getChromeStaticPathname('navigation')}/${currentNamespace}-navigation.json`)
// fallback static CSC for EE env
.catch(() => {
return axios.get<BundleNavigation>(`/config/chrome/${currentNamespace}-navigation.json?ts=${Date.now()}`);
})
.then(async (response) => {
if (observer && typeof observer.disconnect === 'function') {
observer.disconnect();
}

const data = response.data;
try {
const navItems = await Promise.all(data.navItems.map(cleanNavItemsHref).map(evaluateVisibility));
const schema = {
...data,
navItems,
};
observer = registerLocationObserver(pathname, schema);
observer.observe(document.querySelector('body')!, {
childList: true,
subtree: true,
});
} catch (error) {
// Hide nav if an error was encountered. Can happen for non-existing navigation files.
setNoNav(true);
}
return handleNavigationResponse(response.data);
})
.catch(() => {
setNoNav(true);
Expand Down

0 comments on commit 64ffe00

Please sign in to comment.