Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Remove need for scopePathnames (#35584)
- Loading branch information
1 parent
b5d1b54
commit add8ec2
Showing
9 changed files
with
78 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
import { MuiPage } from 'docs/src/pages'; | ||
|
||
export default function findActivePage(currentPages: MuiPage[], pathname: string): MuiPage | null { | ||
export default function findActivePage( | ||
currentPages: MuiPage[], | ||
pathname: string, | ||
): { activePage: MuiPage | null; activePageParents: MuiPage[] } { | ||
const map: Record<string, MuiPage> = {}; | ||
const mapParent: Record<string, MuiPage> = {}; | ||
|
||
const traverse = (array: MuiPage[]) => { | ||
array.forEach((item) => { | ||
map[item.pathname] = item; | ||
traverse(item.children || []); | ||
const traverse = (parent: MuiPage) => { | ||
(parent.children || []).forEach((child) => { | ||
map[child.pathname] = child; | ||
if (mapParent[child.pathname]) { | ||
throw new Error(`Duplicated pathname ${child.pathname} in pages`); | ||
} | ||
mapParent[child.pathname] = parent; | ||
traverse(child); | ||
}); | ||
}; | ||
|
||
traverse(currentPages); | ||
traverse({ pathname: '/', children: currentPages }); | ||
|
||
return map[pathname] || null; | ||
const activePage = map[pathname] || null; | ||
|
||
const activePageParents = []; | ||
let traversePage = activePage; | ||
while (traversePage && traversePage.pathname !== '/') { | ||
const parent = mapParent[traversePage.pathname]; | ||
activePageParents.push(parent); | ||
traversePage = parent; | ||
} | ||
|
||
return { | ||
activePage, | ||
activePageParents, | ||
}; | ||
} |
Oops, something went wrong.