-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Site Editor > Patterns & Parts: generate sidebar from view config #76823
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 all commits
085acc8
1eaf773
6a5e7bf
401b7d3
cbba167
8c87579
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,11 @@ import { | |
| __experimentalItem as Item, | ||
| } from '@wordpress/components'; | ||
| import { getTemplatePartIcon } from '@wordpress/editor'; | ||
| import { useMemo } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { file } from '@wordpress/icons'; | ||
| import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
| import { useViewConfig } from '@wordpress/views'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -28,70 +30,44 @@ import { unlock } from '../../lock-unlock'; | |
| const { useLocation } = unlock( routerPrivateApis ); | ||
|
|
||
| function CategoriesGroup( { | ||
| templatePartAreas, | ||
| patternCategories, | ||
| templatePartViews, | ||
| patternViews, | ||
| templatePartCounts, | ||
| patternCounts, | ||
| currentCategory, | ||
| currentType, | ||
| } ) { | ||
| const [ allPatterns, ...otherPatterns ] = patternCategories; | ||
|
|
||
| return ( | ||
| <ItemGroup className="edit-site-sidebar-navigation-screen-patterns__group"> | ||
| <CategoryItem | ||
| key="all" | ||
| count={ Object.values( templatePartAreas ) | ||
| .map( ( { templateParts } ) => templateParts?.length || 0 ) | ||
| .reduce( ( acc, val ) => acc + val, 0 ) } | ||
| icon={ getTemplatePartIcon() } /* no name, so it provides the fallback icon */ | ||
| label={ __( 'All template parts' ) } | ||
| id={ TEMPLATE_PART_ALL_AREAS_CATEGORY } | ||
| type={ TEMPLATE_PART_POST_TYPE } | ||
| isActive={ | ||
| currentCategory === TEMPLATE_PART_ALL_AREAS_CATEGORY && | ||
| currentType === TEMPLATE_PART_POST_TYPE | ||
| } | ||
| /> | ||
| { Object.entries( templatePartAreas ).map( | ||
| ( [ area, { label, templateParts, icon } ] ) => ( | ||
| <CategoryItem | ||
| key={ area } | ||
| count={ templateParts?.length } | ||
| icon={ getTemplatePartIcon( icon ) } | ||
| label={ label } | ||
| id={ area } | ||
| type={ TEMPLATE_PART_POST_TYPE } | ||
| isActive={ | ||
| currentCategory === area && | ||
| currentType === TEMPLATE_PART_POST_TYPE | ||
| } | ||
| /> | ||
| ) | ||
| ) } | ||
| <div className="edit-site-sidebar-navigation-screen-patterns__divider" /> | ||
| { allPatterns && ( | ||
| { templatePartViews?.map( ( view ) => ( | ||
| <CategoryItem | ||
| key={ allPatterns.name } | ||
| count={ allPatterns.count } | ||
| label={ allPatterns.label } | ||
| icon={ file } | ||
| id={ allPatterns.name } | ||
| type={ PATTERN_TYPES.user } | ||
| key={ view.slug } | ||
| count={ templatePartCounts[ view.slug ] } | ||
| icon={ getTemplatePartIcon( | ||
| view.slug === TEMPLATE_PART_ALL_AREAS_CATEGORY | ||
| ? undefined | ||
| : view.slug | ||
| ) } | ||
| label={ view.title } | ||
| id={ view.slug } | ||
| type={ TEMPLATE_PART_POST_TYPE } | ||
| isActive={ | ||
| currentCategory === `${ allPatterns.name }` && | ||
| currentType === PATTERN_TYPES.user | ||
| currentCategory === view.slug && | ||
| currentType === TEMPLATE_PART_POST_TYPE | ||
| } | ||
| /> | ||
| ) } | ||
| { otherPatterns.map( ( category ) => ( | ||
| ) ) } | ||
| <div className="edit-site-sidebar-navigation-screen-patterns__divider" /> | ||
| { patternViews?.map( ( view ) => ( | ||
| <CategoryItem | ||
| key={ category.name } | ||
| count={ category.count } | ||
| label={ category.label } | ||
| key={ view.slug } | ||
| count={ patternCounts[ view.slug ] } | ||
| label={ view.title } | ||
| icon={ file } | ||
| id={ category.name } | ||
| id={ view.slug } | ||
| type={ PATTERN_TYPES.user } | ||
| isActive={ | ||
| currentCategory === `${ category.name }` && | ||
| currentCategory === `${ view.slug }` && | ||
| currentType === PATTERN_TYPES.user | ||
| } | ||
| /> | ||
|
|
@@ -110,9 +86,38 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) { | |
| ? PATTERN_DEFAULT_CATEGORY | ||
| : TEMPLATE_PART_ALL_AREAS_CATEGORY ); | ||
|
|
||
| const { templatePartAreas, hasTemplateParts, isLoading } = | ||
| const { view_list: templatePartViews } = useViewConfig( { | ||
| kind: 'postType', | ||
| name: TEMPLATE_PART_POST_TYPE, | ||
| } ); | ||
| const { view_list: patternViews } = useViewConfig( { | ||
| kind: 'postType', | ||
| name: PATTERN_TYPES.user, | ||
| } ); | ||
|
|
||
| const { templatePartAreas, isLoading, hasTemplateParts } = | ||
| useTemplatePartAreas(); | ||
| const { patternCategories, hasPatterns } = usePatternCategories(); | ||
| const templatePartCounts = useMemo( () => { | ||
| const counts = { [ TEMPLATE_PART_ALL_AREAS_CATEGORY ]: 0 }; | ||
| Object.entries( templatePartAreas ).forEach( | ||
| ( [ area, { templateParts } ] ) => { | ||
| const count = templateParts?.length || 0; | ||
| counts[ area ] = count; | ||
| counts[ TEMPLATE_PART_ALL_AREAS_CATEGORY ] += count; | ||
| } | ||
| ); | ||
| return counts; | ||
| }, [ templatePartAreas ] ); | ||
| const { patternCategories } = usePatternCategories(); | ||
| const patternCounts = useMemo( () => { | ||
| const counts = {}; | ||
| patternCategories.forEach( ( cat ) => { | ||
| counts[ cat.name ] = cat.count; | ||
| } ); | ||
| return counts; | ||
| }, [ patternCategories ] ); | ||
|
|
||
| const hasPatterns = patternCounts[ PATTERN_DEFAULT_CATEGORY ] > 0; | ||
|
|
||
| return ( | ||
| <SidebarNavigationScreen | ||
|
|
@@ -133,8 +138,10 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) { | |
| </ItemGroup> | ||
| ) } | ||
| <CategoriesGroup | ||
| templatePartAreas={ templatePartAreas } | ||
| patternCategories={ patternCategories } | ||
| templatePartViews={ templatePartViews } | ||
| patternViews={ patternViews } | ||
| templatePartCounts={ templatePartCounts } | ||
| patternCounts={ patternCounts } | ||
|
Comment on lines
+143
to
+144
Member
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. Computing the counts per category client-side is a design decision, the same as retrieving the icons client-side. The rationale for this is that the experimental site editor doesn't have any, so I'm not convinced counts/icon need to be part of the endpoint. If that changes, we can add them later — which is easier than removing them once they've part of the endpoint. |
||
| currentCategory={ currentCategory } | ||
| currentType={ postType } | ||
| /> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.