Skip to content

Commit 599173d

Browse files
pablinosmatticbot
authored andcommitted
Social | Avoid calling the settings endpoint on Social admin page if not relevant (#42102)
* Social | Avoid calling the settings endpoint on Social admin page if not relevant * Use receiveEntityRecords for jetpack/v4 settings selector * Fix unit tests Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13632678056 Upstream-Ref: Automattic/jetpack@cdb0e41
1 parent 59fac84 commit 599173d

File tree

8 files changed

+64
-49
lines changed

8 files changed

+64
-49
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is an alpha version! The changes listed here are not final.
2222

2323
### Fixed
2424
- Clean up Social admin page unit tests
25+
- Social | Avoid calling the settings endpoint on Social admin page if not relevant
2526

2627
## [0.79.0] - 2025-02-24
2728
### Added

src/components/admin-page/test/social-admin-page.test.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ describe( 'SocialAdminPage', () => {
8181

8282
describe( 'Social Notes toggle', () => {
8383
it( 'should show when plugin is active and module is enabled', () => {
84+
mockScriptData();
8485
render( <SocialAdminPage /> );
8586
expect( screen.getByText( 'Enable Social Notes' ) ).toBeInTheDocument();
87+
clearMockedScriptData();
8688
} );
8789

8890
it( 'should not show when plugin is not active', () => {
@@ -107,7 +109,7 @@ describe( 'SocialAdminPage', () => {
107109
},
108110
} );
109111
render( <SocialAdminPage /> );
110-
expect( screen.queryByTestId( 'social-notes-toggle' ) ).not.toBeInTheDocument();
112+
expect( screen.queryByText( 'Enable Social Notes' ) ).not.toBeInTheDocument();
111113
clearMockedScriptData();
112114
} );
113115
} );

src/components/admin-page/toggles/social-module-toggle/index.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ import {
55
getRedirectUrl,
66
useBreakpointMatch,
77
} from '@automattic/jetpack-components';
8-
import {
9-
getScriptData,
10-
isWpcomPlatformSite,
11-
currentUserCan,
12-
} from '@automattic/jetpack-script-data';
8+
import { getScriptData, isWpcomPlatformSite } from '@automattic/jetpack-script-data';
139
import { ExternalLink } from '@wordpress/components';
1410
import { useSelect, useDispatch } from '@wordpress/data';
1511
import { __, _x } from '@wordpress/i18n';
1612
import clsx from 'clsx';
1713
import React, { useCallback } from 'react';
1814
import { store as socialStore } from '../../../../social-store';
1915
import { getSocialScriptData, hasSocialPaidFeatures } from '../../../../utils';
16+
import { canToggleSocialModule } from '../../../../utils/misc';
2017
import ConnectionManagement from '../../../connection-management';
2118
import ToggleSection from '../toggle-section';
2219
import styles from './styles.module.scss';
@@ -81,7 +78,7 @@ const SocialModuleToggle: React.FC = () => {
8178
) : null;
8279
};
8380

84-
const hideToggle = is_wpcom || ! currentUserCan( 'manage_modules' );
81+
const hideToggle = ! canToggleSocialModule();
8582
return (
8683
<ToggleSection
8784
hideToggle={ hideToggle }

src/social-store/hydrate-stores.ts

+31-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@ import { getSocialScriptData } from '../utils';
77
* Hydrate the data stores
88
*/
99
export async function hydrateStores() {
10+
const {
11+
addEntities,
12+
receiveEntityRecords,
13+
// @ts-expect-error finishResolution exists but it's not typed
14+
finishResolution,
15+
} = dispatch( coreStore );
16+
17+
const socialToggleBase = getSocialScriptData()?.api_paths?.socialToggleBase;
18+
19+
const jetpackEntities = select( coreStore ).getEntitiesConfig( 'jetpack/v4' );
20+
if ( ! jetpackEntities.some( ( { name } ) => name === socialToggleBase ) ) {
21+
await addEntities( [
22+
{
23+
kind: 'jetpack/v4',
24+
name: socialToggleBase,
25+
baseURL: `/jetpack/v4/${ socialToggleBase }`,
26+
label: __( 'Social Settings', 'jetpack-publicize-components' ),
27+
},
28+
] );
29+
30+
// @ts-expect-error Only 3 arguments are required, rest are optional but types expect 7
31+
await receiveEntityRecords(
32+
'jetpack/v4',
33+
socialToggleBase,
34+
{ publicize: getSocialScriptData()?.is_publicize_enabled },
35+
true
36+
);
37+
38+
await finishResolution( 'getEntityRecord', [ 'jetpack/v4', socialToggleBase ] );
39+
}
40+
1041
const wpcomEntities = select( coreStore ).getEntitiesConfig( 'wpcom/v2' );
1142

1243
if ( ! wpcomEntities.some( ( { name } ) => name === 'publicize/services' ) ) {
13-
const {
14-
addEntities,
15-
receiveEntityRecords,
16-
// @ts-expect-error finishResolution exists but it's not typed
17-
finishResolution,
18-
} = dispatch( coreStore );
19-
2044
await addEntities( [
2145
{
2246
kind: 'wpcom/v2',

src/social-store/resolvers.ts

-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import apiFetch from '@wordpress/api-fetch';
2-
import { store as coreStore } from '@wordpress/core-data';
32
import { store as editorStore } from '@wordpress/editor';
4-
import { __ } from '@wordpress/i18n';
53
import { getSocialScriptData } from '../utils/script-data';
64
import { normalizeShareStatus } from '../utils/share-status';
75
import { setConnections } from './actions/connection-data';
@@ -77,32 +75,7 @@ export function getPostShareStatus( _postId ) {
7775
};
7876
}
7977

80-
/**
81-
* Resolves the social module settings to ensure the core-data entities are registered.
82-
*
83-
* @return {Function} Resolver
84-
*/
85-
export function getSocialModuleSettings() {
86-
return async ( { registry } ) => {
87-
const { socialToggleBase } = getSocialScriptData().api_paths;
88-
89-
const jetpackEntities = registry.select( coreStore ).getEntitiesConfig( 'jetpack/v4' );
90-
91-
if ( ! jetpackEntities.some( ( { name } ) => name === socialToggleBase ) ) {
92-
await registry.dispatch( coreStore ).addEntities( [
93-
{
94-
kind: 'jetpack/v4',
95-
name: socialToggleBase,
96-
baseURL: `/jetpack/v4/${ socialToggleBase }`,
97-
label: __( 'Social Settings', 'jetpack-publicize-components' ),
98-
},
99-
] );
100-
}
101-
};
102-
}
103-
10478
export default {
10579
getConnections,
10680
getPostShareStatus,
107-
getSocialModuleSettings,
10881
};

src/social-store/selectors/social-module-settings.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import { SocialModuleSettings } from '../types';
66
/**
77
* Returns the Social module settings.
88
*/
9-
export const getSocialModuleSettings = createRegistrySelector( select => () => {
10-
const { socialToggleBase } = getSocialScriptData().api_paths;
9+
export const getSocialModuleSettings = createRegistrySelector(
10+
select => (): SocialModuleSettings => {
11+
const { api_paths, is_publicize_enabled } = getSocialScriptData();
1112

12-
const data = select( coreStore ).getEntityRecord( 'jetpack/v4', socialToggleBase, undefined );
13+
const data = select( coreStore ).getEntityRecord< SocialModuleSettings >(
14+
'jetpack/v4',
15+
api_paths.socialToggleBase
16+
);
1317

14-
return ( data ?? {
15-
publicize: getSocialScriptData().is_publicize_enabled,
16-
} ) as SocialModuleSettings;
17-
} );
18+
return data ?? { publicize: is_publicize_enabled };
19+
}
20+
);
1821

1922
/**
2023
* Returns whether the Social module settings are being saved

src/social-store/selectors/social-settings.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { currentUserCan } from '@automattic/jetpack-script-data';
12
import { store as coreStore } from '@wordpress/core-data';
23
import { createRegistrySelector } from '@wordpress/data';
34
import { SocialSettings } from '../../types';
@@ -15,7 +16,9 @@ export const isSavingSiteSettings = createRegistrySelector( select => () => {
1516
* Returns the social settings.
1617
*/
1718
export const getSocialSettings = createRegistrySelector( select => () => {
18-
const data = select( coreStore ).getEntityRecord< SocialSettingsFields >( 'root', 'site' );
19+
const data = currentUserCan( 'manage_options' )
20+
? select( coreStore ).getEntityRecord< SocialSettingsFields >( 'root', 'site' )
21+
: null;
1922

2023
const { settings } = getSocialScriptData();
2124

src/utils/misc.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { currentUserCan, getScriptData } from '@automattic/jetpack-script-data';
2+
3+
/**
4+
* Check if the social module can be toggled.
5+
*
6+
* @return Whether the social module can be toggled.
7+
*/
8+
export function canToggleSocialModule() {
9+
const is_wpcom = getScriptData().site.host === 'wpcom';
10+
11+
return ! is_wpcom && currentUserCan( 'manage_modules' );
12+
}

0 commit comments

Comments
 (0)