-
Notifications
You must be signed in to change notification settings - Fork 293
/
modules.js
880 lines (785 loc) · 27.6 KB
/
modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/**
* `core/modules` data store: module info.
*
* Site Kit by Google, Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import memize from 'memize';
import defaults from 'lodash/defaults';
import merge from 'lodash/merge';
import isPlainObject from 'lodash/isPlainObject';
import invariant from 'invariant';
import { sprintf, __ } from '@wordpress/i18n';
/**
* WordPress dependencies
*/
import { WPComponent } from '@wordpress/element';
/**
* Internal dependencies
*/
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { STORE_NAME, ERROR_CODE_INSUFFICIENT_MODULE_DEPENDENCIES } from './constants';
import { CORE_SITE } from '../../datastore/site/constants';
import { CORE_USER } from '../../datastore/user/constants';
import { createFetchStore } from '../../data/create-fetch-store';
import { listFormat } from '../../../util';
import DefaultSettingsSetupIncomplete from '../../../components/settings/DefaultSettingsSetupIncomplete';
import { createValidatedAction } from '../../data/utils';
const { createRegistrySelector, createRegistryControl } = Data;
// Actions.
const REFETCH_AUTHENTICATION = 'REFETCH_AUTHENTICATION';
const SELECT_MODULE_REAUTH_URL = 'SELECT_MODULE_REAUTH_URL';
const REGISTER_MODULE = 'REGISTER_MODULE';
const RECEIVE_CHECK_REQUIREMENTS_ERROR = 'RECEIVE_CHECK_REQUIREMENTS_ERROR';
const RECEIVE_CHECK_REQUIREMENTS_SUCCESS = 'RECEIVE_CHECK_REQUIREMENTS_SUCCESS';
const moduleDefaults = {
slug: '',
storeName: null,
name: '',
description: '',
homepage: null,
internal: false,
active: false,
connected: false,
dependencies: [],
dependants: [],
order: 10,
features: [],
Icon: null,
SettingsEditComponent: null,
SettingsViewComponent: null,
SettingsSetupIncompleteComponent: DefaultSettingsSetupIncomplete,
SetupComponent: null,
checkRequirements: () => true,
};
const normalizeModules = memize(
( serverDefinitions, clientDefinitions ) => {
// Module properties in `clientDefinitions` will overwrite `serverDefinitions`
// but only for keys whose values are not `undefined`.
const modules = merge( {}, serverDefinitions, clientDefinitions );
return Object.keys( modules )
.map( ( slug ) => {
const module = { ...modules[ slug ], slug };
// Fill any `undefined` values with defaults.
defaults( module, { name: slug }, moduleDefaults );
return module;
} )
.sort( ( a, b ) => a.order - b.order )
.reduce( ( acc, module ) => {
return { ...acc, [ module.slug ]: module };
}, {} );
},
);
const fetchGetModulesStore = createFetchStore( {
baseName: 'getModules',
controlCallback: () => {
return API.get( 'core', 'modules', 'list', null, {
useCache: false,
} );
},
reducerCallback: ( state, modules ) => {
return {
...state,
isAwaitingModulesRefresh: false,
serverDefinitions: modules.reduce( ( acc, module ) => {
return { ...acc, [ module.slug ]: module };
}, {} ),
};
},
} );
const fetchSetModuleActivationStore = createFetchStore( {
baseName: 'setModuleActivation',
controlCallback: ( { slug, active } ) => {
return API.set( 'core', 'modules', 'activation', {
slug,
active,
} );
},
reducerCallback: ( state ) => {
// Updated module activation state is handled by re-fetching module
// data instead, so this reducer just sets the below flag.
return {
...state,
isAwaitingModulesRefresh: true,
};
},
argsToParams: ( slug, active ) => {
return {
slug,
active,
};
},
validateParams: ( { slug, active } = {} ) => {
invariant( slug, 'slug is required.' );
invariant( active !== undefined, 'active is required.' );
},
} );
const baseInitialState = {
clientDefinitions: {},
serverDefinitions: undefined,
// This value is to indicate that modules data needs to be refreshed after
// a module activation update, since the activation is technically complete
// before this data has been refreshed.
isAwaitingModulesRefresh: false,
checkRequirementsResults: {},
};
const baseActions = {
/**
* Activates a module on the server.
*
* Activate a module (based on the slug provided).
*
* @since 1.8.0
*
* @param {string} slug Slug of the module to activate.
* @return {Object} Object with `{response, error}`. On success, `response.moduleReauthURL`
* is set to redirect the user to the corresponding module setup or OAuth
* consent screen.
*/
*activateModule( slug ) {
const { response, error } = yield baseActions.setModuleActivation( slug, true );
if ( response?.success === true ) {
const moduleReauthURL = yield {
payload: { slug },
type: SELECT_MODULE_REAUTH_URL,
};
return {
response: { ...response, moduleReauthURL },
error,
};
}
return { response, error };
},
/**
* Deactivates a module on the server.
*
* Deactivate a module (based on the slug provided).
*
* @since 1.8.0
*
* @param {string} slug Slug of the module to activate.
* @return {Object} Object with `{response, error}`.
*/
*deactivateModule( slug ) {
const { response, error } = yield baseActions.setModuleActivation( slug, false );
return { response, error };
},
/**
* (De)activates a module on the server.
*
* POSTs to the `core/modules/activation` endpoint to set the `active` status
* supplied for the give `slug`.
*
* @since 1.8.0
* @private
*
* @param {string} slug Slug of the module to activate/deactivate.
* @param {boolean} active `true` to activate; `false` to deactivate.
* @return {Object} Object with `{response, error}`.
*/
setModuleActivation: createValidatedAction(
( slug, active ) => {
invariant( slug, 'slug is required.' );
invariant( active !== undefined, 'active is required.' );
},
function* ( slug, active ) {
const { response, error } = yield fetchSetModuleActivationStore.actions.fetchSetModuleActivation( slug, active );
if ( response?.success === true ) {
// Fetch (or re-fetch) all modules, with their updated status.
yield fetchGetModulesStore.actions.fetchGetModules();
yield {
payload: {},
type: REFETCH_AUTHENTICATION,
};
}
return { response, error };
},
),
/**
* Registers a module.
*
* @since 1.13.0
* @since 1.20.0 Introduced the ability to register settings and setup components.
* @since 1.22.0 Introduced the ability to add a checkRequirements function.
* @since 1.23.0 Introduced the ability to register an Icon component.
* @since 1.24.0 Introduced the ability to explictly define a module store name.
*
* @param {string} slug Module slug.
* @param {Object} [settings] Optional. Module settings.
* @param {string} [settings.storeName] Optional. Module storeName. If none is provided we assume no store exists for this module.
* @param {string} [settings.name] Optional. Module name. Default is the slug.
* @param {string} [settings.description] Optional. Module description. Default empty string.
* @param {Array.<string>} [settings.features] Optional. Module features. Default empty array.
* @param {WPComponent} [settings.Icon] Optional. React component to render module icon. Default none.
* @param {number} [settings.order] Optional. Numeric indicator for module order. Default 10.
* @param {string} [settings.homepage] Optional. Module homepage URL. Default empty string.
* @param {WPComponent} [settings.SettingsEditComponent] Optional. React component to render the settings edit panel. Default none.
* @param {WPComponent} [settings.SettingsViewComponent] Optional. React component to render the settings view panel. Default none.
* @param {WPComponent} [settings.SettingsSetupIncompleteComponent] Optional. React component to render the incomplete settings panel. Default none.
* @param {WPComponent} [settings.SetupComponent] Optional. React component to render the setup panel. Default none.
* @param {Function} [settings.checkRequirements] Optional. Function to check requirements for the module. Throws a WP error object for error or returns on success.
* @param {Function} [settings.screenWidgetContext] Optional. Get the registered context name for a given module.
*/
registerModule: createValidatedAction(
( slug ) => {
invariant( slug, 'module slug is required' );
},
function* ( slug, {
storeName,
name,
description,
features,
Icon,
order,
homepage,
SettingsEditComponent,
SettingsViewComponent,
SetupComponent,
SettingsSetupIncompleteComponent,
checkRequirements,
screenWidgetContext,
} = {} ) {
const settings = {
storeName,
name,
description,
features,
Icon,
order,
homepage,
SettingsEditComponent,
SettingsViewComponent,
SetupComponent,
SettingsSetupIncompleteComponent,
checkRequirements,
screenWidgetContext,
};
yield {
payload: {
settings,
slug,
},
type: REGISTER_MODULE,
};
const registry = yield Data.commonActions.getRegistry();
// As we can specify a custom checkRequirements function here, we're invalidating the resolvers for activation checks.
yield registry.dispatch( STORE_NAME ).invalidateResolution( 'canActivateModule', [ slug ] );
yield registry.dispatch( STORE_NAME ).invalidateResolution( 'getCheckRequirementsError', [ slug ] );
},
),
/**
* Receives the check requirements error map for specified modules modules.
*
* @since 1.22.0
* @private
*
* @param {string} slug Module slug.
* @param {Object} error WordPress Error object containing code, message and data properties.
* @return {Object} Action for RECEIVE_CHECK_REQUIREMENTS_ERROR.
*/
receiveCheckRequirementsError( slug, error ) {
invariant( slug, 'slug is required' );
invariant( isPlainObject( error ), 'error is required and must be an object' );
return {
payload: { slug, error },
type: RECEIVE_CHECK_REQUIREMENTS_ERROR,
};
},
/**
* Receives the check requirements success for a module.
*
* @since 1.22.0
* @private
*
* @param {string} slug Success for a module slug.
* @return {Object} Action for RECEIVE_CHECK_REQUIREMENTS_SUCCESS.
*/
receiveCheckRequirementsSuccess( slug ) {
invariant( slug, 'slug is required' );
return {
payload: {
slug,
},
type: RECEIVE_CHECK_REQUIREMENTS_SUCCESS,
};
},
};
export const baseControls = {
[ REFETCH_AUTHENTICATION ]: createRegistryControl( ( { dispatch } ) => () => {
return dispatch( CORE_USER ).fetchGetAuthentication();
} ),
[ SELECT_MODULE_REAUTH_URL ]: createRegistryControl( ( { select } ) => ( { payload } ) => {
const { slug } = payload;
const storeName = select( STORE_NAME ).getModuleStoreName( slug );
// If a storeName wasn't specified on registerModule we assume there is no store for this module
if ( ! storeName ) {
return;
}
const getAdminReauthURL = select( storeName )?.getAdminReauthURL;
if ( getAdminReauthURL ) {
return getAdminReauthURL();
}
return select( CORE_SITE ).getAdminURL( 'googlesitekit-dashboard' );
} ),
};
const baseReducer = ( state, { type, payload } ) => {
switch ( type ) {
case REGISTER_MODULE: {
const { slug, settings } = payload;
if ( !! state.clientDefinitions[ slug ] ) {
global.console.warn( `Could not register module with slug "${ slug }". Module "${ slug }" is already registered.` );
return state;
}
return {
...state,
clientDefinitions: {
...state.clientDefinitions,
[ slug ]: settings,
},
};
}
case RECEIVE_CHECK_REQUIREMENTS_ERROR: {
const { slug, error } = payload;
return {
...state,
checkRequirementsResults: {
...state.checkRequirementsResults,
[ slug ]: error,
},
};
}
case RECEIVE_CHECK_REQUIREMENTS_SUCCESS: {
const { slug } = payload;
return {
...state,
checkRequirementsResults: {
...state.checkRequirementsResults,
[ slug ]: true,
},
};
}
default: {
return state;
}
}
};
const baseResolvers = {
*getModules() {
const registry = yield Data.commonActions.getRegistry();
const existingModules = registry.select( STORE_NAME ).getModules();
if ( ! existingModules ) {
yield fetchGetModulesStore.actions.fetchGetModules();
}
},
*canActivateModule( slug ) {
const registry = yield Data.commonActions.getRegistry();
yield Data.commonActions.await( registry.__experimentalResolveSelect( STORE_NAME ).getModules() );
const module = registry.select( STORE_NAME ).getModule( slug );
if ( ! module ) {
return;
}
const inactiveModules = [];
module.dependencies.forEach( ( dependencySlug ) => {
const dependedentModule = registry.select( STORE_NAME ).getModule( dependencySlug );
if ( ! dependedentModule?.active ) {
inactiveModules.push( dependedentModule.name );
}
} );
// If we have inactive dependencies, there's no need to check if we can
// activate the module until the dependencies have been activated.
if ( inactiveModules.length ) {
/* translators: Error message text. 1: A flattened list of module names. 2: A module name. */
const messageTemplate = __( 'You need to set up %1$s to gain access to %2$s.', 'google-site-kit' );
const errorMessage = sprintf( messageTemplate, listFormat( inactiveModules ), module.name );
yield baseActions.receiveCheckRequirementsError( slug, {
code: ERROR_CODE_INSUFFICIENT_MODULE_DEPENDENCIES,
message: errorMessage,
data: { inactiveModules },
} );
} else {
try {
yield Data.commonActions.await( module.checkRequirements( registry ) );
yield baseActions.receiveCheckRequirementsSuccess( slug );
} catch ( error ) {
yield baseActions.receiveCheckRequirementsError( slug, error );
}
}
},
};
const baseSelectors = {
/**
* Gets the list of modules registered for use with Site Kit.
*
* A module is a section of Site Kit that relates to a particular service,
* like Google Analytics or Google PageSpeed modules. They can provide
* admin-only features (like PageSpeed Insights), frontend-only features,
* or both (eg. Analytics, which can install Analytics <script> tags in the
* frontend, and show dashboards in the WordPress Admin).
*
* Returns an Object/map of objects, keyed by slug, with the following shape when successful:
* ```
* slug: {
* "slug": "tagmanager",
* "name": "Tag Manager",
* "description": "Tag Manager creates an easy to manage way to create tags on your site without updating code.",
* "homepage": "https://tagmanager.google.com/",
* "internal": false,
* "active": false,
* "connected": false,
* "dependencies": [
* "analytics"
* ],
* "dependents": []
* }
* ```
*
* @since 1.8.0
*
* @param {Object} state Data store's state.
* @return {(Object|undefined)} Modules available on the site.
*/
getModules( state ) {
const { clientDefinitions, serverDefinitions } = state;
// Return `undefined` if modules haven't been loaded yet.
if ( serverDefinitions === undefined ) {
return undefined;
}
// `normalizeModules` must be called with stable arguments directly from state.
// Redefining/spreading these will undermine the memoization!
return normalizeModules( serverDefinitions, clientDefinitions );
},
/**
* Gets a specific module by slug.
*
* Returns a specific module by its slug.
* Returns `undefined` if state is still loading or if said module doesn't exist.
*
* @since 1.8.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(Object|undefined)} A specific module object; `undefined` if state is still loading or if said module doesn't exist.
*/
getModule: createRegistrySelector( ( select ) => ( state, slug ) => {
const modules = select( STORE_NAME ).getModules();
// Return `undefined` if modules haven't been loaded yet.
if ( modules === undefined ) {
return undefined;
}
// A module with this slug couldn't be found; return `null` to signify the
// "not found" state.
if ( modules[ slug ] === undefined ) {
return null;
}
// This module exists, so let's return it.
return modules[ slug ];
} ),
/**
* Gets a specific module icon by slug.
*
* Returns a specific module icon by its slug.
* Returns `null` if state is still loading or if said module doesn't exist or doesn't have an icon.
*
* @since 1.23.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(WPComponent|undefined|null)} A specific module's icon; `undefined` if state is still loading; `null` if said module doesn't exist or doesn't have an icon.
*/
getModuleIcon: createRegistrySelector( ( select ) => ( state, slug ) => {
const module = select( STORE_NAME ).getModule( slug );
// Return `undefined` if module with this slug isn't loaded yet.
if ( module === undefined ) {
return undefined;
}
// A module with this slug couldn't be found or the icon is not found for the module; return `null` to signify the
// "module not found" or "icon not found" state
if ( module === null || module.Icon === null ) {
return null;
}
// This module and the icon exists, so let's return it.
return module.Icon;
} ),
/**
* Gets module dependency names by slug.
*
* Returns a list of modules that depend on this module.
* Returns `undefined` if state is still loading or if said module doesn't exist.
*
* @since 1.20.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(Array|undefined)} An array of dependency module names; `undefined` if state is still loading.
*/
getModuleDependencyNames: createRegistrySelector( ( select ) => ( state, slug ) => {
const module = select( STORE_NAME ).getModule( slug );
// Return `undefined` if module with this slug isn't loaded yet.
if ( module === undefined ) {
return undefined;
}
// A module with this slug couldn't be found; return `[]` to signify the
// "not found" state.
if ( module === null ) {
return [];
}
// Module is found, return the names of the dependencies
// Modules are already resolved after we getModule() so they can't be undefined.
const modules = select( STORE_NAME ).getModules();
return module.dependencies.map( ( dependencySlug ) => modules[ dependencySlug ]?.name || dependencySlug );
} ),
/**
* Gets module dependant names by slug.
*
* Returns a list of modules on which this module depends.
* Returns `undefined` if state is still loading or if said module doesn't exist.
*
* @since 1.20.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(Array|undefined)} An array of dependant module names; `undefined` if state is still loading.
*/
getModuleDependantNames: createRegistrySelector( ( select ) => ( state, slug ) => {
const module = select( STORE_NAME ).getModule( slug );
// Return `undefined` if module with this slug isn't loaded yet.
if ( module === undefined ) {
return undefined;
}
// A module with this slug couldn't be found; return `[]` to signify the
// "not found" state.
if ( module === null ) {
return [];
}
// Module is found, return the names of the dependants
// Modules are already resolved after we getModule() so they can't be undefined.
const modules = select( STORE_NAME ).getModules();
return module.dependants.map( ( dependantSlug ) => modules[ dependantSlug ]?.name || dependantSlug );
} ),
/**
* Gets module store name by slug.
*
* Returns the store name if preset or null if there is no store name for this module.
* Returns `undefined` if state is still loading or if said module doesn't exist.
*
* @since 1.24.0
*
* @param {string} slug Module slug.
* @return {(string|null|undefined)} `string` of the store name if a name has been set for this module.
* `null` if no store name was set.
* `undefined` if state is still loading.
*/
getModuleStoreName: createRegistrySelector( ( select ) => ( state, slug ) => {
const module = select( STORE_NAME ).getModule( slug );
// Return `undefined` if module with this slug isn't loaded yet.
if ( module === undefined ) {
return undefined;
}
// Return null if no store name was set
if ( module === null ) {
return null;
}
return module.storeName;
} ),
/**
* Checks a module's activation status.
*
* Returns `true` if the module exists and is active.
* Returns `false` if the module exists but is not active.
* Returns `undefined` if state is still loading or if no module with that slug exists.
*
* @since 1.8.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(boolean|null|undefined)} `true` when the module exists and is active.
* `undefined` if state is still loading.
* `null` if said module doesn't exist.
*/
isModuleActive: createRegistrySelector( ( select ) => ( state, slug ) => {
const module = select( STORE_NAME ).getModule( slug );
// Return `undefined` if modules haven't been loaded yet.
if ( module === undefined ) {
return undefined;
}
// A module with this slug couldn't be found; return `null` to signify the
// "not found" state.
if ( module === null ) {
return null;
}
return module.active;
} ),
/**
* Checks whether a module is connected or not.
*
* Returns `true` if the module exists, is active and connected.
* Returns `false` if the module exists but is either not active or not connected.
* Returns `undefined` if state is still loading or if no module with that slug exists.
*
* @since 1.16.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(boolean|null|undefined)} `true` when the module exists, is active and connected, otherwise `false`.
* `undefined` if state is still loading.
* `null` if said module doesn't exist.
*/
isModuleConnected: createRegistrySelector( ( select ) => ( state, slug ) => {
const module = select( STORE_NAME ).getModule( slug );
// Return `undefined` if modules haven't been loaded yet.
if ( module === undefined ) {
return undefined;
}
// A module with this slug couldn't be found; return `null` to signify the
// "not found" state.
if ( module === null ) {
return null;
}
return module.active && module.connected;
} ),
/**
* Checks if a module's status is changing.
*
* Returns `true` if the module exists and is changing its `active` flag.
* Returns `false` if the module exists but is not changing its `active` flag.
* Returns `undefined` if state is still loading or if no module with that slug exists.
*
* @since 1.8.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(boolean|undefined)} Activation change status; `undefined` if state is still loading or if no module with that slug exists.
*/
isDoingSetModuleActivation: createRegistrySelector( ( select ) => ( state, slug ) => {
// Return undefined if modules not loaded or invalid slug.
if ( ! select( STORE_NAME ).getModule( slug ) ) {
return undefined;
}
// Check if the module is being activated.
if ( select( STORE_NAME ).isFetchingSetModuleActivation( slug, true ) ) {
return true;
}
// Check if the module is being deactivated.
if ( select( STORE_NAME ).isFetchingSetModuleActivation( slug, false ) ) {
return true;
}
// Check if modules data still needs to be refreshed after activation
// update.
return state.isAwaitingModulesRefresh;
} ),
/**
* Checks if we can activate a module with a given slug.
*
* Returns `true` if the module can be activated.
* Returns `false` if the module can not be activated.
* Returns `undefined` if slug can not be found in state.
*
* @since 1.22.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(boolean|undefined)} Can activate module status; `undefined` if state is still loading or if no module with that slug exists.
*/
canActivateModule( state, slug ) {
invariant( slug, 'slug is required' );
const moduleRequirements = state.checkRequirementsResults[ slug ];
if ( moduleRequirements === undefined ) {
return undefined;
}
return moduleRequirements === true;
},
/**
* Gets the module activation error for a given slug.
*
* Returns `null` if the module can be activated and there is no error.
* Returns `object` containing code, message and optional data property if there is an activation error for a slug.
*
* @since 1.22.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(null|Object)} Activation error for a module slug; `null` if there is no error or an error object if we cannot activate a given module.
*/
getCheckRequirementsError: createRegistrySelector( ( select ) => ( state, slug ) => {
invariant( slug, 'slug is required.' );
// Need to use registry selector here to ensure resolver is invoked.
if ( select( STORE_NAME ).canActivateModule( slug ) ) {
return null;
}
return state.checkRequirementsResults[ slug ];
} ),
/**
* Gets the module's screenWidgetContext.
*
* Returns the registered context string, screenWidgetContext for the given module.
*
* @since 1.28.0
*
* @param {Object} state Data store's state.
* @param {string} moduleSlug Module slug.
* @return {(string|null|undefined)} The module's registered context string, null if there is
* none, undefined if not loaded yet.
*/
getScreenWidgetContext: createRegistrySelector( ( select ) => ( state, moduleSlug ) => {
invariant( moduleSlug, 'slug is required.' );
const modules = select( STORE_NAME ).getModules();
if ( modules === undefined ) {
return undefined;
}
const screenWidgetContext = modules[ moduleSlug ]?.screenWidgetContext;
return screenWidgetContext || null;
} ),
/**
* Gets the module's list of features.
*
* Returns a list of features of this module.
*
* @since 1.30.0
*
* @param {Object} state Data store's state.
* @param {string} slug Module slug.
* @return {(Array|undefined)} An array of features for the module; `undefined` if state is still loading.
*/
getModuleFeatures: createRegistrySelector( ( select ) => ( state, slug ) => {
const modules = select( STORE_NAME ).getModules();
// Return `undefined` if modules haven't been loaded yet.
if ( modules === undefined ) {
return undefined;
}
return Array.isArray( modules[ slug ]?.features ) ? modules[ slug ].features : [];
} ),
};
const store = Data.combineStores(
fetchGetModulesStore,
fetchSetModuleActivationStore,
{
initialState: baseInitialState,
actions: baseActions,
controls: baseControls,
reducer: baseReducer,
resolvers: baseResolvers,
selectors: baseSelectors,
},
);
export const initialState = store.initialState;
export const actions = store.actions;
export const controls = store.controls;
export const reducer = store.reducer;
export const resolvers = store.resolvers;
export const selectors = store.selectors;
export default store;