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 Title Optimization: Add error handling for Title Optimization #37195

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: minor
Type: other

Add error handling on Title Optimization
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default function TitleOptimization( {
const [ isTitleOptimizationModalVisible, setIsTitleOptimizationModalVisible ] = useState( false );
const [ generating, setGenerating ] = useState( false );
const [ options, setOptions ] = useState( [] );
const [ error, setError ] = useState( false );
const { editPost } = useDispatch( 'core/editor' );
const { autosave } = useAutoSaveAndRedirect();
const { increaseAiAssistantRequestsCount } = useDispatch( 'wordpress-com/plans' );
Expand Down Expand Up @@ -61,6 +62,7 @@ export default function TitleOptimization( {
const { request, stopSuggestion } = useAiSuggestions( {
onDone: handleDone,
onError: () => {
setError( true );
setGenerating( false );
},
} );
Expand Down Expand Up @@ -91,6 +93,12 @@ export default function TitleOptimization( {
handleRequest();
}, [ handleRequest, placement, recordEvent, toggleTitleOptimizationModal ] );

const handleTryAgain = useCallback( () => {
setError( false );
setGenerating( true );
handleRequest();
}, [ handleRequest ] );

const handleAccept = useCallback(
( event: MouseEvent ) => {
// track the generated title acceptance
Expand Down Expand Up @@ -120,7 +128,7 @@ export default function TitleOptimization( {
<p>{ __( 'Use AI to optimize key details of your post.', 'jetpack' ) }</p>
<Button
isBusy={ busy }
disabled={ disabled }
disabled={ ! postContent || disabled }
onClick={ handleTitleOptimization }
variant="secondary"
>
Expand All @@ -140,25 +148,42 @@ export default function TitleOptimization( {
</div>
) : (
<>
<span className="jetpack-ai-title-optimization__intro">
{ __( 'Choose an optimized title below:', 'jetpack' ) }
</span>
<TitleOptimizationOptions
onChangeValue={ e => setSelected( e.target.value ) }
selected={ selected }
options={ options?.map?.( option => ( {
value: option.title,
label: option.title,
description: option.explanation,
} ) ) }
/>
{ error ? (
<div className="jetpack-ai-title-optimization__error">
{ __(
'It failed to generate your suggested titles. Please try again.',
renatoagds marked this conversation as resolved.
Show resolved Hide resolved
'jetpack'
) }
</div>
) : (
<>
<span className="jetpack-ai-title-optimization__intro">
{ __( 'Choose an optimized title below:', 'jetpack' ) }
</span>
<TitleOptimizationOptions
onChangeValue={ e => setSelected( e.target.value ) }
selected={ selected }
options={ options?.map?.( option => ( {
value: option.title,
label: option.title,
description: option.explanation,
} ) ) }
/>
</>
) }
<div className="jetpack-ai-title-optimization__cta">
<Button variant="secondary" onClick={ toggleTitleOptimizationModal }>
{ __( 'Cancel', 'jetpack' ) }
</Button>
<Button variant="primary" onClick={ handleAccept }>
{ __( 'Replace title', 'jetpack' ) }
</Button>
{ error ? (
<Button variant="primary" onClick={ handleTryAgain }>
{ __( 'Try again', 'jetpack' ) }
</Button>
) : (
<Button variant="primary" onClick={ handleAccept }>
{ __( 'Replace title', 'jetpack' ) }
</Button>
) }
</div>
</>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
justify-content: center;
}

&__intro {
&__intro, &__error {
margin-top: 16px;
}

Expand Down
Loading