💄 Display active page in manage navigation bar

This commit is contained in:
Manuel
2023-08-06 16:22:24 +02:00
parent 7a499da903
commit 936980d67a
@@ -30,6 +30,7 @@ import { useSession } from 'next-auth/react';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
import Image from 'next/image'; import Image from 'next/image';
import Link from 'next/link'; import Link from 'next/link';
import { useRouter } from 'next/router';
import { ReactNode, RefObject, forwardRef } from 'react'; import { ReactNode, RefObject, forwardRef } from 'react';
import { useScreenLargerThan } from '~/hooks/useScreenLargerThan'; import { useScreenLargerThan } from '~/hooks/useScreenLargerThan';
import { usePackageAttributesStore } from '~/tools/client/zustands/usePackageAttributesStore'; import { usePackageAttributesStore } from '~/tools/client/zustands/usePackageAttributesStore';
@@ -140,6 +141,7 @@ const CustomNavigationLink = forwardRef<
CustomNavigationLinkProps CustomNavigationLinkProps
>(({ name, navigationLink }, ref) => { >(({ name, navigationLink }, ref) => {
const { t } = useTranslation('layout/manage'); const { t } = useTranslation('layout/manage');
const router = useRouter();
const commonProps = { const commonProps = {
label: t(`navigation.${name}.title`), label: t(`navigation.${name}.title`),
@@ -148,21 +150,28 @@ const CustomNavigationLink = forwardRef<
<navigationLink.icon size={16} /> <navigationLink.icon size={16} />
</ThemeIcon> </ThemeIcon>
), ),
defaultOpened: false,
}; };
if ('href' in navigationLink) { if ('href' in navigationLink) {
const isActive = router.pathname.endsWith(navigationLink.href);
return ( return (
<NavLink <NavLink
{...commonProps} {...commonProps}
ref={ref as RefObject<HTMLAnchorElement>} ref={ref as RefObject<HTMLAnchorElement>}
component={Link} component={Link}
href={navigationLink.href} href={navigationLink.href}
active={isActive}
/> />
); );
} }
const isAnyActive = Object.entries(navigationLink.items)
.map(([_, item]) => item.href)
.some((href) => router.pathname.endsWith(href));
return ( return (
<NavLink {...commonProps} ref={ref as RefObject<HTMLButtonElement>}> <NavLink {...commonProps} defaultOpened={isAnyActive} ref={ref as RefObject<HTMLButtonElement>}>
{Object.entries(navigationLink.items).map(([itemName, item]) => { {Object.entries(navigationLink.items).map(([itemName, item]) => {
const commonItemProps = { const commonItemProps = {
label: t(`navigation.${name}.items.${itemName}`), label: t(`navigation.${name}.items.${itemName}`),
@@ -170,11 +179,13 @@ const CustomNavigationLink = forwardRef<
href: item.href, href: item.href,
}; };
const matchesActive = router.pathname.endsWith(item.href);
if (item.href.startsWith('http')) { if (item.href.startsWith('http')) {
return <NavLink {...commonItemProps} component="a" />; return <NavLink {...commonItemProps} active={matchesActive} component="a" />;
} }
return <NavLink {...commonItemProps} component={Link} />; return <NavLink {...commonItemProps} active={matchesActive} component={Link} />;
})} })}
</NavLink> </NavLink>
); );