diff --git a/apps/web/screens/Organization/ParentSubpage.tsx b/apps/web/screens/Organization/ParentSubpage.tsx index ca63c7f97f55..e49946141a26 100644 --- a/apps/web/screens/Organization/ParentSubpage.tsx +++ b/apps/web/screens/Organization/ParentSubpage.tsx @@ -68,7 +68,7 @@ const OrganizationParentSubpage: Screen< ]} navigationData={{ title: n('navigationTitle', 'Efnisyfirlit'), - items: getSubpageNavList(organizationPage, router), + items: getSubpageNavList(organizationPage, router, 3), }} > diff --git a/apps/web/screens/Organization/SubPage.tsx b/apps/web/screens/Organization/SubPage.tsx index dd2850892088..1365e372dd61 100644 --- a/apps/web/screens/Organization/SubPage.tsx +++ b/apps/web/screens/Organization/SubPage.tsx @@ -215,25 +215,46 @@ export const SubPageContent = ({ ) } +const generateNavigationItems = ( + organizationPage: OrganizationPage | null | undefined, + pathname: string, +): NavigationItem[] => { + const links = (organizationPage?.menuLinks ?? []).map( + ({ primaryLink, childrenLinks }) => ({ + title: primaryLink?.text ?? '', + href: primaryLink?.url, + active: + primaryLink?.url === pathname || + childrenLinks.some((link) => link.url === pathname), + items: childrenLinks.map(({ text, url }) => ({ + title: text, + href: url, + active: url === pathname, + })), + }), + ) + return links +} + export const getSubpageNavList = ( organizationPage: OrganizationPage | null | undefined, router: NextRouter, + depthOfMatch: number | null = null, ): NavigationItem[] => { const pathname = new URL(router.asPath, 'https://island.is').pathname - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore make web strict - return organizationPage?.menuLinks.map(({ primaryLink, childrenLinks }) => ({ - title: primaryLink?.text, - href: primaryLink?.url, - active: - primaryLink?.url === pathname || - childrenLinks.some((link) => link.url === pathname), - items: childrenLinks.map(({ text, url }) => ({ - title: text, - href: url, - active: url === pathname, - })), - })) + if (!depthOfMatch) { + return generateNavigationItems(organizationPage, pathname) + } + + const items = generateNavigationItems( + organizationPage, + pathname + .split('/') + .slice(0, depthOfMatch + 1) // Add one to account for the starting '/' + .join('/'), + ) + + return items } type SubPageScreenContext = ScreenContext & {