Skip to content

Commit 31a2d18

Browse files
edanzerenejb
andauthored
Forms: remove unused hasAI from config (#45522)
--------- Co-authored-by: Enej Bajgoric <[email protected]>
1 parent 388a048 commit 31a2d18

File tree

9 files changed

+65
-61
lines changed

9 files changed

+65
-61
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Forms: remove unusaed hasAI from config.

projects/packages/forms/src/contact-form/class-contact-form-endpoint.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Automattic\Jetpack\Redirect;
1717
use Automattic\Jetpack\Status;
1818
use Automattic\Jetpack\Status\Host;
19-
use Jetpack_AI_Helper;
2019
use WP_Error;
2120
use WP_REST_Request;
2221
use WP_REST_Response;
@@ -1269,12 +1268,6 @@ public function update_read_status( $request ) {
12691268
* @return WP_REST_Response
12701269
*/
12711270
public function get_forms_config( WP_REST_Request $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1272-
$has_ai = false;
1273-
if ( class_exists( 'Jetpack_AI_Helper' ) ) {
1274-
$feature = Jetpack_AI_Helper::get_ai_assistance_feature();
1275-
$has_ai = ! is_wp_error( $feature ) ? ( $feature['has-feature'] ?? false ) : false;
1276-
}
1277-
12781271
$config = array(
12791272
// From jpFormsBlocks in class-contact-form-block.php.
12801273
'formsResponsesUrl' => Forms_Dashboard::get_forms_admin_url(),
@@ -1286,7 +1279,6 @@ public function get_forms_config( WP_REST_Request $request ) { // phpcs:ignore V
12861279
'pluginAssetsURL' => Jetpack_Forms::assets_url(),
12871280
'siteURL' => ( new Status() )->get_site_suffix(),
12881281
'hasFeedback' => ( new Forms_Dashboard() )->has_feedback(),
1289-
'hasAI' => $has_ai,
12901282
'isIntegrationsEnabled' => Jetpack_Forms::is_integrations_enabled(),
12911283
'dashboardURL' => Forms_Dashboard::get_forms_admin_url(),
12921284
// New data.

projects/packages/forms/src/hooks/use-config-value.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type { FormsConfigData } from '../types';
1313
*
1414
* @example
1515
* const isMailPoetEnabled = useConfigValue( 'isMailPoetEnabled' );
16-
* const hasAI = useConfigValue( 'hasAI' );
1716
*/
1817
export default function useConfigValue< K extends keyof FormsConfigData >(
1918
key: K

projects/packages/forms/src/store/config/README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import useConfigValue from '../hooks/use-config-value';
1515

1616
function MyComponent() {
1717
const isMailPoetEnabled = useConfigValue('isMailPoetEnabled');
18-
const hasAI = useConfigValue('hasAI');
1918
const blogId = useConfigValue('blogId');
2019

2120
if (isMailPoetEnabled === undefined) {
@@ -35,7 +34,6 @@ The store provides access to the following configuration values (see `FormsConfi
3534
- `canInstallPlugins` - Whether the current user can install plugins
3635
- `canActivatePlugins` - Whether the current user can activate plugins
3736
- `hasFeedback` - Whether there are any form responses on the site
38-
- `hasAI` - Whether AI Assist features are available
3937
- `formsResponsesUrl` - URL of the Forms responses list in wp-admin
4038
- `blogId` - Current site blog ID
4139
- `gdriveConnectSupportURL` - Support URL for Google Drive connect guidance
@@ -54,9 +52,9 @@ The store provides access to the following configuration values (see `FormsConfi
5452
import useConfigValue from '../hooks/use-config-value';
5553

5654
function ExampleComponent() {
57-
const hasAI = useConfigValue('hasAI');
55+
const hasFeedback = useConfigValue('hasFeedback');
5856

59-
return hasAI ? <AIFeature /> : <RegularFeature />;
57+
return hasFeedback ? <ResponsesPanel /> : <EmptyState />;
6058
}
6159
```
6260

@@ -96,8 +94,8 @@ function AdvancedComponent() {
9694
);
9795

9896
// Get a specific value
99-
const hasAI = useSelect(
100-
select => select(CONFIG_STORE).getConfigValue('hasAI'),
97+
const canInstall = useSelect(
98+
select => select(CONFIG_STORE).getConfigValue('canInstallPlugins'),
10199
[]
102100
);
103101

@@ -209,20 +207,20 @@ The config store has one resolver: `getConfig`
209207

210208
**How `useConfigValue` works:**
211209

212-
When you call `useConfigValue('hasAI')`:
210+
When you call `useConfigValue('blogId')`:
213211
1. The hook internally calls `getConfig()` selector to fetch the entire config object
214212
2. WordPress automatically triggers the `getConfig` resolver if config isn't loaded
215213
3. The resolver checks if config is already loaded or currently loading via `isFulfilled`
216214
4. If not loaded, it fetches from `/wp/v2/feedback/config`
217-
5. Once loaded, it returns the value for the specific key (`config.hasAI`)
215+
5. Once loaded, it returns the value for the specific key (`config.blogId`)
218216
6. Subsequent calls to `useConfigValue()` with any key use the cached config
219217

220218
**Request Deduplication:**
221219

222220
Multiple components calling different config values simultaneously:
223221
```typescript
224222
// Component A
225-
const hasAI = useConfigValue('hasAI');
223+
const siteURL = useConfigValue('siteURL');
226224

227225
// Component B
228226
const blogId = useConfigValue('blogId');
@@ -284,7 +282,6 @@ First, add your new config value to the REST API endpoint response. In the Forms
284282
public function get_config() {
285283
return array(
286284
'isMailPoetEnabled' => $this->is_mailpoet_enabled(),
287-
'hasAI' => $this->has_ai(),
288285
// Add your new key here
289286
'myNewFeature' => $this->check_my_new_feature(),
290287
);
@@ -300,8 +297,7 @@ export interface FormsConfigData {
300297
/** Whether MailPoet integration is enabled across contexts. */
301298
isMailPoetEnabled?: boolean;
302299

303-
/** Whether AI Assist features are available for the site/user. */
304-
hasAI?: boolean;
300+
// Add your new key here with appropriate doc
305301

306302
/** Whether my new feature is enabled. */
307303
myNewFeature?: boolean; // Add your new key with proper JSDoc

projects/packages/forms/src/types/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ export interface FormsConfigData {
224224
canActivatePlugins?: boolean;
225225
/** Whether there are any feedback (form response) posts on the site. */
226226
hasFeedback?: boolean;
227-
/** Whether AI Assist features are available for the site/user. */
228-
hasAI?: boolean;
229227
/** The URL of the Forms responses list in wp-admin. */
230228
formsResponsesUrl?: string;
231229
/** Current site blog ID. */

projects/packages/forms/tests/js/hooks/use-config-value.test.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const mockConfigData = {
1515
canInstallPlugins: false,
1616
canActivatePlugins: true,
1717
hasFeedback: true,
18-
hasAI: false,
1918
formsResponsesUrl: 'https://example.com/wp-admin/edit.php?post_type=feedback',
2019
blogId: 12345,
2120
gdriveConnectSupportURL: 'https://example.com/support',
@@ -43,7 +42,7 @@ describe( 'useConfigValue', () => {
4342
} );
4443

4544
it( 'returns undefined when config is not loaded', () => {
46-
const { result } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
45+
const { result } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), { wrapper } );
4746

4847
expect( result.current ).toBeUndefined();
4948
} );
@@ -52,9 +51,9 @@ describe( 'useConfigValue', () => {
5251
// Populate the store with config data
5352
registry.dispatch( CONFIG_STORE ).receiveConfig( mockConfigData );
5453

55-
const { result } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
54+
const { result } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), { wrapper } );
5655

57-
expect( result.current ).toBe( false );
56+
expect( result.current ).toBe( true );
5857
} );
5958

6059
it( 'returns boolean values correctly', () => {
@@ -63,14 +62,12 @@ describe( 'useConfigValue', () => {
6362
const { result: mailpoetResult } = renderHook( () => useConfigValue( 'isMailPoetEnabled' ), {
6463
wrapper,
6564
} );
66-
const { result: aiResult } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
6765
const { result: integrationsResult } = renderHook(
6866
() => useConfigValue( 'isIntegrationsEnabled' ),
6967
{ wrapper }
7068
);
7169

7270
expect( mailpoetResult.current ).toBe( true );
73-
expect( aiResult.current ).toBe( false );
7471
expect( integrationsResult.current ).toBe( true );
7572
} );
7673

@@ -101,21 +98,23 @@ describe( 'useConfigValue', () => {
10198
it( 'returns undefined for non-existent keys', () => {
10299
registry.dispatch( CONFIG_STORE ).receiveConfig( { isMailPoetEnabled: true } );
103100

104-
const { result } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
101+
const { result } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), { wrapper } );
105102

106103
expect( result.current ).toBeUndefined();
107104
} );
108105

109106
it( 'updates when config value changes', () => {
110-
registry.dispatch( CONFIG_STORE ).receiveConfig( { hasAI: false } );
107+
registry.dispatch( CONFIG_STORE ).receiveConfig( { isIntegrationsEnabled: false } );
111108

112-
const { result, rerender } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
109+
const { result, rerender } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), {
110+
wrapper,
111+
} );
113112

114113
expect( result.current ).toBe( false );
115114

116115
// Update the config
117116
act( () => {
118-
registry.dispatch( CONFIG_STORE ).receiveConfigValue( 'hasAI', true );
117+
registry.dispatch( CONFIG_STORE ).receiveConfigValue( 'isIntegrationsEnabled', true );
119118
} );
120119
rerender();
121120

@@ -125,23 +124,28 @@ describe( 'useConfigValue', () => {
125124
it( 'multiple hooks can read different config values', () => {
126125
registry.dispatch( CONFIG_STORE ).receiveConfig( mockConfigData );
127126

128-
const { result: hasAI } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
127+
const { result: isIntegrations } = renderHook(
128+
() => useConfigValue( 'isIntegrationsEnabled' ),
129+
{ wrapper }
130+
);
129131
const { result: blogId } = renderHook( () => useConfigValue( 'blogId' ), { wrapper } );
130132
const { result: isMailPoet } = renderHook( () => useConfigValue( 'isMailPoetEnabled' ), {
131133
wrapper,
132134
} );
133135

134-
expect( hasAI.current ).toBe( false );
136+
expect( isIntegrations.current ).toBe( true );
135137
expect( blogId.current ).toBe( 12345 );
136138
expect( isMailPoet.current ).toBe( true );
137139
} );
138140

139141
it( 'returns undefined when config is invalidated', () => {
140142
registry.dispatch( CONFIG_STORE ).receiveConfig( mockConfigData );
141143

142-
const { result, rerender } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
144+
const { result, rerender } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), {
145+
wrapper,
146+
} );
143147

144-
expect( result.current ).toBe( false );
148+
expect( result.current ).toBe( true );
145149

146150
// Invalidate the config
147151
act( () => {
@@ -156,38 +160,43 @@ describe( 'useConfigValue', () => {
156160
// Only set a few config values
157161
registry.dispatch( CONFIG_STORE ).receiveConfig( {
158162
isMailPoetEnabled: true,
159-
hasAI: false,
163+
isIntegrationsEnabled: false,
160164
} );
161165

162166
const { result: mailpoetResult } = renderHook( () => useConfigValue( 'isMailPoetEnabled' ), {
163167
wrapper,
164168
} );
165-
const { result: aiResult } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
169+
const { result: integrationsResult } = renderHook(
170+
() => useConfigValue( 'isIntegrationsEnabled' ),
171+
{ wrapper }
172+
);
166173
const { result: blogIdResult } = renderHook( () => useConfigValue( 'blogId' ), { wrapper } );
167174

168175
expect( mailpoetResult.current ).toBe( true );
169-
expect( aiResult.current ).toBe( false );
176+
expect( integrationsResult.current ).toBe( false );
170177
expect( blogIdResult.current ).toBeUndefined();
171178
} );
172179

173180
it( 'works with different config keys in the same component', () => {
174181
registry.dispatch( CONFIG_STORE ).receiveConfig( mockConfigData );
175182

176-
const { result: result1 } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
183+
const { result: result1 } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), {
184+
wrapper,
185+
} );
177186
const { result: result2 } = renderHook( () => useConfigValue( 'blogId' ), { wrapper } );
178187
const { result: result3 } = renderHook( () => useConfigValue( 'canInstallPlugins' ), {
179188
wrapper,
180189
} );
181190

182-
expect( result1.current ).toBe( false );
191+
expect( result1.current ).toBe( true );
183192
expect( result2.current ).toBe( 12345 );
184193
expect( result3.current ).toBe( false );
185194
} );
186195

187196
it( 'handles empty config object', () => {
188197
registry.dispatch( CONFIG_STORE ).receiveConfig( {} );
189198

190-
const { result } = renderHook( () => useConfigValue( 'hasAI' ), { wrapper } );
199+
const { result } = renderHook( () => useConfigValue( 'isIntegrationsEnabled' ), { wrapper } );
191200

192201
expect( result.current ).toBeUndefined();
193202
} );

0 commit comments

Comments
 (0)