Skip to content

Commit

Permalink
Allow link match for smaller depth
Browse files Browse the repository at this point in the history
  • Loading branch information
RunarVestmann committed Dec 30, 2024
1 parent 56493cc commit 5ea245a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/web/screens/Organization/ParentSubpage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const OrganizationParentSubpage: Screen<
]}
navigationData={{
title: n('navigationTitle', 'Efnisyfirlit'),
items: getSubpageNavList(organizationPage, router),
items: getSubpageNavList(organizationPage, router, 3),
}}
>
<Box paddingTop={4}>
Expand Down
49 changes: 35 additions & 14 deletions apps/web/screens/Organization/SubPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down

0 comments on commit 5ea245a

Please sign in to comment.