Skip to content

Commit

Permalink
[core] Remove need for scopePathnames (#35584)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Dec 30, 2022
1 parent b5d1b54 commit add8ec2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 76 deletions.
1 change: 0 additions & 1 deletion docs/data/joy/pages.ts
@@ -1,7 +1,6 @@
const pages = [
{
pathname: '/joy-ui/getting-started',
scopePathnames: ['/joy-ui/main-features'],
icon: 'DescriptionIcon',
children: [
{ pathname: '/joy-ui/getting-started/overview' },
Expand Down
12 changes: 3 additions & 9 deletions docs/data/material/pages.ts
Expand Up @@ -21,12 +21,6 @@ const pages: MuiPage[] = [
},
{
pathname: '/material-ui/react-',
scopePathnames: [
'/material-ui/icons',
'/material-ui/material-icons',
'/material-ui/about-the-lab',
'/material-ui/transitions',
],
title: 'Components',
icon: 'ToggleOnIcon',
children: [
Expand Down Expand Up @@ -165,7 +159,7 @@ const pages: MuiPage[] = [
icon: 'CreateIcon',
children: [
{
pathname: '/material-ui/customization',
pathname: '/material-ui/customization/theme',
subheader: '/material-ui/customization/theme',
children: [
{ pathname: '/material-ui/customization/theming' },
Expand Down Expand Up @@ -263,7 +257,7 @@ const pages: MuiPage[] = [
title: 'Migrating to Grid v2',
},
{
pathname: '/material-ui/migration',
pathname: '/material-ui/migration/v5',
subheader: 'Upgrade to v5',
children: [
{
Expand All @@ -289,7 +283,7 @@ const pages: MuiPage[] = [
],
},
{
pathname: '/material-ui/migration',
pathname: '/material-ui/migration/earlier',
subheader: 'Earlier versions',
children: [
{ pathname: '/material-ui/migration/migration-v3', title: 'Migration from v3 to v4' },
Expand Down
15 changes: 0 additions & 15 deletions docs/data/system/pages.ts
Expand Up @@ -14,21 +14,6 @@ const pages = [
},
{
pathname: '/style-utilities',
scopePathnames: [
'/system/properties',
'/system/borders',
'/system/display',
'/system/flexbox',
'/system/grid',
'/system/palette',
'/system/positions',
'/system/shadows',
'/system/sizing',
'/system/spacing',
'/system/screen-readers',
'/system/typography',
'/system/styled',
],
icon: 'BuildIcon',
children: [
{ pathname: '/system/properties' },
Expand Down
33 changes: 16 additions & 17 deletions docs/pages/_app.js
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import { loadCSS } from 'fg-loadcss/src/loadCSS';
import NextHead from 'next/head';
import PropTypes from 'prop-types';
import pages from 'docs/src/pages';
import generalPages from 'docs/src/pages';
import basePages from 'docs/data/base/pages';
import materialPages from 'docs/data/material/pages';
import joyPages from 'docs/data/joy/pages';
Expand Down Expand Up @@ -142,18 +142,22 @@ function AppWrapper(props) {
}
}, []);

let productPages = pages;
if (product === 'base') {
productPages = basePages;
} else if (product === 'material-ui') {
productPages = materialPages;
} else if (product === 'joy-ui') {
productPages = joyPages;
} else if (product === 'system') {
productPages = systemPages;
}
const pageContextValue = React.useMemo(() => {
let pages = generalPages;
if (product === 'base') {
pages = basePages;
} else if (product === 'material-ui') {
pages = materialPages;
} else if (product === 'joy-ui') {
pages = joyPages;
} else if (product === 'system') {
pages = systemPages;
}

const { activePage, activePageParents } = findActivePage(pages, router.pathname);

const activePage = findActivePage(productPages, router.pathname);
return { activePage, activePageParents, pages };
}, [product, router.pathname]);

let fonts = [];
if (asPathWithoutLang.match(/onepirate/)) {
Expand All @@ -162,11 +166,6 @@ function AppWrapper(props) {
];
}

const pageContextValue = React.useMemo(
() => ({ activePage, pages: productPages }),
[activePage, productPages],
);

return (
<React.Fragment>
<NextHead>
Expand Down
6 changes: 5 additions & 1 deletion docs/src/MuiPage.ts
Expand Up @@ -15,6 +15,7 @@ export interface MuiPage {
/**
* In case the children have pathnames out of pathname value, use this field to scope other pathnames.
* Pathname can be partial, e.g. '/components/' will cover '/components/button/' and '/components/link/'.
* @deprecated Dead code, to remove.
*/
scopePathnames?: string[];
/**
Expand All @@ -24,9 +25,12 @@ export interface MuiPage {
*/
inSideNav?: boolean;
/**
* Props spread to the Link component
* Props spread to the Link component.
*/
linkProps?: Record<string, unknown>;
/**
* Subheader to display before navigation links.
*/
subheader?: string;
/**
* Overrides the default page title.
Expand Down
17 changes: 8 additions & 9 deletions docs/src/modules/components/AppNavDrawer.js
Expand Up @@ -208,7 +208,7 @@ function renderNavItems(options) {
* @param {import('docs/src/pages').MuiPage} context.page
*/
function reduceChildRoutes(context) {
const { onClose, activePage, items, depth, t } = context;
const { onClose, activePageParents, items, depth, t } = context;
let { page } = context;
if (page.inSideNav === false) {
return items;
Expand All @@ -217,10 +217,9 @@ function reduceChildRoutes(context) {
const title = pageToTitleI18n(page, t);

if (page.children && page.children.length >= 1) {
const topLevel = activePage
? activePage.pathname.indexOf(`${page.pathname}`) === 0 ||
page.scopePathnames?.some((pathname) => activePage.pathname.includes(pathname))
: false;
const topLevel =
activePageParents.map((parentPage) => parentPage.pathname).indexOf(page.pathname) !== -1;

let firstChild = page.children[0];

if (firstChild.subheader && firstChild.children) {
Expand All @@ -247,7 +246,7 @@ function reduceChildRoutes(context) {
{renderNavItems({
onClose,
pages: page.children,
activePage,
activePageParents,
depth: subheader ? depth : depth + 1,
t,
})}
Expand Down Expand Up @@ -283,7 +282,7 @@ const iOS = typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigato

export default function AppNavDrawer(props) {
const { className, disablePermanent, mobileOpen, onClose, onOpen } = props;
const { activePage, pages } = React.useContext(PageContext);
const { activePageParents, pages } = React.useContext(PageContext);
const router = useRouter();
const [anchorEl, setAnchorEl] = React.useState(null);
const userLanguage = useUserLanguage();
Expand All @@ -294,7 +293,7 @@ export default function AppNavDrawer(props) {
const drawer = React.useMemo(() => {
const { canonicalAs } = pathnameToLanguage(router.asPath);

const navItems = renderNavItems({ onClose, pages, activePage, depth: 0, t });
const navItems = renderNavItems({ onClose, pages, activePageParents, depth: 0, t });

const renderVersionSelector = (versions, sx) => {
if (!versions?.length) {
Expand Down Expand Up @@ -528,7 +527,7 @@ export default function AppNavDrawer(props) {
{navItems}
</React.Fragment>
);
}, [activePage, pages, onClose, languagePrefix, t, anchorEl, setAnchorEl, router.asPath]);
}, [activePageParents, pages, onClose, languagePrefix, t, anchorEl, setAnchorEl, router.asPath]);

return (
<nav className={className} aria-label={t('mainNavigation')}>
Expand Down
13 changes: 7 additions & 6 deletions docs/src/modules/utils/findActivePage.test.js
Expand Up @@ -10,7 +10,7 @@ describe('findActivePage', () => {
children: [{ pathname: '/getting-started/installation' }],
},
{
pathname: '/components',
pathname: '/react-',
icon: 'ToggleOnIcon',
children: [
{
Expand All @@ -35,22 +35,23 @@ describe('findActivePage', () => {
],
},
];

it('return first level page', () => {
expect(findActivePage(pages, '/getting-started')).to.deep.equal({
expect(findActivePage(pages, '/getting-started').activePage).to.deep.equal({
pathname: '/getting-started',
icon: 'DescriptionIcon',
children: [{ pathname: '/getting-started/installation' }],
});
});

it('return nested page', () => {
expect(findActivePage(pages, '/getting-started/installation')).to.deep.equal({
expect(findActivePage(pages, '/getting-started/installation').activePage).to.deep.equal({
pathname: '/getting-started/installation',
});
});

it('return deep nested page', () => {
expect(findActivePage(pages, '/components/radio-buttons')).to.deep.equal({
expect(findActivePage(pages, '/components/radio-buttons').activePage).to.deep.equal({
pathname: '/components/radio-buttons',
title: 'Radio button',
});
Expand All @@ -60,7 +61,7 @@ describe('findActivePage', () => {
describe('new structure', () => {
const pages = [
{
pathname: '/material-ui/components',
pathname: '/material-ui/react-',
icon: 'ToggleOnIcon',
children: [
{
Expand All @@ -87,7 +88,7 @@ describe('findActivePage', () => {
];

it('return deep nested page', () => {
expect(findActivePage(pages, '/material-ui/react-radio-buttons')).to.deep.equal({
expect(findActivePage(pages, '/material-ui/react-radio-buttons').activePage).to.deep.equal({
pathname: '/material-ui/react-radio-buttons',
title: 'Radio button',
});
Expand Down
35 changes: 28 additions & 7 deletions docs/src/modules/utils/findActivePage.ts
@@ -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,
};
}

0 comments on commit add8ec2

Please sign in to comment.