Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI Client: fix empty prompts handling #34547

Merged
merged 2 commits into from
Dec 8, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Ai Client: use default last value (not empty), allows for handling 'one click' prompts (empty prompts)
63 changes: 34 additions & 29 deletions projects/js-packages/ai-client/src/components/ai-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ export function AIControl(
const promptUserInputRef = useRef( null );
const loading = state === 'requesting' || state === 'suggesting';
const [ editRequest, setEditRequest ] = React.useState( false );
const [ lastValue, setLastValue ] = React.useState( value || '' );
const [ lastValue, setLastValue ] = React.useState( value || null );

useEffect( () => {
if ( editRequest ) {
promptUserInputRef?.current?.focus();
}

if ( ! editRequest && lastValue && value !== lastValue ) {
if ( ! editRequest && lastValue !== null && value !== lastValue ) {
onChange?.( lastValue );
}
}, [ editRequest, lastValue ] );
Expand Down Expand Up @@ -229,34 +229,39 @@ export function AIControl(
</div>
) }

{ showAccept && ! editRequest && value?.length > 0 && (
{ showAccept && ! editRequest && (
<div className="jetpack-components-ai-control__controls-prompt_button_wrapper">
<ButtonGroup>
<Button
className="jetpack-components-ai-control__controls-prompt_button"
label={ __( 'Back to edit', 'jetpack-ai-client' ) }
onClick={ () => setEditRequest( true ) }
tooltipPosition="top"
>
<Icon icon={ arrowLeft } />
</Button>
<Button
className="jetpack-components-ai-control__controls-prompt_button"
label={ __( 'Discard', 'jetpack-ai-client' ) }
onClick={ discardHandler }
tooltipPosition="top"
>
<Icon icon={ trash } />
</Button>
<Button
className="jetpack-components-ai-control__controls-prompt_button"
label={ __( 'Regenerate', 'jetpack-ai-client' ) }
onClick={ () => onSend?.( value ) }
tooltipPosition="top"
>
<Icon icon={ reusableBlock } />
</Button>
</ButtonGroup>
{ ( value?.length > 0 || lastValue === null ) && (
<ButtonGroup>
{ value.length > 0 && (
<Button
className="jetpack-components-ai-control__controls-prompt_button"
label={ __( 'Back to edit', 'jetpack-ai-client' ) }
onClick={ () => setEditRequest( true ) }
tooltipPosition="top"
>
<Icon icon={ arrowLeft } />
</Button>
) }
<Button
className="jetpack-components-ai-control__controls-prompt_button"
label={ __( 'Discard', 'jetpack-ai-client' ) }
onClick={ discardHandler }
tooltipPosition="top"
>
<Icon icon={ trash } />
</Button>
<Button
className="jetpack-components-ai-control__controls-prompt_button"
label={ __( 'Regenerate', 'jetpack-ai-client' ) }
onClick={ () => onSend?.( value ) }
tooltipPosition="top"
disabled={ ! value?.length || value === null || disabled }
>
<Icon icon={ reusableBlock } />
</Button>
</ButtonGroup>
) }
<Button
className="jetpack-components-ai-control__controls-prompt_button"
onClick={ onAccept }
Expand Down