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

Page List: Fix ESLint warnings #52267

Merged
merged 1 commit into from
Jul 4, 2023
Merged
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
105 changes: 54 additions & 51 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,63 +183,66 @@ export default function PageListEdit( {
style: { ...context.style?.color },
} );

const getBlockList = ( parentId = parentPageID ) => {
const childPages = pagesByParentId.get( parentId );
const pagesTree = useMemo(
function makePagesTree( parentId = 0, level = 0 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not from this PR, but it's a bit weird to see the function declaration inside useMemo. Can't we declare the function outside the components?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as it's valid JS, I don't see a problem :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets fix this in a followup.

const childPages = pagesByParentId.get( parentId );

if ( ! childPages?.length ) {
return [];
}

return childPages.reduce( ( template, page ) => {
const hasChildren = pagesByParentId.has( page.id );
const pageProps = {
id: page.id,
label:
// translators: displayed when a page has an empty title.
page.title?.rendered?.trim() !== ''
? page.title?.rendered
: __( '(no title)' ),
title: page.title?.rendered,
link: page.url,
hasChildren,
};
let item = null;
const children = getBlockList( page.id );
item = createBlock( 'core/page-list-item', pageProps, children );
template.push( item );

return template;
}, [] );
};
if ( ! childPages?.length ) {
return [];
}

const makePagesTree = ( parentId = 0, level = 0 ) => {
const childPages = pagesByParentId.get( parentId );
return childPages.reduce( ( tree, page ) => {
const hasChildren = pagesByParentId.has( page.id );
const item = {
value: page.id,
label: '— '.repeat( level ) + page.title.rendered,
rawName: page.title.rendered,
};
tree.push( item );
if ( hasChildren ) {
tree.push( ...makePagesTree( page.id, level + 1 ) );
}
return tree;
}, [] );
},
[ pagesByParentId ]
);

if ( ! childPages?.length ) {
return [];
}
const blockList = useMemo(
function getBlockList( parentId = parentPageID ) {
const childPages = pagesByParentId.get( parentId );

return childPages.reduce( ( tree, page ) => {
const hasChildren = pagesByParentId.has( page.id );
const item = {
value: page.id,
label: '— '.repeat( level ) + page.title.rendered,
rawName: page.title.rendered,
};
tree.push( item );
if ( hasChildren ) {
tree.push( ...makePagesTree( page.id, level + 1 ) );
if ( ! childPages?.length ) {
return [];
}
return tree;
}, [] );
};

const pagesTree = useMemo( makePagesTree, [ pagesByParentId ] );

const blockList = useMemo( getBlockList, [
pagesByParentId,
parentPageID,
] );
return childPages.reduce( ( template, page ) => {
const hasChildren = pagesByParentId.has( page.id );
const pageProps = {
id: page.id,
label:
// translators: displayed when a page has an empty title.
page.title?.rendered?.trim() !== ''
? page.title?.rendered
: __( '(no title)' ),
title: page.title?.rendered,
link: page.url,
hasChildren,
};
let item = null;
const children = getBlockList( page.id );
item = createBlock(
'core/page-list-item',
pageProps,
children
);
template.push( item );

return template;
}, [] );
},
[ pagesByParentId, parentPageID ]
);

const {
isNested,
Expand Down