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

Group templates in sidebar list #57711

Merged
merged 3 commits into from
Jan 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecords } from '@wordpress/core-data';
Expand All @@ -30,20 +31,11 @@ const TemplateItem = ( { postType, postId, ...props } ) => {

export default function SidebarNavigationScreenTemplates() {
const isMobileViewport = useViewportMatch( 'medium', '<' );

const { records: templates, isResolving: isLoading } = useEntityRecords(
'postType',
TEMPLATE_POST_TYPE,
{
per_page: -1,
}
);

const sortedTemplates = templates ? [ ...templates ] : [];
sortedTemplates.sort( ( a, b ) =>
a.title.rendered.localeCompare( b.title.rendered )
{ per_page: -1 }
);

const browseAllLink = useLink( { path: '/wp_template/all' } );
const canCreate = ! isMobileViewport;
return (
Expand All @@ -66,24 +58,7 @@ export default function SidebarNavigationScreenTemplates() {
<>
{ isLoading && __( 'Loading templates…' ) }
{ ! isLoading && (
<ItemGroup>
{ ! templates?.length && (
<Item>{ __( 'No templates found' ) }</Item>
) }
{ sortedTemplates.map( ( template ) => (
<TemplateItem
postType={ TEMPLATE_POST_TYPE }
postId={ template.id }
key={ template.id }
withChevron
>
{ decodeEntities(
template.title?.rendered ||
template.slug
) }
</TemplateItem>
) ) }
</ItemGroup>
<SidebarTemplatesList templates={ templates } />
) }
</>
}
Expand All @@ -97,3 +72,85 @@ export default function SidebarNavigationScreenTemplates() {
/>
);
}

function TemplatesGroup( { title, templates } ) {
return (
<ItemGroup>
{ !! title && (
<Item className="edit-site-sidebar-navigation-screen-templates__templates-group-title">
{ title }
</Item>
) }
{ templates.map( ( template ) => (
<TemplateItem
postType={ TEMPLATE_POST_TYPE }
postId={ template.id }
key={ template.id }
withChevron
>
{ decodeEntities(
template.title?.rendered || template.slug
) }
</TemplateItem>
) ) }
</ItemGroup>
);
}
function SidebarTemplatesList( { templates } ) {
if ( ! templates?.length ) {
return (
<ItemGroup>
<Item>{ __( 'No templates found' ) }</Item>
</ItemGroup>
);
}
const sortedTemplates = templates ? [ ...templates ] : [];
sortedTemplates.sort( ( a, b ) =>
a.title.rendered.localeCompare( b.title.rendered )
);
const { hierarchyTemplates, customTemplates, ...plugins } =
sortedTemplates.reduce(
( accumulator, template ) => {
const {
original_source: originalSource,
author_text: authorText,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since I'm not familiar enough with the domain: any chance that a plugin template could be missing author_text? If so, should we capture those templates in a special group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

author_text is always filled with something. The issue is though that for a while(from the old templates list) we were showing information about the author based on the original_source value. Now it seems by just checking one more plugin(Sensei) that plugins could add templates in different ways without filling the original_source property with plugin value. So I'm not really sure what's the way forward here. Maybe it would be useful to find how most plugins add templates and explore our options..

} = template;
if ( originalSource === 'plugin' ) {
if ( ! accumulator[ authorText ] ) {
accumulator[ authorText ] = [];
}
accumulator[ authorText ].push( template );
} else if ( template.is_custom ) {
accumulator.customTemplates.push( template );
} else {
accumulator.hierarchyTemplates.push( template );
}
return accumulator;
},
{ hierarchyTemplates: [], customTemplates: [] }
);
return (
<VStack spacing={ 3 }>
{ !! hierarchyTemplates.length && (
<TemplatesGroup templates={ hierarchyTemplates } />
) }
{ !! customTemplates.length && (
<TemplatesGroup
title={ __( 'Custom' ) }
templates={ customTemplates }
/>
) }
{ Object.entries( plugins ).map(
( [ plugin, pluginTemplates ] ) => {
return (
<TemplatesGroup
key={ plugin }
title={ plugin }
templates={ pluginTemplates }
/>
);
}
) }
</VStack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.edit-site-sidebar-navigation-screen-templates__templates-group-title.components-item {
text-transform: uppercase;
color: $gray-200;
// 6px right padding to align with + button
padding: $grid-unit-30 6px $grid-unit-20 $grid-unit-20;
border-top: 1px solid $gray-800;
font-size: 11px;
font-weight: 500;
}
1 change: 1 addition & 0 deletions packages/edit-site/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@import "./components/sidebar-navigation-screen-details-footer/style.scss";
@import "./components/sidebar-navigation-screen-navigation-menu/style.scss";
@import "./components/sidebar-navigation-screen-page/style.scss";
@import "./components/sidebar-navigation-screen-templates/style.scss";
@import "components/sidebar-navigation-screen-details-panel/style.scss";
@import "./components/sidebar-navigation-screen-pattern/style.scss";
@import "./components/sidebar-navigation-screen-patterns/style.scss";
Expand Down
Loading