Skip to content

Commit

Permalink
fix: expanding chapter are incorrectly expanding (#1949)
Browse files Browse the repository at this point in the history
* fix: expanding chapter are incorrectly expanding

* chore: add debug message

* chore: remove pathPrefix from location
  • Loading branch information
gabriele-ct authored Mar 25, 2024
1 parent 07effa4 commit bb025af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-beans-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-docs/gatsby-theme-docs': patch
---

Expandible chapters state needs to be changed based on the exact matching of the location, instead of a loose "includes"
7 changes: 5 additions & 2 deletions packages/gatsby-theme-docs/src/layouts/internals/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
isRightChapter,
isRightChapterRecursive,
} from './sidebar.utils';
import { useSiteData } from '../../hooks/use-site-data';

const ReleaseNotesIcon = createStyledIcon(Icons.ReleaseNotesSvgIcon);

Expand Down Expand Up @@ -323,10 +324,12 @@ const ChapterItem = styled.div`
`;

const Chapter = (props) => {
const isSelectedChapter = isRightChapter(props.chapter, props.location);
const { pathPrefix } = useSiteData();
const pathnameNoPrefix = props.location.pathname.replace(pathPrefix, '');
const isSelectedChapter = isRightChapter(props.chapter, pathnameNoPrefix);
const isSSRSelectedChapter = isRightChapterRecursive(
props.chapter,
props.location
pathnameNoPrefix
);
const { ancestorsMap } = useSidebarNavigationItems();
const chapterId =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ export const getItemAncestors = (chapterId, ancestorsMap) => {
return [].concat(...ancestorsArray);
};

export const isRightChapter = (chapter, loc) => {
return (
chapter.pages.find((page) => loc.pathname.includes(page.path)) !== undefined
);
export const isRightChapter = (chapter, pathname) => {
return chapter.pages.find((page) => pathname === page.path) !== undefined;
};

export const isRightChapterRecursive = (chapter, loc) => {
if (!loc) {
export const isRightChapterRecursive = (chapter, pathname) => {
if (!pathname) {
return false;
}
return (
chapter.pages.find((page) =>
page.pages
? isRightChapterRecursive(page, loc)
: loc.pathname.includes(page.path)
? isRightChapterRecursive(page, pathname)
: pathname === page.path
) !== undefined
);
};

0 comments on commit bb025af

Please sign in to comment.