-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Group templates in sidebar list #57711
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -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 ( | ||
|
|
@@ -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 } /> | ||
| ) } | ||
| </> | ||
| } | ||
|
|
@@ -97,3 +72,86 @@ 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 { themeTemplates, customTemplates, ...pluginTemplates } = | ||
| sortedTemplates.reduce( | ||
| ( accumulator, template ) => { | ||
| const { | ||
| original_source: originalSource, | ||
| author_text: authorText, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } = template; | ||
| if ( originalSource === 'plugin' ) { | ||
| if ( ! accumulator[ authorText ] ) { | ||
| accumulator[ authorText ] = []; | ||
| } | ||
| accumulator[ authorText ].push( template ); | ||
| } else if ( template.is_custom ) { | ||
| accumulator.customTemplates.push( template ); | ||
| } else { | ||
| accumulator.themeTemplates.push( template ); | ||
| } | ||
| return accumulator; | ||
| }, | ||
| { themeTemplates: [], customTemplates: [] } | ||
| ); | ||
| return ( | ||
| <VStack spacing={ 3 }> | ||
| { !! themeTemplates.length && ( | ||
| <TemplatesGroup templates={ themeTemplates } /> | ||
| ) } | ||
| { !! customTemplates.length && ( | ||
| <TemplatesGroup | ||
| title={ __( 'Custom' ) } | ||
| templates={ customTemplates } | ||
| /> | ||
| ) } | ||
| { !! Object.keys( pluginTemplates ).length && | ||
| Object.entries( pluginTemplates ).map( | ||
| ( [ plugin, _pluginTemplates ] ) => { | ||
ntsekouras marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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-600; | ||
| // 6px right padding to align with + button | ||
| padding: $grid-unit-10 6px $grid-unit-10 $grid-unit-20; | ||
| border: none; | ||
| min-height: $grid-unit-50; | ||
| border-radius: $radius-block-ui; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.