Skip to content

Commit

Permalink
Merge pull request #2567 from RedHatInsights/fake-bundle
Browse files Browse the repository at this point in the history
Allow 'fake' services to be favorited
  • Loading branch information
Hyperkid123 authored Jul 13, 2023
2 parents 2c6f60a + f29ebe7 commit 31973f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/hooks/useAllServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ const useAllServices = () => {
servicesLinks,
error,
ready,
availableSections,
filterValue,
setFilterValue,
findNavItems,
};
};

Expand Down
31 changes: 27 additions & 4 deletions src/hooks/useFavoritedServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,50 @@ import { ServiceTileProps } from '../components/FavoriteServices/ServiceTile';
import useAllServices from './useAllServices';
import { useEffect, useMemo, useState } from 'react';
import fetchNavigationFiles, { extractNavItemGroups } from '../utils/fetchNavigationFiles';
import { Navigation } from '../@types/types';
import { NavItem, Navigation } from '../@types/types';
import { findNavLeafPath } from '../utils/common';
import useFavoritePagesWrapper from './useFavoritePagesWrapper';
import { isAllServicesLink } from '../components/AllServices/allServicesLinks';

const useFavoritedServices = () => {
const { favoritePages } = useFavoritePagesWrapper();
const { allLinks } = useAllServices();
const { allLinks, availableSections } = useAllServices();
const [bundles, setBundles] = useState<Navigation[]>([]);

const fakeBundle: NavItem[] = useMemo(() => {
// escape early if we have no services
if (availableSections.length === 0) {
return [];
}

// map services links to nav links
return availableSections.reduce<NavItem[]>((acc, curr) => {
const fakeNavItems: NavItem[] = curr.links.filter(isAllServicesLink);
// no need to recreate the reduce array
acc.push(...fakeNavItems);
return acc;
}, []);
}, [availableSections]);

useEffect(() => {
fetchNavigationFiles()
.then((data) => setBundles(data as Navigation[]))
.catch((error) => {
console.error('Unable to fetch favorite services', error);
});
}, []);

const linksWithFragments = useMemo(() => {
// push items with unique hrefs from our fake bundle for leaf creation
fakeBundle.forEach((item) => {
if (!allLinks.some((link) => link.href === item.href)) {
allLinks.push(item);
}
});
return allLinks.map((link) => {
let linkLeaf: ReturnType<typeof findNavLeafPath> | undefined;
// use every to exit early if match was found
bundles.every((bundle) => {
[...bundles, fakeBundle || []].every((bundle) => {
const leaf = findNavLeafPath(extractNavItemGroups(bundle), (item) => item?.href === link.href);
if (leaf.activeItem) {
linkLeaf = leaf;
Expand All @@ -35,7 +58,7 @@ const useFavoritedServices = () => {
linkLeaf,
};
});
}, [allLinks, bundles]);
}, [allLinks, bundles, fakeBundle]);

// extract human friendly data from the all services data set
const favoriteServices = favoritePages.reduce<ServiceTileProps[]>((acc, curr) => {
Expand Down

0 comments on commit 31973f8

Please sign in to comment.