Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): Organization parent subpage - Link highlighting in sidebar #17367

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading