1010 * External dependencies
1111 */
1212import { sortBy } from 'lodash'
13- import { AdvancedToolbarControl } from '..'
1413import { fetchDesignLibrary } from '~stackable/design-library'
1514import { isPro , i18n } from 'stackable'
1615import classnames from 'classnames'
@@ -24,12 +23,14 @@ import {
2423import { __ } from '@wordpress/i18n'
2524import { useLocalStorage } from '~stackable/util'
2625
26+ const isWireframe = design => design . uikit . toLowerCase ( ) === 'wireframes'
27+
2728const BlockList = props => {
2829 const [ uiKitList , setUiKitList ] = useState ( [ ] )
2930 const [ categoryList , setCategoryList ] = useState ( [ ] )
31+ const [ wireframeList , setWireframeList ] = useState ( [ ] )
3032 const [ selected , setSelected ] = useLocalStorage ( 'stk__design_library__block-list__selected' , '' )
31- const [ viewBy , setViewBy ] = useLocalStorage ( 'stk__design_library__block-list__view_by' , 'uikit' )
32- const { apiVersion } = props
33+ const { viewBy, apiVersion } = props
3334
3435 // Create our block list.
3536 useEffect ( ( ) => {
@@ -38,7 +39,8 @@ const BlockList = props => {
3839 const design = designs [ name ]
3940 const { categories, uikit } = design
4041
41- if ( typeof output . uikits [ uikit ] === 'undefined' ) {
42+ // Get all UI Kits. Don't include the Wireframe UI Kit.
43+ if ( typeof output . uikits [ uikit ] === 'undefined' && ! isWireframe ( design ) ) {
4244 output . uikits [ uikit ] = {
4345 id : uikit ,
4446 label : design . uikit ,
@@ -47,18 +49,36 @@ const BlockList = props => {
4749 }
4850 }
4951
50- categories . forEach ( category => {
51- if ( typeof output . categories [ category ] === 'undefined' ) {
52- output . categories [ category ] = {
53- id : category ,
54- label : category ,
55- count : 0 ,
52+ // Get all wireframe categories.
53+ if ( isWireframe ( design ) ) {
54+ categories . forEach ( category => {
55+ if ( typeof output . wireframes [ category ] === 'undefined' ) {
56+ output . wireframes [ category ] = {
57+ id : category ,
58+ label : category ,
59+ count : 0 ,
60+ }
5661 }
57- }
58- } )
62+ } )
63+ } else {
64+ // Get all block design categories.
65+ categories . forEach ( category => {
66+ if ( typeof output . categories [ category ] === 'undefined' ) {
67+ output . categories [ category ] = {
68+ id : category ,
69+ label : category ,
70+ count : 0 ,
71+ }
72+ }
73+ } )
74+ }
5975
6076 return output
61- } , { uikits : { } , categories : { } } )
77+ } , {
78+ uikits : { } ,
79+ categories : { } ,
80+ wireframes : { } ,
81+ } )
6282
6383 let uikitSort = [ 'label' ]
6484 if ( ! isPro ) {
@@ -72,16 +92,23 @@ const BlockList = props => {
7292 label : __ ( 'All' , i18n ) ,
7393 count : 0 ,
7494 } )
95+ const wireframes = sortBy ( Object . values ( designList . wireframes ) , 'label' )
96+ wireframes . unshift ( {
97+ id : 'all' ,
98+ label : __ ( 'All' , i18n ) ,
99+ count : 0 ,
100+ } )
75101
76102 setUiKitList ( uikits )
77103 setCategoryList ( categories )
104+ setWireframeList ( wireframes )
78105 } )
79106 } , [ apiVersion ] )
80107
81108 // Update the counts of the designs, but don't update the list, only the counts.
82109 useEffect ( ( ) => {
83110 // If these are empty, then our component hasn't finished initializing.
84- if ( ! uiKitList . length || ! categoryList . length ) {
111+ if ( ! uiKitList . length || ! categoryList . length || ! wireframeList . length ) {
85112 return
86113 }
87114
@@ -99,11 +126,31 @@ const BlockList = props => {
99126 }
100127 return categories
101128 } , { } )
129+ const newWireframes = wireframeList . reduce ( ( categories , category ) => {
130+ categories [ category . id ] = {
131+ ...category ,
132+ count : 0 ,
133+ }
134+ return categories
135+ } , { } )
102136
103137 props . designs . forEach ( design => {
138+ // Gather all wireframe designs.
139+ if ( isWireframe ( design ) ) {
140+ design . categories . forEach ( category => {
141+ if ( category && newWireframes [ category ] ) {
142+ newWireframes [ category ] . count ++
143+ }
144+ } )
145+ return
146+ }
147+
148+ // Gather all ui kit designs.
104149 if ( design . uikit && newUiKits [ design . uikit ] ) {
105150 newUiKits [ design . uikit ] . count ++
106151 }
152+
153+ // Gather all block design categories.
107154 design . categories . forEach ( category => {
108155 if ( category && newCategories [ category ] ) {
109156 newCategories [ category ] . count ++
@@ -119,50 +166,47 @@ const BlockList = props => {
119166
120167 // Sort the categories so that the "All" is first.
121168 if ( newCategories . all ) {
122- newCategories . all . count = props . designs . length
169+ newCategories . all . count = props . designs . filter ( design => ! isWireframe ( design ) ) . length
123170 newCategories . all . label = ' ' // Spaces so we will be first when sorting.
124171 }
125172 const sorted = sortBy ( Object . values ( newCategories ) , 'label' )
126173 if ( sorted [ 0 ] ) {
127174 sorted [ 0 ] . label = __ ( 'All' , i18n )
128175 }
129176 setCategoryList ( sorted )
130- } , [ props . designs . length , JSON . stringify ( uiKitList ) , JSON . stringify ( categoryList ) ] )
177+
178+ // Sort the wireframes so that the "All" is first.
179+ if ( newWireframes . all ) {
180+ newWireframes . all . count = props . designs . filter ( isWireframe ) . length
181+ newWireframes . all . label = ' ' // Spaces so we will be first when sorting.
182+ }
183+ const sortedWireframes = sortBy ( Object . values ( newWireframes ) , 'label' )
184+ if ( sortedWireframes [ 0 ] ) {
185+ sortedWireframes [ 0 ] . label = __ ( 'All' , i18n )
186+ }
187+ setWireframeList ( sortedWireframes )
188+ } , [ props . designs . length , JSON . stringify ( uiKitList ) , JSON . stringify ( categoryList ) , JSON . stringify ( wireframeList ) ] )
131189
132190 useEffect ( ( ) => {
133191 // If these are empty, then our component hasn't finished initializing.
134- if ( ! uiKitList . length || ! categoryList . length ) {
192+ if ( ! uiKitList . length || ! categoryList . length || ! wireframeList . length ) {
135193 return
136194 }
137195
138196 setSelected ( viewBy === 'uikit' ? uiKitList [ 0 ] . id : 'all' )
139197 } , [ viewBy ] )
140198
141199 useEffect ( ( ) => {
142- props . onSelect ( { id : selected , type : viewBy } )
200+ props . onSelect ( selected )
143201 } , [ selected ] )
144202
203+ const list = viewBy === 'uikit'
204+ ? uiKitList : viewBy === 'category'
205+ ? categoryList : wireframeList
206+
145207 return (
146208 < ul className = "ugb-block-list" >
147- < AdvancedToolbarControl
148- controls = { [
149- {
150- value : 'uikit' ,
151- title : __ ( 'UI Kits' , i18n ) ,
152- } ,
153- {
154- value : 'category' ,
155- title : __ ( 'Categories' , i18n ) ,
156- } ,
157- ] }
158- value = { viewBy }
159- onChange = { setViewBy }
160- isSmall = { true }
161- fullwidth = { false }
162- isToggleOnly = { true }
163- allowReset = { false }
164- />
165- { ( viewBy === 'uikit' ? uiKitList : categoryList ) . reduce ( ( list , itemData ) => {
209+ { list . reduce ( ( list , itemData ) => {
166210 const {
167211 id,
168212 label,
0 commit comments