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: fix expanding chapters bug #1942

Merged
merged 5 commits into from
Mar 18, 2024
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
6 changes: 6 additions & 0 deletions .changeset/fuzzy-planets-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-docs/gatsby-theme-docs': patch
'@commercetools-website/docs-smoke-test': patch
---

Fix issue with multiple expanding chapters working in "tanem" when expanded
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ function createAncestorsTree(data) {
function traverse(chapters, parentPath = [], level = 0) {
chapters.forEach((chapter, index) => {
if (chapter.pages) {
const currentPath = [...parentPath, `${level}-${index}`];
let newPath = `${parentPath.join('#')}#${level}-${index}`;
if (newPath.startsWith('#')) {
newPath = newPath.substring(1);
}
const currentPath = [...parentPath, newPath];

ancestorsArray.push(currentPath);

if (chapter.pages.length > 0) {
Expand Down
43 changes: 25 additions & 18 deletions packages/gatsby-theme-docs/src/layouts/internals/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,23 @@ const Title = styled.span`
: designSystem.typography.fontWeights.regular};
`;

const ChapterTitle = (props) => (
<ChapterTitleWrapper level={props.level} onClick={() => props.toggleExpand()}>
<Title isExpanded={props.isExpanded} level={props.level}>
{props.text}
</Title>
{props.isExpanded ? (
<AngleDownIcon size="medium" />
) : (
<AngleRightIcon size="medium" />
)}
</ChapterTitleWrapper>
);
const ChapterTitle = (props) => {
return (
<ChapterTitleWrapper
level={props.level}
onClick={() => props.toggleExpand()}
>
<Title isExpanded={props.isExpanded} level={props.level}>
{props.text}
</Title>
{props.isExpanded ? (
<AngleDownIcon size="medium" />
) : (
<AngleRightIcon size="medium" />
)}
</ChapterTitleWrapper>
);
};
ChapterTitle.propTypes = {
text: PropTypes.string.isRequired,
isExpanded: PropTypes.bool,
Expand Down Expand Up @@ -326,17 +331,16 @@ const Chapter = (props) => {
props.location
);
const { ancestorsMap } = useSidebarNavigationItems();
const chapterId = `${props.level}-${props.index}`;
const chapterId =
props.level === 1 && props.parentChapterId
? `${props.parentChapterId}#${props.level}-${props.index}`
: `${props.level}-${props.index}`;
const { setExpandedChapters } = useContext(SidebarContextApi);
const { expandedChapters } = useContext(SidebarContextState);

useEffect(() => {
if (isSelectedChapter) {
const initialState = getItemAncestors(
props.level,
props.index,
ancestorsMap
);
const initialState = getItemAncestors(chapterId, ancestorsMap);
const expandedItemsList =
initialState.length > 0 ? initialState : [chapterId];

Expand All @@ -356,6 +360,7 @@ const Chapter = (props) => {
if (expandedChapters?.includes(chapterId)) {
// close the chapter (and all its descendants, if any)
const descendants = getItemDescendants(
chapterId,
props.level,
props.index,
ancestorsMap
Expand Down Expand Up @@ -402,6 +407,7 @@ const Chapter = (props) => {
index={pageIndex}
level={1}
chapter={page}
parentChapterId={chapterId}
location={props.location}
onLinkClick={props.onLinkClick}
nextScrollPosition={props.nextScrollPosition}
Expand Down Expand Up @@ -429,6 +435,7 @@ const Chapter = (props) => {
Chapter.propTypes = {
index: PropTypes.number.isRequired,
level: PropTypes.number.isRequired,
parentChapterId: PropTypes.string,
chapter: PropTypes.object.isRequired,
onLinkClick: PropTypes.func,
nextScrollPosition: PropTypes.number.isRequired,
Expand Down
16 changes: 11 additions & 5 deletions packages/gatsby-theme-docs/src/layouts/internals/sidebar.utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
export const getItemDescendants = (level, index, ancestorsMap) => {
export const getItemDescendants = (
subChapterId,
level,
index,
ancestorsMap
) => {
const chapterId = `${level}-${index}`;
if (level === 1) {
return [chapterId];
return [subChapterId];
} else {
const descendantsArray = ancestorsMap.filter((element) =>
element.includes(chapterId)
);
return [].concat(...descendantsArray);
return (chapterId.includes('#') ? [subChapterId] : []).concat(
...descendantsArray
);
}
};

export const getItemAncestors = (level, index, ancestorsMap) => {
const chapterId = `${level}-${index}`;
export const getItemAncestors = (chapterId, ancestorsMap) => {
const ancestorsArray = ancestorsMap.filter(
(element) => element.includes(chapterId) && element.indexOf(chapterId) > 0
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Empty page
---
10 changes: 10 additions & 0 deletions websites/docs-smoke-test/src/content/empty-pages-2/overview-1.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Empty pages
showTimeToRead: false
timeToRead: false
---

# Contents of this chapter

- [PageWithLongNameThatOverflowsBecauseItsOneWord](/empty-pages/empty-page-three)
- [BetaPageWithLongNameThatOverflowsBecauseItsOneWord](/empty-pages/empty-page-four)
10 changes: 10 additions & 0 deletions websites/docs-smoke-test/src/content/empty-pages-2/overview.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Empty pages
showTimeToRead: false
timeToRead: false
---

# Contents of this chapter

- [Page with some very long title too much for our innocent API to handle](/empty-pages/empty-page-one)
- [Beta Page with some very long title too much for our innocent API to handle](/empty-pages/empty-page-two)
24 changes: 24 additions & 0 deletions websites/docs-smoke-test/src/data/navigation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@
path: /empty-pages/empty-page-ten
- title: third level page 3
path: /empty-pages/empty-page-eleven
- chapter-title: Further Subchapters
pages:
- title: Subchapter One
pages:
- title: third level page 1
path: /empty-pages-2/empty-page-five
- title: third level page 2
path: /empty-pages-2/empty-page-six
- title: third level page 3
path: /empty-pages-2/empty-page-seven
- title: Normal Chapter 1
path: /empty-pages-2/empty-page-twelve
- title: Normal Chapter 2
path: /empty-pages-2/empty-page-thirteen
- title: Normal Chapter 3
path: /empty-pages-2/empty-page-fourteen
- title: Subchapter Two
pages:
- title: third level page 1
path: /empty-pages-2/empty-page-nine
- title: third level page 2
path: /empty-pages-2/empty-page-ten
- title: third level page 3
path: /empty-pages-2/empty-page-eleven
- chapter-title: Chapter 9999 With a Longer Multi-Word Name that Wraps
pages:
- title: Page with some very long title too much for our innocent API to handle
Expand Down
Loading