Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 89 additions & 28 deletions routes/ai-home/components/DeveloperSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import { Button, Spinner } from '@wordpress/components';
import { DataForm } from '@wordpress/dataviews';
import type { Field, Form } from '@wordpress/dataviews';
import { useCallback, useMemo, useRef } from '@wordpress/element';
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Notice } from '@wordpress/ui';

Expand Down Expand Up @@ -46,8 +52,26 @@ export function DeveloperSettings( {
const { settings, update, clear, isSaving } =
useDeveloperFeatureSettings( featureId );

const [ draftSettings, setDraftSettings ] =
useState< DeveloperSelection | null >( null );
const [ isSavingThis, setIsSavingThis ] = useState( false );

useEffect( () => {
if ( ! isSaving ) {
setIsSavingThis( false );
Comment thread
dkotter marked this conversation as resolved.
}
}, [ isSaving ] );

useEffect( () => {
setDraftSettings( null );
}, [ settings.provider, settings.model ] );

const currentSettings = draftSettings ?? settings;

const getModelElements = useCallback( () => {
const provider = providers.find( ( p ) => p.id === settings.provider );
const provider = providers.find(
( p ) => p.id === currentSettings.provider
);
if ( ! provider ) {
return Promise.resolve( [] );
}
Expand All @@ -58,7 +82,7 @@ export function DeveloperSettings( {
label: m.name,
} ) ),
] );
}, [ settings.provider, providers ] );
}, [ currentSettings.provider, providers ] );

const fields = useMemo< Field< DeveloperSelection >[] >(
() => [
Expand Down Expand Up @@ -97,18 +121,38 @@ export function DeveloperSettings( {
const handleChange = useCallback(
( changes: Partial< DeveloperSelection > ) => {
if ( 'provider' in changes ) {
void update( { provider: changes.provider ?? '', model: '' } );
setDraftSettings( {
provider: changes.provider ?? '',
model: '',
} );
} else {
void update( { ...settings, ...changes } );
setDraftSettings( { ...currentSettings, ...changes } );
}
},
[ update, settings ]
[ currentSettings ]
);

const hasSavedSelection = settings.provider !== '' || settings.model !== '';
const handleSave = useCallback( () => {
if ( draftSettings ) {
setIsSavingThis( true );
if ( draftSettings.provider === '' && draftSettings.model === '' ) {
void clear();
} else {
void update( draftSettings );
}
}
}, [ draftSettings, update, clear ] );

const hasSavedSelection =
currentSettings.provider !== '' || currentSettings.model !== '';
const hasUnsavedChanges =
draftSettings !== null &&
( draftSettings.provider !== settings.provider ||
draftSettings.model !== settings.model );

const hasStaleProvider =
!! settings.provider &&
! providers.find( ( p ) => p.id === settings.provider );
!! currentSettings.provider &&
! providers.find( ( p ) => p.id === currentSettings.provider );

if ( capability === 'none' ) {
return (
Expand Down Expand Up @@ -153,30 +197,47 @@ export function DeveloperSettings( {
) }
<div ref={ formWrapperRef }>
<DataForm< DeveloperSelection >
data={ settings }
data={ currentSettings }
fields={ fields }
form={ form }
onChange={ handleChange }
/>
</div>
{ hasSavedSelection && (
<Button
variant="link"
className="ai-developer-mode-fields__reset-button"
onClick={ () => {
formWrapperRef.current
?.querySelector< HTMLSelectElement >(
'select'
)
?.focus();
void clear();
} }
disabled={ isSaving }
accessibleWhenDisabled
>
{ __( 'Reset to default', 'ai' ) }
</Button>
) }
<div className="ai-developer-mode-fields__actions">
{ ( hasUnsavedChanges || isSavingThis ) && (
<Button
variant="primary"
onClick={ handleSave }
disabled={ isSavingThis || ! hasUnsavedChanges }
isBusy={ isSavingThis }
accessibleWhenDisabled
__next40pxDefaultSize
>
{ __( 'Save', 'ai' ) }
</Button>
) }
{ hasSavedSelection && (
<Button
variant="link"
className="ai-developer-mode-fields__reset-button"
onClick={ () => {
formWrapperRef.current
?.querySelector< HTMLSelectElement >(
'select'
)
?.focus();
setDraftSettings( {
provider: '',
model: '',
} );
} }
disabled={ isSavingThis }
accessibleWhenDisabled
>
{ __( 'Reset to default', 'ai' ) }
</Button>
) }
</div>
</>
) }
</div>
Expand Down
8 changes: 6 additions & 2 deletions routes/ai-home/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ body:has(.ai-settings-page) .components-popover {
margin-bottom: var(--wpds-dimension-gap-lg, 12px);
}

.ai-developer-mode-fields__reset-button {
margin-top: var(--wpds-dimension-gap-lg, 12px);
.ai-developer-mode-fields__actions {
display: flex;
align-items: center;
gap: var(--wpds-dimension-gap-lg, 16px);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're inconsistent with the fallback here, some places use 16px some use 12px. Should standardize that

margin-top: var(--wpds-dimension-gap-lg, 16px);
padding-bottom: var(--wpds-dimension-gap-sm, 8px);
}
}
Loading