Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Pass current path as a param
Browse files Browse the repository at this point in the history
Makes it easier to inject a mock
  • Loading branch information
ptgott committed Jul 17, 2024
1 parent b3c8b4c commit 1c66628
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions layouts/DocsPage/Navigation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const NavigationFourLevels = () => {
data={data}
section={true}
currentVersion="16.x"
currentPathGetter={() => {
return "/enroll-resources/machine-id/deployment/aws/";
}}
></DocNavigation>
);
};
Expand Down
16 changes: 13 additions & 3 deletions layouts/DocsPage/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ const SCOPE_DICTIONARY: Record<string, ScopeType> = {
interface DocsNavigationItemsProps {
entries: NavigationItem[];
onClick: () => void;
currentPath: string;
}

const DocsNavigationItems = ({
entries,
onClick,
currentPath,
}: DocsNavigationItemsProps) => {
const docPath = useCurrentHref().split(SCOPELESS_HREF_REGEX)[0];
const docPath = currentPath.split(SCOPELESS_HREF_REGEX)[0];
const { getVersionAgnosticRoute } = useVersionAgnosticPages();

return (
Expand Down Expand Up @@ -62,6 +64,7 @@ const DocsNavigationItems = ({
<DocsNavigationItems
entries={entry.entries}
onClick={onClick}
currentPath={currentPath}
/>
</ul>
)}
Expand All @@ -77,6 +80,7 @@ interface DocNavigationCategoryProps extends NavigationCategory {
opened: boolean;
onToggleOpened: (value: number) => void;
onClick: () => void;
currentPath: string;
}

const DocNavigationCategory = ({
Expand All @@ -87,6 +91,7 @@ const DocNavigationCategory = ({
icon,
title,
entries,
currentPath,
}: DocNavigationCategoryProps) => {
const toggleOpened = useCallback(
() => onToggleOpened(opened ? null : id),
Expand All @@ -107,7 +112,11 @@ const DocNavigationCategory = ({
</HeadlessButton>
{opened && (
<ul className={styles["category-links"]}>
<DocsNavigationItems entries={entries} onClick={onClick} />
<DocsNavigationItems
entries={entries}
onClick={onClick}
currentPath={currentPath}
/>
</ul>
)}
</>
Expand Down Expand Up @@ -139,11 +148,11 @@ interface DocNavigationProps {
currentPathGetter: () => string;
}

// TODO: inject the router for mocking
const DocNavigation = ({
data,
section,
currentVersion,
currentPathGetter = { useCurrentHref },
}: DocNavigationProps) => {
const route = currentPathGetter();

Expand Down Expand Up @@ -177,6 +186,7 @@ const DocNavigation = ({
opened={index === openedId}
onToggleOpened={setOpenedId}
onClick={toggleMenu}
currentPath={route}
{...props}
/>
</li>
Expand Down

0 comments on commit 1c66628

Please sign in to comment.