forked from WordPress/ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstage.tsx
More file actions
660 lines (583 loc) · 17 KB
/
stage.tsx
File metadata and controls
660 lines (583 loc) · 17 KB
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
/**
* WordPress dependencies
*/
import { Page } from '@wordpress/admin-ui';
import { Button, Notice, Spinner, ToggleControl } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { DataForm } from '@wordpress/dataviews';
import { useCallback, useMemo } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import type { DataFormControlProps, Field, Form } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import './style.scss';
import AIIcon from './ai-icon';
type AISettings = Record< string, boolean >;
interface FeatureGroupData {
id: string;
label: string;
description: string;
}
interface FeatureData {
id: string;
settingName: string;
label: string;
description: string;
category: string;
}
interface PageData {
hasCredentials: boolean;
hasValidCredentials: boolean;
connectorsUrl: string;
featureGroups: FeatureGroupData[];
features: FeatureData[];
}
interface SelectAllToggleProps extends DataFormControlProps< AISettings > {
featureSettingNames: string[];
groupLabel: string;
globalEnabled: boolean;
}
const FEATURE_SETTING_PATTERN = /^wpai_feature_(.+)_enabled$/;
const GLOBAL_FIELD_ID = 'wpai_features_enabled';
const SELECT_ALL_FIELD_PREFIX = 'wpai_select_all_';
function isRecord( value: unknown ): value is Record< string, unknown > {
return typeof value === 'object' && value !== null;
}
function toStringValue( value: unknown ): string {
return typeof value === 'string' ? value : '';
}
function isDefined< T >( value: T | null | undefined ): value is T {
return value !== null && value !== undefined;
}
function parseFeatureGroup( value: unknown ): FeatureGroupData | null {
if ( ! isRecord( value ) ) {
return null;
}
const featureGroup = value as Partial< FeatureGroupData >;
const id = toStringValue( featureGroup.id );
if ( ! id ) {
return null;
}
return {
id,
label: toStringValue( featureGroup.label ) || id,
description: toStringValue( featureGroup.description ),
};
}
function parseFeature( value: unknown ): FeatureData | null {
if ( ! isRecord( value ) ) {
return null;
}
const feature = value as Partial< FeatureData >;
const settingName = toStringValue( feature.settingName );
if ( ! settingName ) {
return null;
}
const id =
toStringValue( feature.id ) ||
getFeatureIdFromSettingName( settingName );
return {
id,
settingName,
label: toStringValue( feature.label ) || getDefaultLabel( id ),
description: toStringValue( feature.description ),
category: toStringValue( feature.category ) || 'other',
};
}
function getFeatureIdFromSettingName( settingName: string ): string {
const match = FEATURE_SETTING_PATTERN.exec( settingName );
return match?.[ 1 ] ?? settingName;
}
function getDefaultLabel( key: string ): string {
return key
.split( /[-_]/ )
.filter( Boolean )
.map( ( part ) => part[ 0 ]?.toUpperCase() + part.slice( 1 ) )
.join( ' ' );
}
function getSectionId( groupId: string ): string {
return `feature-group-${ groupId.replace( /[^a-zA-Z0-9_-]/g, '-' ) }`;
}
function getSelectAllFieldId( groupId: string ): string {
return `${ SELECT_ALL_FIELD_PREFIX }${ groupId.replace(
/[^a-zA-Z0-9_-]/g,
'_'
) }`;
}
function createSelectAllEditComponent(
featureSettingNames: string[],
groupLabel: string,
globalEnabled: boolean
): React.ComponentType< DataFormControlProps< AISettings > > {
return function SelectAllEdit( props ) {
return (
<SelectAllToggle
{ ...props }
featureSettingNames={ featureSettingNames }
groupLabel={ groupLabel }
globalEnabled={ globalEnabled }
/>
);
};
}
function buildFallbackFeatureGroups(
features: FeatureData[]
): FeatureGroupData[] {
const categories = Array.from(
new Set( features.map( ( feature ) => feature.category || 'other' ) )
);
return categories.map( ( category ) => ( {
id: category,
label:
category === 'other'
? __( 'Other Features', 'ai' )
: getDefaultLabel( category ),
description:
category === 'other'
? __( 'Additional AI-powered features.', 'ai' )
: '',
} ) );
}
function getPageData(): PageData {
const fallback: PageData = {
hasCredentials: false,
hasValidCredentials: false,
connectorsUrl: '',
featureGroups: [],
features: [],
};
try {
const rawData = JSON.parse(
document.getElementById( 'wp-script-module-data-ai-wp-admin' )
?.textContent ?? '{}'
);
if ( ! isRecord( rawData ) ) {
return fallback;
}
const pageData = rawData as Partial< PageData >;
const featureGroups = Array.isArray( pageData.featureGroups )
? pageData.featureGroups
.map( parseFeatureGroup )
.filter( isDefined )
: [];
const features = Array.isArray( pageData.features )
? pageData.features.map( parseFeature ).filter( isDefined )
: [];
return {
hasCredentials: Boolean( pageData.hasCredentials ),
hasValidCredentials: Boolean( pageData.hasValidCredentials ),
connectorsUrl: toStringValue( pageData.connectorsUrl ),
featureGroups,
features,
};
} catch {
return fallback;
}
}
const PAGE_DATA = getPageData();
const GLOBAL_FIELD: Field< AISettings > = {
id: GLOBAL_FIELD_ID,
label: __( 'Enable AI', 'ai' ),
type: 'boolean',
Edit: 'toggle',
};
function DisabledToggle( { field, data }: DataFormControlProps< AISettings > ) {
return (
<ToggleControl
__nextHasNoMarginBottom
label={ field.label }
help={ field.description }
checked={ !! field.getValue( { item: data } ) }
// No-op handler required to satisfy React's controlled-component warning; the toggle is disabled.
onChange={ () => {} }
disabled
/>
);
}
function SelectAllToggle( {
data,
onChange,
featureSettingNames,
groupLabel,
globalEnabled,
}: SelectAllToggleProps ) {
const enabledCount = featureSettingNames.filter(
( settingName ) => !! data[ settingName ]
).length;
const isAllEnabled = enabledCount === featureSettingNames.length;
// Dynamic label based on current state
const label = isAllEnabled
? sprintf(
// translators: %s: Group label (e.g., "Editor Experiments")
__( 'Disable all %s', 'ai' ),
groupLabel
)
: sprintf(
// translators: %s: Group label (e.g., "Editor Experiments")
__( 'Enable all %s', 'ai' ),
groupLabel
);
const handleToggle = useCallback(
( checked: boolean ) => {
const updates: Record< string, boolean > = {};
for ( const settingName of featureSettingNames ) {
updates[ settingName ] = checked;
}
onChange( updates );
},
[ featureSettingNames, onChange ]
);
return (
<div className="ai-select-all-toggle">
<ToggleControl
__nextHasNoMarginBottom
label={ label }
checked={ isAllEnabled }
onChange={ handleToggle }
disabled={ ! globalEnabled }
/>
</div>
);
}
function AISettingsPage() {
const { siteSettings, isLoading } = useSelect( ( select ) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- core-data store selectors aren't fully typed for 'root'/'site' entity args.
const store: any = select( coreStore );
return {
siteSettings: store.getEditedEntityRecord( 'root', 'site' ) as
| Record< string, unknown >
| undefined,
isLoading: ! store.hasFinishedResolution( 'getEntityRecord', [
'root',
'site',
] ) as boolean,
};
}, [] );
const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const featureDefinitions = useMemo< FeatureData[] >( () => {
const sourceFeatures =
PAGE_DATA.features.length > 0
? PAGE_DATA.features
: Object.keys( siteSettings ?? {} )
.filter( ( key ) =>
FEATURE_SETTING_PATTERN.test( key )
)
.sort()
.map( ( settingName ) => {
const id =
getFeatureIdFromSettingName( settingName );
return {
id,
settingName,
label: getDefaultLabel( id ),
description: '',
category: 'other',
};
} );
const uniqueFeatures: FeatureData[] = [];
const seenSettingNames = new Set< string >();
for ( const feature of sourceFeatures ) {
if ( seenSettingNames.has( feature.settingName ) ) {
continue;
}
seenSettingNames.add( feature.settingName );
uniqueFeatures.push( feature );
}
return uniqueFeatures;
}, [ siteSettings ] );
const featureGroups = useMemo< FeatureGroupData[] >(
() =>
PAGE_DATA.featureGroups.length > 0
? PAGE_DATA.featureGroups
: buildFallbackFeatureGroups( featureDefinitions ),
[ featureDefinitions ]
);
const aiSettingKeys = useMemo( () => {
const settingKeys = new Set< string >( [ GLOBAL_FIELD_ID ] );
for ( const feature of featureDefinitions ) {
settingKeys.add( feature.settingName );
}
// Add select-all field IDs (they're virtual, not saved to DB)
for ( const group of featureGroups ) {
settingKeys.add( getSelectAllFieldId( group.id ) );
}
return Array.from( settingKeys );
}, [ featureDefinitions, featureGroups ] );
const data: AISettings = useMemo( () => {
const aiSettings: AISettings = {};
for ( const key of aiSettingKeys ) {
aiSettings[ key ] = Boolean( siteSettings?.[ key ] ?? false );
}
return aiSettings;
}, [ aiSettingKeys, siteSettings ] );
const globalEnabled = Boolean( data[ GLOBAL_FIELD.id ] );
const fields = useMemo< Field< AISettings >[] >( () => {
// Group features by category to create select-all fields
const groupedFeatures = new Map< string, FeatureData[] >();
for ( const feature of featureDefinitions ) {
const category = feature.category || 'other';
const categoryFeatures = groupedFeatures.get( category ) ?? [];
categoryFeatures.push( feature );
groupedFeatures.set( category, categoryFeatures );
}
// Create select-all fields for each group
const selectAllFields: Field< AISettings >[] = [];
for ( const group of featureGroups ) {
const groupFeatures = groupedFeatures.get( group.id ) ?? [];
if ( groupFeatures.length === 0 ) {
continue;
}
const featureSettingNames = groupFeatures.map(
( f ) => f.settingName
);
selectAllFields.push( {
id: getSelectAllFieldId( group.id ),
label: '', // Dynamic label set by SelectAllToggle component
type: 'boolean',
Edit: createSelectAllEditComponent(
featureSettingNames,
group.label,
globalEnabled
),
} );
}
// Create individual feature fields
const featureFields = featureDefinitions.map( ( feature ) => ( {
id: feature.settingName,
label: feature.label,
description: feature.description,
type: 'boolean' as const,
Edit: globalEnabled ? ( 'toggle' as const ) : DisabledToggle,
} ) );
return [ GLOBAL_FIELD, ...selectAllFields, ...featureFields ];
}, [ featureDefinitions, featureGroups, globalEnabled ] );
const form = useMemo< Form >( () => {
const groupedFields = new Map< string, string[] >();
for ( const feature of featureDefinitions ) {
const category = feature.category || 'other';
const categoryFields = groupedFields.get( category ) ?? [];
categoryFields.push( feature.settingName );
groupedFields.set( category, categoryFields );
}
const sectionFields: NonNullable< Form[ 'fields' ] > = [];
const seenCategories = new Set< string >();
for ( const group of featureGroups ) {
const children = groupedFields.get( group.id ) ?? [];
if ( children.length === 0 ) {
continue;
}
seenCategories.add( group.id );
// Add select-all field as first child
const selectAllFieldId = getSelectAllFieldId( group.id );
const sectionChildren = [ selectAllFieldId, ...children ];
sectionFields.push( {
id: getSectionId( group.id ),
label: group.label,
description: group.description,
layout: {
type: 'card',
withHeader: true,
isOpened: true,
isCollapsible: true,
},
children: sectionChildren,
} );
}
for ( const [ category, children ] of groupedFields.entries() ) {
if ( children.length === 0 || seenCategories.has( category ) ) {
continue;
}
// Add select-all field as first child
const selectAllFieldId = getSelectAllFieldId( category );
const sectionChildren = [ selectAllFieldId, ...children ];
sectionFields.push( {
id: getSectionId( category ),
label: getDefaultLabel( category ),
description: '',
layout: {
type: 'card',
withHeader: true,
isOpened: true,
isCollapsible: true,
},
children: sectionChildren,
} );
}
return {
fields: [
{
id: 'generalSettings',
label: __( 'General Settings', 'ai' ),
description: __(
'Control whether AI is enabled for your site. When disabled, all features and experiments will be inactive regardless of their individual settings.',
'ai'
),
layout: {
type: 'card',
withHeader: true,
isCollapsible: false,
},
children: [ GLOBAL_FIELD_ID ],
},
...sectionFields,
],
};
}, [ featureDefinitions, featureGroups ] );
const handleChange = useCallback(
async ( edits: Record< string, unknown > ) => {
// Filter out virtual select-all fields - they shouldn't be saved
const actualEdits: Record< string, unknown > = {};
for ( const [ key, value ] of Object.entries( edits ) ) {
if ( ! key.startsWith( SELECT_ALL_FIELD_PREFIX ) ) {
actualEdits[ key ] = value;
}
}
// If no actual edits after filtering, skip
if ( Object.keys( actualEdits ).length === 0 ) {
return;
}
// @ts-expect-error -- core-data types don't expose editEntityRecord for 'root'/'site' args.
editEntityRecord( 'root', 'site', undefined, actualEdits );
// Determine success message
let message: string;
const editCount = Object.keys( actualEdits ).length;
if ( editCount > 1 ) {
// Bulk edit message
const enabledCount =
Object.values( actualEdits ).filter( Boolean ).length;
if ( enabledCount === editCount ) {
message = sprintf(
// translators: %d: number of experiments.
__( '%d experiments enabled.', 'ai' ),
editCount
);
} else if ( enabledCount === 0 ) {
message = sprintf(
// translators: %d: number of experiments.
__( '%d experiments disabled.', 'ai' ),
editCount
);
} else {
message = sprintf(
// translators: %d: number of experiments.
__( '%d experiments updated.', 'ai' ),
editCount
);
}
} else {
// Single edit message
const entry = Object.entries( actualEdits )[ 0 ];
if ( ! entry ) {
message = __( 'Settings saved.', 'ai' );
} else if ( entry[ 0 ] === GLOBAL_FIELD_ID ) {
message = entry[ 1 ]
? __( 'AI enabled.', 'ai' )
: __( 'AI disabled.', 'ai' );
} else {
const feature = featureDefinitions.find(
( f ) => f.settingName === entry[ 0 ]
);
const label = feature?.label ?? entry[ 0 ];
if ( entry[ 1 ] ) {
// translators: %s: Feature label.
message = sprintf( __( '%s enabled.', 'ai' ), label );
} else {
// translators: %s: Feature label.
message = sprintf( __( '%s disabled.', 'ai' ), label );
}
}
}
try {
// @ts-expect-error -- core-data types don't expose saveEditedEntityRecord for 'root'/'site' args.
await saveEditedEntityRecord( 'root', 'site' );
createSuccessNotice( message, { type: 'snackbar' } );
} catch {
createErrorNotice( __( 'Failed to save settings.', 'ai' ), {
type: 'snackbar',
} );
}
},
[
editEntityRecord,
saveEditedEntityRecord,
createSuccessNotice,
createErrorNotice,
featureDefinitions,
]
);
return (
<Page
title={
<>
<AIIcon />
{ __( 'AI', 'ai' ) }
</>
}
subTitle={ __(
'Configure AI features and experiments for your WordPress site.',
'ai'
) }
actions={
<>
<Button
variant="secondary"
href="https://github.com/WordPress/ai/tree/develop/docs"
target="_blank"
rel="noopener noreferrer"
>
{ __( 'Docs', 'ai' ) }
</Button>
<Button
variant="primary"
href="https://github.com/WordPress/ai/blob/develop/CONTRIBUTING.md"
target="_blank"
rel="noopener noreferrer"
>
{ __( 'Contribute', 'ai' ) }
</Button>
</>
}
>
<div className="ai-settings-page">
{ ! PAGE_DATA.hasValidCredentials && (
<Notice status="error" isDismissible={ false }>
{ ! PAGE_DATA.hasCredentials
? __(
'The AI plugin requires a valid AI Connector to function properly. Verify you have one or more AI Connectors configured.',
'ai'
)
: __(
'The AI plugin requires a valid AI Connector to function properly. Please review the AI Connectors you have configured to ensure they are valid.',
'ai'
) }{ ' ' }
{ PAGE_DATA.connectorsUrl && (
<Button
variant="link"
href={ PAGE_DATA.connectorsUrl }
>
{ __( 'Manage Connectors', 'ai' ) }
</Button>
) }
</Notice>
) }
{ isLoading ? (
<Spinner />
) : (
<DataForm< AISettings >
data={ data }
fields={ fields }
form={ form }
onChange={ handleChange }
/>
) }
</div>
</Page>
);
}
export const stage = AISettingsPage;