Skip to content

Commit

Permalink
Performance: Optimize the AddTemplate component used in data views pa…
Browse files Browse the repository at this point in the history
…ges (#60586)

Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ellatrix <[email protected]>
  • Loading branch information
3 people authored Apr 9, 2024
1 parent d579009 commit 5e99454
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 523 deletions.
424 changes: 412 additions & 12 deletions packages/edit-site/src/components/add-new-template/index.js

Large diffs are not rendered by default.

429 changes: 0 additions & 429 deletions packages/edit-site/src/components/add-new-template/new-template.js

This file was deleted.

22 changes: 0 additions & 22 deletions packages/edit-site/src/components/add-new-template/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,6 @@
}
}

.edit-site-template-actions-loading-screen-modal {
backdrop-filter: none;
background-color: transparent;

&.is-full-screen {
background-color: $white;
box-shadow: 0 0 0 transparent;
min-width: 100%;
min-height: 100%;
}

&__content {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
}

.edit-site-add-new-template__modal {
max-width: $grid-unit-80 * 13;
width: calc(100% - #{$grid-unit-80});
Expand Down

This file was deleted.

52 changes: 35 additions & 17 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { blockMeta, post, archive } from '@wordpress/icons';
*/
import { TEMPLATE_POST_TYPE } from '../../utils/constants';

const EMPTY_OBJECT = {};

/**
* @typedef IHasNameAndId
* @property {string|number} id The entity's id.
Expand Down Expand Up @@ -622,41 +624,57 @@ const useTemplatesToExclude = (
const useEntitiesInfo = (
entityName,
templatePrefixes,
additionalQueryParameters = {}
additionalQueryParameters = EMPTY_OBJECT
) => {
const recordsToExcludePerEntity = useTemplatesToExclude(
entityName,
templatePrefixes,
additionalQueryParameters
);
const entitiesInfo = useSelect(
const entitiesHasRecords = useSelect(
( select ) => {
return Object.keys( templatePrefixes || {} ).reduce(
( accumulator, slug ) => {
const existingEntitiesIds =
recordsToExcludePerEntity?.[ slug ]?.map(
( { id } ) => id
) || [];
accumulator[ slug ] = {
hasEntities: !! select( coreStore ).getEntityRecords(
entityName,
slug,
{
per_page: 1,
_fields: 'id',
context: 'view',
exclude: existingEntitiesIds,
...additionalQueryParameters[ slug ],
}
)?.length,
existingEntitiesIds,
};
accumulator[ slug ] = !! select(
coreStore
).getEntityRecords( entityName, slug, {
per_page: 1,
_fields: 'id',
context: 'view',
exclude: existingEntitiesIds,
...additionalQueryParameters[ slug ],
} )?.length;
return accumulator;
},
{}
);
},
[ templatePrefixes, recordsToExcludePerEntity ]
[
templatePrefixes,
recordsToExcludePerEntity,
entityName,
additionalQueryParameters,
]
);
const entitiesInfo = useMemo( () => {
return Object.keys( templatePrefixes || {} ).reduce(
( accumulator, slug ) => {
const existingEntitiesIds =
recordsToExcludePerEntity?.[ slug ]?.map(
( { id } ) => id
) || [];
accumulator[ slug ] = {
hasEntities: entitiesHasRecords[ slug ],
existingEntitiesIds,
};
return accumulator;
},
{}
);
}, [ templatePrefixes, recordsToExcludePerEntity, entitiesHasRecords ] );
return entitiesInfo;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import { useState, memo } from '@wordpress/element';
import { Button } from '@wordpress/components';

/**
Expand All @@ -17,7 +17,7 @@ import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants';

const { useHistory } = unlock( routerPrivateApis );

export default function AddNewTemplatePart() {
function AddNewTemplatePart() {
const { canCreate, postType } = useSelect( ( select ) => {
const { supportsTemplatePartsMode } =
select( editSiteStore ).getSettings();
Expand Down Expand Up @@ -58,3 +58,5 @@ export default function AddNewTemplatePart() {
</>
);
}

export default memo( AddNewTemplatePart );
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,7 @@ export default function PageTemplatesTemplateParts( { postType } ) {
}
actions={
postType === TEMPLATE_POST_TYPE ? (
<AddNewTemplate
templateType={ postType }
showIcon={ false }
toggleProps={ { variant: 'primary' } }
/>
<AddNewTemplate />
) : (
<AddNewTemplatePart />
)
Expand Down
14 changes: 1 addition & 13 deletions test/e2e/specs/site-editor/site-editor-url-navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ test.describe( 'Site editor url navigation', () => {
status: 'publish',
} );
await admin.visitSiteEditor();
// We need to wait the response from the `posts` request in order to click the augmented menu item,
// that includes the options for creating templates for specific posts.
await Promise.all( [
page.waitForResponse(
( resp ) =>
resp
.url()
.includes(
'/index.php?rest_route=%2Fwp%2Fv2%2Fposts&context=view'
) && resp.status() === 200
),
page.click( 'role=button[name="Templates"]' ),
] );
await page.click( 'role=button[name="Templates"]' );
await page.click( 'role=button[name="Add New Template"i]' );
await page
.getByRole( 'button', {
Expand Down

0 comments on commit 5e99454

Please sign in to comment.