Skip to content

Commit 7210e8c

Browse files
authored
fix (admin): use the translated roles from editorRoles for the search tree (#3580)
* fix: use the translated roles from editorRoles for the search tree * fix: add the previous values as fallback * fix: do not search if current search string is empty * fix: remove fallback
1 parent 2241a1e commit 7210e8c

File tree

1 file changed

+43
-50
lines changed

1 file changed

+43
-50
lines changed

src/welcome/admin.js

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
isPro,
3030
v2disabledBlocks,
3131
defaultBreakpoints,
32+
editorRoles,
3233
} from 'stackable'
3334
import classnames from 'classnames'
3435
import { fetchSettings, importBlocks } from '~stackable/util/admin'
@@ -166,14 +167,7 @@ const SEARCH_TREE = [
166167
groups: [
167168
{
168169
id: 'role-manager',
169-
children: [
170-
__( 'Role Manager', i18n ),
171-
__( 'Administrator', i18n ),
172-
__( 'Editor', i18n ),
173-
__( 'Author', i18n ),
174-
__( 'Contributor', i18n ),
175-
__( 'Subscriber', i18n ),
176-
],
170+
children: Object.values( editorRoles || {} ),
177171
},
178172
],
179173
},
@@ -185,11 +179,7 @@ const SEARCH_TREE = [
185179
id: 'custom-fields-settings',
186180
children: [
187181
__( 'Custom Fields', i18n ),
188-
__( 'Administrator', i18n ),
189-
__( 'Editor', i18n ),
190-
__( 'Author', i18n ),
191-
__( 'Contributor', i18n ),
192-
__( 'Subscriber', i18n ),
182+
...Object.values( editorRoles || {} ),
193183
],
194184
},
195185
],
@@ -578,13 +568,13 @@ const Settings = () => {
578568
}, [ hasUnsavedChanges ] )
579569

580570
const filteredSearchTree = useMemo( () => {
581-
if ( ! currentSearch ) {
582-
return SEARCH_TREE
583-
}
584571
const loweredSearch = currentSearch.toLowerCase()
585572

586573
return SEARCH_TREE.map( tabs => {
587574
const filtedGroups = tabs.groups.map( group => {
575+
if ( ! currentSearch ) {
576+
return { ...group, children: null }
577+
}
588578
const filteredChildren = group.children.filter( child => {
589579
return child.toLowerCase().includes( loweredSearch.toLowerCase() )
590580
} )
@@ -597,6 +587,7 @@ const Settings = () => {
597587
const props = {
598588
settings,
599589
handleSettingsChange,
590+
currentSearch,
600591
filteredSearchTree,
601592
currentTab,
602593
isFetching,
@@ -643,15 +634,15 @@ const EditorSettings = props => {
643634
const editor = groups.find( group => group.id === 'editor' )
644635
const toolbar = groups.find( group => group.id === 'toolbar' )
645636
const inspector = groups.find( group => group.id === 'inspector' )
646-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
637+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
647638

648639
return (
649640
<div className="s-editor-settings">
650-
{ groupLength <= 0 ? (
641+
{ ! hasGroupMatch ? (
651642
<h3>{ __( 'No matching settings', i18n ) }</h3>
652643
) : (
653644
<>
654-
{ blocks.children.length > 0 &&
645+
{ ( blocks.children === null || blocks.children.length > 0 ) &&
655646
<div className="s-setting-group">
656647
<h2>{ __( 'Block Widths', i18n ) }</h2>
657648
<p className="s-settings-subtitle">{ __( 'Adjust the width of Stackable blocks here.', i18n ) }</p>
@@ -677,7 +668,7 @@ const EditorSettings = props => {
677668
/>
678669
</div>
679670
}
680-
{ editor.children.length > 0 &&
671+
{ ( editor.children === null || editor.children.length > 0 ) &&
681672
<div className="s-setting-group">
682673
<h2>{ __( 'Editor', i18n ) }</h2>
683674
<p className="s-settings-subtitle">{ __( 'You can customize some of the features and behavior of Stackable in the editor here.', i18n ) } </p>
@@ -736,7 +727,7 @@ const EditorSettings = props => {
736727
/>
737728
</div>
738729
}
739-
{ toolbar.children.length > 0 &&
730+
{ ( toolbar.children === null || toolbar.children.length > 0 ) &&
740731
<div className="s-setting-group">
741732
<h2>{ __( 'Toolbar', i18n ) }</h2>
742733
<p className="s-settings-subtitle">{ __( 'You can disable some toolbar features here.', i18n ) } </p>
@@ -787,7 +778,7 @@ const EditorSettings = props => {
787778
/> }
788779
</div>
789780
}
790-
{ inspector.children.length > 0 &&
781+
{ ( inspector.children === null || inspector.children.length > 0 ) &&
791782
<div className="s-setting-group">
792783
<h2>{ __( 'Inspector', i18n ) }</h2>
793784
<p className="s-settings-subtitle">{ __( 'You can customize some of the features and behavior of Stackable in the inspector here.', i18n ) }</p>
@@ -826,15 +817,15 @@ const Responsiveness = props => {
826817

827818
const groups = filteredSearchTree.find( tab => tab.id === 'responsiveness' ).groups
828819
const dynamicBreakpoints = groups.find( group => group.id === 'dynamic-breakpoints' )
829-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
820+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
830821

831822
return (
832823
<div className="s-responsiveness">
833-
{ groupLength <= 0 ? (
824+
{ ! hasGroupMatch ? (
834825
<h3>{ __( 'No matching settings', i18n ) }</h3>
835826
) : (
836827
<>
837-
{ dynamicBreakpoints.children.length > 0 &&
828+
{ ( dynamicBreakpoints.children === null || dynamicBreakpoints.children.length > 0 ) &&
838829
<div className="s-setting-group">
839830
<h2>{ __( 'Dynamic Breakpoints', i18n ) }</h2>
840831
<p className="s-settings-subtitle">
@@ -898,7 +889,7 @@ const Blocks = props => {
898889
} )
899890
const DERIVED_BLOCKS = getAllBlocks()
900891
const groups = filteredSearchTree.find( tab => tab.id === 'blocks' ).groups
901-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
892+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
902893
const disabledBlocks = settings.stackable_block_states ?? {} // eslint-disable-line camelcase
903894

904895
const [ isDisabledDialogOpen, setIsDisabledDialogOpen ] = useState( false )
@@ -1079,7 +1070,7 @@ const Blocks = props => {
10791070
) }
10801071

10811072
<div className="s-blocks">
1082-
{ groupLength <= 0 ? (
1073+
{ ! hasGroupMatch ? (
10831074
<h3>{ __( 'No matching settings', i18n ) }</h3>
10841075
) : (
10851076
<div className="s-setting-group">
@@ -1098,7 +1089,7 @@ const Blocks = props => {
10981089
`s-box-block__title--${ id }`,
10991090
] )
11001091
const group = groups.find( group => group.id === id )
1101-
return group.children.length > 0 && (
1092+
return ( group.children === null || group.children.length > 0 ) && (
11021093
<div className="s-box s-box-block" key={ id }>
11031094
<h3 className={ classes }>
11041095
{ Icon && <Icon height="20" width="20" /> }
@@ -1165,15 +1156,15 @@ const Optimizations = props => {
11651156

11661157
const groups = filteredSearchTree.find( tab => tab.id === 'optimizations' ).groups
11671158
const optimizations = groups.find( group => group.id === 'optimizations' )
1168-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
1159+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
11691160

11701161
return (
11711162
<div className="s-optimizations">
1172-
{ groupLength <= 0 ? (
1163+
{ ! hasGroupMatch ? (
11731164
<h3>{ __( 'No matching settings', i18n ) }</h3>
11741165
) : (
11751166
<>
1176-
{ optimizations.children.length > 0 &&
1167+
{ ( optimizations.children === null || optimizations.children.length > 0 ) &&
11771168
<div className="s-setting-group">
11781169
<h2>{ __( 'Optimizations', i18n ) }</h2>
11791170
<p className="s-settings-subtitle">{ __( 'Here you can adjust some optimization settings that are performed by Stackable.', i18n ) }</p>
@@ -1206,15 +1197,15 @@ const Optimizations = props => {
12061197
const GlobalSettings = props => {
12071198
const groups = props.filteredSearchTree.find( tab => tab.id === 'global-settings' ).groups
12081199
const globalSettings = groups.find( group => group.id === 'global-settings' )
1209-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
1200+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
12101201

12111202
return (
12121203
<div className="s-global-settings">
1213-
{ groupLength <= 0 ? (
1204+
{ ! hasGroupMatch ? (
12141205
<h3>{ __( 'No matching settings', i18n ) }</h3>
12151206
) : (
12161207
<>
1217-
{ globalSettings.children.length > 0 &&
1208+
{ ( globalSettings.children === null || globalSettings.children.length > 0 ) &&
12181209
<div className="s-setting-group">
12191210
<h2>{ __( 'Global Settings', i18n ) }</h2>
12201211
<p className="s-settings-subtitle">{ __( 'Here you can tweak Global Settings that affect the styles across your entire site.', i18n ) }</p>
@@ -1238,7 +1229,7 @@ const GlobalSettings = props => {
12381229

12391230
const RoleManager = props => {
12401231
const groups = props.filteredSearchTree.find( tab => tab.id === 'role-manager' ).groups
1241-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
1232+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
12421233
const propsToPass = {
12431234
...props,
12441235
roleManager: groups.find( group => group.id === 'role-manager' ),
@@ -1248,11 +1239,11 @@ const RoleManager = props => {
12481239

12491240
return (
12501241
<div className="s-role-manager">
1251-
{ groupLength <= 0 ? (
1242+
{ ! hasGroupMatch ? (
12521243
<h3>{ __( 'No matching settings', i18n ) }</h3>
12531244
) : (
12541245
<>
1255-
{ propsToPass.roleManager.children.length > 0 &&
1246+
{ ( propsToPass.roleManager.children === null || propsToPass.roleManager.children.length > 0 ) &&
12561247
<div className="s-setting-group">
12571248
<h2>{ __( 'Role Manager', i18n ) }</h2>
12581249
<p className="s-settings-subtitle">
@@ -1290,7 +1281,7 @@ const RoleManager = props => {
12901281

12911282
const CustomFields = props => {
12921283
const groups = props.filteredSearchTree.find( tab => tab.id === 'custom-fields-settings' ).groups
1293-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
1284+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
12941285
const propsToPass = {
12951286
...props,
12961287
customFields: groups.find( group => group.id === 'custom-fields-settings' ),
@@ -1301,11 +1292,11 @@ const CustomFields = props => {
13011292

13021293
return (
13031294
<div className="s-custom-fields">
1304-
{ groupLength <= 0 ? (
1295+
{ ! hasGroupMatch ? (
13051296
<h3>{ __( 'No matching settings', i18n ) }</h3>
13061297
) : (
13071298
<>
1308-
{ propsToPass.customFields.children.length > 0 &&
1299+
{ ( propsToPass.customFields.children === null || propsToPass.customFields.children.length > 0 ) &&
13091300
<div className="s-setting-group">
13101301
<div className="s-custom-fields-settings-header">
13111302
<h2>{ __( 'Custom Fields', i18n ) }</h2>
@@ -1352,7 +1343,7 @@ const Integrations = props => {
13521343
} = props
13531344

13541345
const groups = filteredSearchTree.find( tab => tab.id === 'integrations' ).groups
1355-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
1346+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
13561347
const propsToPass = {
13571348
...props,
13581349
integrations: groups.find( group => group.id === 'integrations' ),
@@ -1362,11 +1353,11 @@ const Integrations = props => {
13621353

13631354
return (
13641355
<div className="s-integrations">
1365-
{ groupLength <= 0 ? (
1356+
{ ! hasGroupMatch ? (
13661357
<h3>{ __( 'No matching settings', i18n ) }</h3>
13671358
) : (
13681359
<>
1369-
{ propsToPass.integrations.children.length > 0 &&
1360+
{ ( propsToPass.integrations.children === null || propsToPass.integrations.children.length > 0 ) &&
13701361
<div className="s-setting-group">
13711362
<h2>{ __( 'Integrations', i18n ) }</h2>
13721363
<p className="s-settings-subtitle">{ __( 'Here are settings for the different integrations available in Stackable.', i18n ) }</p>
@@ -1459,19 +1450,21 @@ const AdditionalOptions = props => {
14591450
const groups = filteredSearchTree.find( tab => tab.id === 'other-settings' ).groups
14601451
const miscellaneous = groups.find( group => group.id === 'miscellaneous' )
14611452
const migrationSettings = groups.find( group => group.id === 'migration-settings' )
1462-
const groupLength = groups.reduce( ( acc, curr ) => acc + curr.children.length, 0 )
1453+
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
14631454

14641455
const searchClassname = ( label, searchedSettings ) => {
1465-
return searchedSettings.children.includes( label ) ? '' : 'ugb-admin-setting--not-highlight'
1456+
return searchedSettings.children === null || searchedSettings.children.includes( label )
1457+
? ''
1458+
: 'ugb-admin-setting--not-highlight'
14661459
}
14671460

14681461
return (
14691462
<div className="s-other-options-wrapper">
1470-
{ groupLength <= 0 ? (
1463+
{ ! hasGroupMatch ? (
14711464
<h3>{ __( 'No matching settings', i18n ) }</h3>
14721465
) : (
14731466
<>
1474-
{ miscellaneous.children.length > 0 &&
1467+
{ ( miscellaneous.children === null || miscellaneous.children.length > 0 ) &&
14751468
<div className="s-setting-group">
14761469
<h2>{ __( 'Miscellaneous', i18n ) }</h2>
14771470
<p className="s-settings-subtitle">{ __( 'Below are other minor settings. Some may be useful when upgrading from older versions of Stackable.', i18n ) }</p>
@@ -1528,7 +1521,7 @@ const AdditionalOptions = props => {
15281521
/>
15291522
</div>
15301523
}
1531-
{ migrationSettings.children.length > 0 &&
1524+
{ ( migrationSettings.children === null || migrationSettings.children.length > 0 ) &&
15321525
<div className="s-setting-group">
15331526
<h2>{ __( 'Migration Settings', i18n ) }</h2>
15341527
<p>{ __( 'After enabling the version 2 blocks, please refresh the page to re-fetch the blocks from the server.', i18n ) }</p>
@@ -1592,7 +1585,7 @@ const V2Settings = props => {
15921585

15931586
return (
15941587
<div className={ classes }>
1595-
{ optimizations.children.length > 0 &&
1588+
{ ( optimizations.children === null || optimizations.children.length > 0 ) &&
15961589
<div className="s-setting-group">
15971590
<h2>{ __( '🏃‍♂️ Optimization Settings', i18n ) } (V2)</h2>
15981591
<p className="s-settings-subtitle">
@@ -1604,7 +1597,7 @@ const V2Settings = props => {
16041597
<OptimizationSettings searchedSettings={ optimizations.children } />
16051598
</div>
16061599
}
1607-
{ blocks.children.length > 0 &&
1600+
{ ( blocks.children === null || blocks.children.length > 0 ) &&
16081601
<div className="s-setting-group">
16091602
<h2>{ __( 'Enable & Disable Blocks', i18n ) } (V2)</h2>
16101603
<strong>{ __( 'This only works for version 2 blocks.', i18n ) }</strong>

0 commit comments

Comments
 (0)