Skip to content

Commit

Permalink
adjust behavior upon observations
Browse files Browse the repository at this point in the history
  • Loading branch information
CGastrell committed Nov 30, 2023
1 parent 7eb35d5 commit 7892c9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
28 changes: 25 additions & 3 deletions projects/js-packages/ai-client/src/components/ai-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,37 @@ export function AIControl(
const promptUserInputRef = useRef( null );
const loading = state === 'requesting' || state === 'suggesting';
const [ editRequest, setEditRequest ] = React.useState( false );
const [ lastValue, setLastValue ] = React.useState( '' );

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

if ( ! editRequest && lastValue && value !== lastValue ) {
onChange?.( lastValue );
}
}, [ editRequest, lastValue ] );

const sendRequest = useCallback( () => {
setLastValue( value );
setEditRequest( false );
onSend?.( value );
}, [ value ] );

const changeHandler = useCallback(
( newValue: string ) => {
onChange?.( newValue );
setEditRequest( state !== 'init' && lastValue && lastValue !== newValue );
},
[ lastValue, state ]
);

const discardHandler = useCallback( () => {
onDiscard?.();
onAccept?.();
}, [] );

// Pass the ref to forwardRef.
useImperativeHandle( ref, () => promptUserInputRef.current );

Expand Down Expand Up @@ -147,7 +166,7 @@ export function AIControl(
<div className="jetpack-components-ai-control__input-wrapper">
<PlainText
value={ value }
onChange={ onChange }
onChange={ changeHandler }
placeholder={ placeholder }
className="jetpack-components-ai-control__input"
disabled={ loading || disabled }
Expand Down Expand Up @@ -211,20 +230,23 @@ export function AIControl(
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={ () => onDiscard?.() }
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,
* - Create blocks from HTML code
*/
const HTML = markdownConverter
.render( attributes.content )
.render( attributes.content || '' )
// Fix list indentation
.replace( /<li>\s+<p>/g, '<li>' )
.replace( /<\/p>\s+<\/li>/g, '</li>' );
Expand All @@ -359,7 +359,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,

const handleAcceptTitle = () => {
if ( isInBlockEditor ) {
editPost( { title: attributes.content.trim() } );
editPost( { title: attributes.content ? attributes.content.trim() : '' } );
removeBlock( clientId );
} else {
handleAcceptContent();
Expand Down Expand Up @@ -557,7 +557,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,
state={ requestingState }
isTransparent={ requireUpgrade || ! connected }
showButtonLabels={ ! isMobileViewport }
showAccept={ contentIsLoaded && ! isWaitingState }
showAccept={ requestingState !== 'init' && contentIsLoaded && ! isWaitingState }
acceptLabel={ acceptLabel }
showGuideLine={ contentIsLoaded }
/>
Expand Down

0 comments on commit 7892c9f

Please sign in to comment.