fix: open specific nav group on docs page on first page load (#4034)

Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
This commit is contained in:
DivyanshuLohani
2024-10-27 04:46:04 +05:30
committed by GitHub
parent 6f043ec16e
commit 47826a45aa

View File

@@ -160,6 +160,33 @@ const NavigationGroup = ({
const pathname = usePathname();
const [isActiveGroup, setIsActiveGroup] = useState<boolean>(false);
// We need to expand the group with the current link so we loop over all links
// Until we find the one and then expand the groups
useEffect(() => {
const findMatchingGroup = () => {
for (const group of navigation) {
for (const link of group.links) {
if (!link.children) continue;
const matchingChild = link.children.find((child) => pathname && child.href.startsWith(pathname));
if (matchingChild) {
setOpenGroups([`${group.title}-${link.title}`]);
setActiveGroup(group);
return;
}
}
}
};
findMatchingGroup();
return () => {
setOpenGroups([]);
setActiveGroup(null);
};
}, [pathname, setActiveGroup, setOpenGroups]);
useEffect(() => {
setIsActiveGroup(activeGroup?.title === group.title);
}, [activeGroup?.title, group.title]);