@@ -6,9 +6,11 @@ import {
66 __experimentalItem as Item ,
77} from '@wordpress/components' ;
88import { getTemplatePartIcon } from '@wordpress/editor' ;
9+ import { useMemo } from '@wordpress/element' ;
910import { __ } from '@wordpress/i18n' ;
1011import { file } from '@wordpress/icons' ;
1112import { privateApis as routerPrivateApis } from '@wordpress/router' ;
13+ import { useViewConfig } from '@wordpress/views' ;
1214
1315/**
1416 * Internal dependencies
@@ -28,70 +30,44 @@ import { unlock } from '../../lock-unlock';
2830const { useLocation } = unlock ( routerPrivateApis ) ;
2931
3032function CategoriesGroup ( {
31- templatePartAreas,
32- patternCategories,
33+ templatePartViews,
34+ patternViews,
35+ templatePartCounts,
36+ patternCounts,
3337 currentCategory,
3438 currentType,
3539} ) {
36- const [ allPatterns , ...otherPatterns ] = patternCategories ;
37-
3840 return (
3941 < ItemGroup className = "edit-site-sidebar-navigation-screen-patterns__group" >
40- < CategoryItem
41- key = "all"
42- count = { Object . values ( templatePartAreas )
43- . map ( ( { templateParts } ) => templateParts ?. length || 0 )
44- . reduce ( ( acc , val ) => acc + val , 0 ) }
45- icon = { getTemplatePartIcon ( ) } /* no name, so it provides the fallback icon */
46- label = { __ ( 'All template parts' ) }
47- id = { TEMPLATE_PART_ALL_AREAS_CATEGORY }
48- type = { TEMPLATE_PART_POST_TYPE }
49- isActive = {
50- currentCategory === TEMPLATE_PART_ALL_AREAS_CATEGORY &&
51- currentType === TEMPLATE_PART_POST_TYPE
52- }
53- />
54- { Object . entries ( templatePartAreas ) . map (
55- ( [ area , { label, templateParts, icon } ] ) => (
56- < CategoryItem
57- key = { area }
58- count = { templateParts ?. length }
59- icon = { getTemplatePartIcon ( icon ) }
60- label = { label }
61- id = { area }
62- type = { TEMPLATE_PART_POST_TYPE }
63- isActive = {
64- currentCategory === area &&
65- currentType === TEMPLATE_PART_POST_TYPE
66- }
67- />
68- )
69- ) }
70- < div className = "edit-site-sidebar-navigation-screen-patterns__divider" />
71- { allPatterns && (
42+ { templatePartViews ?. map ( ( view ) => (
7243 < CategoryItem
73- key = { allPatterns . name }
74- count = { allPatterns . count }
75- label = { allPatterns . label }
76- icon = { file }
77- id = { allPatterns . name }
78- type = { PATTERN_TYPES . user }
44+ key = { view . slug }
45+ count = { templatePartCounts [ view . slug ] }
46+ icon = { getTemplatePartIcon (
47+ view . slug === TEMPLATE_PART_ALL_AREAS_CATEGORY
48+ ? undefined
49+ : view . slug
50+ ) }
51+ label = { view . title }
52+ id = { view . slug }
53+ type = { TEMPLATE_PART_POST_TYPE }
7954 isActive = {
80- currentCategory === ` ${ allPatterns . name } ` &&
81- currentType === PATTERN_TYPES . user
55+ currentCategory === view . slug &&
56+ currentType === TEMPLATE_PART_POST_TYPE
8257 }
8358 />
84- ) }
85- { otherPatterns . map ( ( category ) => (
59+ ) ) }
60+ < div className = "edit-site-sidebar-navigation-screen-patterns__divider" />
61+ { patternViews ?. map ( ( view ) => (
8662 < CategoryItem
87- key = { category . name }
88- count = { category . count }
89- label = { category . label }
63+ key = { view . slug }
64+ count = { patternCounts [ view . slug ] }
65+ label = { view . title }
9066 icon = { file }
91- id = { category . name }
67+ id = { view . slug }
9268 type = { PATTERN_TYPES . user }
9369 isActive = {
94- currentCategory === `${ category . name } ` &&
70+ currentCategory === `${ view . slug } ` &&
9571 currentType === PATTERN_TYPES . user
9672 }
9773 />
@@ -110,9 +86,38 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) {
11086 ? PATTERN_DEFAULT_CATEGORY
11187 : TEMPLATE_PART_ALL_AREAS_CATEGORY ) ;
11288
113- const { templatePartAreas, hasTemplateParts, isLoading } =
89+ const { view_list : templatePartViews } = useViewConfig ( {
90+ kind : 'postType' ,
91+ name : TEMPLATE_PART_POST_TYPE ,
92+ } ) ;
93+ const { view_list : patternViews } = useViewConfig ( {
94+ kind : 'postType' ,
95+ name : PATTERN_TYPES . user ,
96+ } ) ;
97+
98+ const { templatePartAreas, isLoading, hasTemplateParts } =
11499 useTemplatePartAreas ( ) ;
115- const { patternCategories, hasPatterns } = usePatternCategories ( ) ;
100+ const templatePartCounts = useMemo ( ( ) => {
101+ const counts = { [ TEMPLATE_PART_ALL_AREAS_CATEGORY ] : 0 } ;
102+ Object . entries ( templatePartAreas ) . forEach (
103+ ( [ area , { templateParts } ] ) => {
104+ const count = templateParts ?. length || 0 ;
105+ counts [ area ] = count ;
106+ counts [ TEMPLATE_PART_ALL_AREAS_CATEGORY ] += count ;
107+ }
108+ ) ;
109+ return counts ;
110+ } , [ templatePartAreas ] ) ;
111+ const { patternCategories } = usePatternCategories ( ) ;
112+ const patternCounts = useMemo ( ( ) => {
113+ const counts = { } ;
114+ patternCategories . forEach ( ( cat ) => {
115+ counts [ cat . name ] = cat . count ;
116+ } ) ;
117+ return counts ;
118+ } , [ patternCategories ] ) ;
119+
120+ const hasPatterns = patternCounts [ PATTERN_DEFAULT_CATEGORY ] > 0 ;
116121
117122 return (
118123 < SidebarNavigationScreen
@@ -133,8 +138,10 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) {
133138 </ ItemGroup >
134139 ) }
135140 < CategoriesGroup
136- templatePartAreas = { templatePartAreas }
137- patternCategories = { patternCategories }
141+ templatePartViews = { templatePartViews }
142+ patternViews = { patternViews }
143+ templatePartCounts = { templatePartCounts }
144+ patternCounts = { patternCounts }
138145 currentCategory = { currentCategory }
139146 currentType = { postType }
140147 />
0 commit comments