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

Jetpack SEO: persist selected option #41637

Merged
merged 3 commits into from
Feb 7, 2025
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: other

Jetpack SEO assistant: persist selected options if they haven't changed
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export default function AssistantWizard( { close } ) {
) }
{ currentStep === 2 && steps[ currentStep ].type === 'options' && (
<OptionsInput
disabled={ ! steps[ currentStep ].value }
disabled={ ! steps[ currentStep ].hasSelection }
submitCtaLabel={ steps[ currentStep ].submitCtaLabel }
retryCtaLabel={ steps[ currentStep ].retryCtaLabel }
handleRetry={ handleRetry }
Expand All @@ -230,7 +230,7 @@ export default function AssistantWizard( { close } ) {
) }
{ currentStep === 3 && steps[ currentStep ].type === 'options' && (
<OptionsInput
disabled={ ! steps[ currentStep ].value }
disabled={ ! steps[ currentStep ].hasSelection }
submitCtaLabel={ steps[ currentStep ].submitCtaLabel }
retryCtaLabel={ steps[ currentStep ].retryCtaLabel }
handleRetry={ handleRetry }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Message {
selected?: boolean;
}

export type OptionMessage = Pick< Message, 'id' | 'content' >;
export type OptionMessage = Pick< Message, 'id' | 'content' | 'selected' >;

export interface Results {
[ key: string ]: {
Expand Down Expand Up @@ -44,4 +44,5 @@ export interface Step {
submitCtaLabel?: string;
onRetry?: () => void;
retryCtaLabel?: string;
hasSelection?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useCompletionStep = (): Step => {
}
setMessages( firstMessages );

await new Promise( resolve => setTimeout( resolve, 1500 ) );
await new Promise( resolve => setTimeout( resolve, 1000 ) );

const resultsString = Object.values( results )
.map( ( result: Results[ string ] ) => `${ result.value ? '✅' : '❌' } ${ result.label }` )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useMessages } from './wizard-messages';
import type { Step, OptionMessage } from './types';

export const useMetaDescriptionStep = ( { keywords }: { keywords: string } ): Step => {
const [ value, setValue ] = useState< string >();
const [ selectedMetaDescription, setSelectedMetaDescription ] = useState< string >();
const [ metaDescriptionOptions, setMetaDescriptionOptions ] = useState< OptionMessage[] >( [] );
const { messages, setMessages, addMessage, editLastMessage, setSelectedMessage } = useMessages();
Expand Down Expand Up @@ -47,6 +48,9 @@ export const useMetaDescriptionStep = ( { keywords }: { keywords: string } ): St
( option: OptionMessage ) => {
setSelectedMetaDescription( option.content as string );
setSelectedMessage( option );
setMetaDescriptionOptions( prev =>
prev.map( o => ( { ...o, selected: o.id === option.id } ) )
);
},
[ setSelectedMessage ]
);
Expand All @@ -67,6 +71,7 @@ export const useMetaDescriptionStep = ( { keywords }: { keywords: string } ): St
}, [ generatedCount, request ] );

const handleMetaDescriptionSubmit = useCallback( async () => {
setValue( selectedMetaDescription );
await editPost( { meta: { advanced_seo_description: selectedMetaDescription } } );
addMessage( { content: __( 'Meta description updated! ✅', 'jetpack' ) } );
return selectedMetaDescription;
Expand All @@ -87,8 +92,8 @@ export const useMetaDescriptionStep = ( { keywords }: { keywords: string } ): St
showIcon: true,
};
let newMetaDescriptions = [ ...metaDescriptionOptions ];
// we only generate if options are empty
setMessages( [ initialMessage ] );
// we only generate if options are empty
if ( newMetaDescriptions.length === 0 ) {
newMetaDescriptions = await getMetaDescriptions();
}
Expand Down Expand Up @@ -133,8 +138,9 @@ export const useMetaDescriptionStep = ( { keywords }: { keywords: string } ): St
onRetry: handleMetaDescriptionRegenerate,
retryCtaLabel: __( 'Regenerate', 'jetpack' ),
onStart: handleMetaDescriptionGenerate,
value: selectedMetaDescription,
setValue: setSelectedMetaDescription,
value,
setValue,
includeInResults: true,
hasSelection: !! selectedMetaDescription,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useMessages } from './wizard-messages';
import type { Step, OptionMessage } from './types';

export const useTitleStep = ( { keywords }: { keywords: string } ): Step => {
const [ value, setValue ] = useState< string >( '' );
const [ selectedTitle, setSelectedTitle ] = useState< string >( '' );
const [ titleOptions, setTitleOptions ] = useState< OptionMessage[] >( [] );
const { editPost } = useDispatch( 'core/editor' );
Expand Down Expand Up @@ -47,6 +48,7 @@ export const useTitleStep = ( { keywords }: { keywords: string } ): Step => {
( option: OptionMessage ) => {
setSelectedTitle( option.content as string );
setSelectedMessage( option );
setTitleOptions( prev => prev.map( o => ( { ...o, selected: o.id === option.id } ) ) );
},
[ setSelectedMessage ]
);
Expand Down Expand Up @@ -135,6 +137,7 @@ export const useTitleStep = ( { keywords }: { keywords: string } ): Step => {
}, [ addMessage, getTitles, titleOptions ] );

const handleTitleSubmit = useCallback( async () => {
setValue( selectedTitle );
await editPost( { title: selectedTitle, meta: { jetpack_seo_html_title: selectedTitle } } );
addMessage( { content: __( 'Title updated! ✅', 'jetpack' ) } );
return selectedTitle;
Expand All @@ -153,8 +156,9 @@ export const useTitleStep = ( { keywords }: { keywords: string } ): Step => {
onRetry: handleTitleRegenerate,
retryCtaLabel: __( 'Regenerate', 'jetpack' ),
onStart: handleTitleGenerate,
value: selectedTitle,
setValue: setSelectedTitle,
value,
setValue,
includeInResults: true,
hasSelection: !! selectedTitle,
};
};
Loading