@automattic/jetpack-shared-extension-utils 0.13.7 → 0.13.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.13.9] - 2024-01-18
9
+ ### Changed
10
+ - Update dependencies.
11
+
12
+ ## [0.13.8] - 2024-01-08
13
+ ### Added
14
+ - Remove unused logic from the modules store and cover store with tests. [#34835]
15
+
16
+ ### Changed
17
+ - Updated useModuleStatus hook to use module_status redux store. [#34845]
18
+
8
19
  ## [0.13.7] - 2024-01-04
9
20
  ### Changed
10
21
  - Updated package dependencies. [#34815] [#34816]
@@ -313,6 +324,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
313
324
  ### Changed
314
325
  - Core: prepare utility for release
315
326
 
327
+ [0.13.9]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.13.8...0.13.9
328
+ [0.13.8]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.13.7...0.13.8
316
329
  [0.13.7]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.13.6...0.13.7
317
330
  [0.13.6]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.13.5...0.13.6
318
331
  [0.13.5]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.13.4...0.13.5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/jetpack-shared-extension-utils",
3
- "version": "0.13.7",
3
+ "version": "0.13.9",
4
4
  "description": "Utility functions used by the block editor extensions",
5
5
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
6
6
  "bugs": {
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@automattic/jetpack-analytics": "^0.1.29",
21
- "@automattic/jetpack-components": "^0.45.10",
22
- "@automattic/jetpack-connection": "^0.30.12",
21
+ "@automattic/jetpack-components": "^0.46.0",
22
+ "@automattic/jetpack-connection": "^0.31.0",
23
23
  "@wordpress/api-fetch": "6.45.0",
24
24
  "@wordpress/compose": "6.25.0",
25
25
  "@wordpress/data": "9.18.0",
@@ -1,66 +1,6 @@
1
- import apiFetch from '@wordpress/api-fetch';
2
- import { useEffect, useState, useMemo, useCallback } from '@wordpress/element';
3
- import { isSimpleSite } from '../../site-type-utils';
4
-
5
- /**
6
- * Fetch information about all Jetpack modules.
7
- *
8
- * @returns {Promise<object>} Details about all available modules on the site.
9
- */
10
- async function fetchModules() {
11
- try {
12
- const result = await apiFetch( {
13
- path: `/jetpack/v4/module/all`,
14
- method: 'GET',
15
- } );
16
- return result;
17
- } catch ( error ) {
18
- return error.message;
19
- }
20
- }
21
-
22
- /**
23
- * Update a Jetpack module's status.
24
- *
25
- * @param {*} name - The module's name.
26
- * @param {*} toggle - New module status.
27
- * @returns {Promise<boolean>} Promise that resolves to the new module status.
28
- */
29
- async function changeModuleStatus( name, toggle ) {
30
- const result = await apiFetch( {
31
- path: `/jetpack/v4/module/${ name }/active`,
32
- method: 'POST',
33
- data: {
34
- active: toggle,
35
- },
36
- } );
37
- return result;
38
- }
39
-
40
- /**
41
- * Determine whethher a Jetpack module is active.
42
- *
43
- * @param {string} name - The module's name
44
- * @returns {Promise<boolean>} Whether the module is active.
45
- */
46
- async function isJetpackModuleActive( name ) {
47
- // On WordPress.com Simple sites, all modules are always active.
48
- if ( isSimpleSite() ) {
49
- return true;
50
- }
51
-
52
- // Fetch module info.
53
- try {
54
- // Check if module is active.
55
- const modulesInfo = await fetchModules();
56
- if ( ! modulesInfo || ! modulesInfo.hasOwnProperty( name ) ) {
57
- return false;
58
- }
59
- return !! modulesInfo[ name ].activated;
60
- } catch ( e ) {
61
- return false;
62
- }
63
- }
1
+ import { useDispatch, useSelect } from '@wordpress/data';
2
+ import { useMemo, useCallback } from '@wordpress/element';
3
+ import { JETPACK_MODULES_STORE_ID } from '../../modules-state';
64
4
 
65
5
  /**
66
6
  * Manage a Jetpack module's status (get and set).
@@ -69,40 +9,27 @@ async function isJetpackModuleActive( name ) {
69
9
  * @returns {boolean} Whether the module is active.
70
10
  */
71
11
  const useModuleStatus = name => {
72
- const [ isLoadingModules, setIsLoadingModules ] = useState( Boolean( name ) );
73
- const [ isChangingStatus, setIsChangingStatus ] = useState( false );
74
- const [ isModuleActive, setModuleStatus ] = useState( false );
75
-
76
- // Get module status.
77
- useEffect( () => {
78
- if ( ! name ) {
79
- return;
80
- }
81
-
82
- setIsLoadingModules( true );
12
+ const { isModuleActive, isChangingStatus, isLoadingModules } = useSelect(
13
+ selectData => {
14
+ const data = selectData( JETPACK_MODULES_STORE_ID );
15
+ return {
16
+ isModuleActive: data.isModuleActive( name ),
17
+ isChangingStatus: data.isModuleUpdating( name ),
18
+ isLoadingModules: data.areModulesLoading( name ),
19
+ };
20
+ },
21
+ [ JETPACK_MODULES_STORE_ID ]
22
+ );
83
23
 
84
- isJetpackModuleActive( name ).then( moduleStatus => {
85
- setModuleStatus( moduleStatus );
86
- setIsLoadingModules( false );
87
- } );
88
- }, [ name ] );
24
+ const { updateJetpackModuleStatus } = useDispatch( JETPACK_MODULES_STORE_ID );
89
25
 
90
26
  const changeStatus = useCallback(
91
- newModuleStatus => {
92
- if ( ! name || isModuleActive === newModuleStatus ) {
93
- return;
94
- }
95
- setIsChangingStatus( true );
96
- changeModuleStatus( name, newModuleStatus )
97
- .then( () => {
98
- setModuleStatus( newModuleStatus );
99
- setIsChangingStatus( false );
100
- } )
101
- .catch( () => {
102
- setIsChangingStatus( false );
103
- } );
104
- },
105
- [ name, isModuleActive ]
27
+ value =>
28
+ updateJetpackModuleStatus( {
29
+ name,
30
+ active: value,
31
+ } ),
32
+ [ name, updateJetpackModuleStatus ]
106
33
  );
107
34
 
108
35
  return useMemo(
@@ -20,11 +20,7 @@ export const SET_MODULE_UPDATING = 'SET_MODULE_UPDATING';
20
20
  */
21
21
  export function* updateJetpackModuleStatus( settings ) {
22
22
  try {
23
- const originalData = select( JETPACK_MODULES_STORE_ID ).getJetpackModules();
24
23
  yield setIsUpdating( settings.name, true );
25
- if ( originalData.data?.[ settings.name ]?.activated !== settings.active ) {
26
- yield setJetpackModules( originalData );
27
- }
28
24
  yield updateJetpackModuleStatusControl( settings );
29
25
  const data = yield fetchJetpackModules();
30
26
  yield setJetpackModules( { data } );