Skip to content

Commit

Permalink
AI Client: fix empty prompts handling (#34547)
Browse files Browse the repository at this point in the history
* use a default value to keep track of changes in the prompt, allow for empty prompts to show suggestion action buttons

* use null instead of a made up default value
  • Loading branch information
CGastrell committed Dec 15, 2023
1 parent 9e9417e commit e90e463
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
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

0 comments on commit e90e463

Please sign in to comment.