Commit 04fdcf38 authored by Tom Darneix's avatar Tom Darneix
Browse files

Fix menu (#11098)

parent dccaea14
import { MouseEvent, useEffect, useRef, useState } from 'react';
import { equals, flatten, isEmpty, isNil } from 'ramda';
import { equals, flatten, isEmpty, isNil, length } from 'ramda';
import { useNavigate, useLocation } from 'react-router-dom';
import { useAtom } from 'jotai';
import { useAtomValue, useUpdateAtom } from 'jotai/utils';
......@@ -127,7 +127,6 @@ const NavigationMenu = ({
return;
}
navigate(getUrlFromEntry(currentPage) as string);
setSelectedNavigationItems(hoveredNavigationItems);
};
const isItemHovered = ({ navigationItem, level, currentPage }): boolean => {
......@@ -186,28 +185,41 @@ const NavigationMenu = ({
return [parentItem, ...args].reverse();
};
const getItemHoveredByDefault = (
currentPage,
...args
): Array<Page> | null => {
if (!currentPage.is_react && searchItemsWithPhpUrl(currentPage, ...args)) {
return searchItemsWithPhpUrl(currentPage, ...args);
}
if (currentPage.is_react && searchItemsWithReactUrl(currentPage, ...args)) {
return searchItemsWithReactUrl(currentPage, ...args);
}
return null;
};
const searchItemsHoveredByDefault = (
currentPage,
...args
): Array<Page> | null => {
const hoveredCurrentPage = getItemHoveredByDefault(currentPage, ...args);
if (hoveredCurrentPage) {
return hoveredCurrentPage;
}
const childPage = currentPage?.children;
if (isNil(childPage) || !isArrayItem(childPage)) {
if (
!currentPage.is_react &&
searchItemsWithPhpUrl(currentPage, ...args)
) {
return searchItemsWithPhpUrl(currentPage, ...args);
}
if (
currentPage.is_react &&
searchItemsWithReactUrl(currentPage, ...args)
) {
return searchItemsWithReactUrl(currentPage, ...args);
}
return getItemHoveredByDefault(currentPage, ...args);
}
return childPage?.map((item) => {
const hoveredItem = getItemHoveredByDefault(item, ...args);
if (hoveredItem && equals(length(hoveredItem as Array<Page>), 1)) {
return [currentPage, ...hoveredItem];
}
const grandsonPage = item?.groups;
if (isNil(grandsonPage) || !isArrayItem(grandsonPage)) {
if (args.length > 0) {
......@@ -248,6 +260,19 @@ const NavigationMenu = ({
return () => window.removeEventListener('beforeunload', handleWindowClose);
}, []);
useEffect(() => {
navigationData?.forEach((item) => {
const searchedItems = searchItemsHoveredByDefault(item);
const filteredResult = flatten(searchedItems || []).filter(Boolean);
if (isEmpty(filteredResult)) {
return;
}
addSelectedNavigationItemsByDefault(filteredResult);
});
}, [pathname, search]);
return useMemoComponent({
Component: (
<List
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment