From a4201123b224d738148f62beea5d27b37b80d431 Mon Sep 17 00:00:00 2001 From: Adnan Haque <3737780+haqadn@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:44:52 +0100 Subject: [PATCH] Boost: Update foundation page list instructions (#39885) --- .../lib/stores/foundation-pages.ts | 1 - .../features/foundation-pages/meta/meta.tsx | 76 ++++++++++++------- .../app/data-sync/Foundation_Pages_Entry.php | 5 ++ .../boost/app/lib/Foundation_Pages.php | 16 ---- 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/projects/plugins/boost/app/assets/src/js/features/foundation-pages/lib/stores/foundation-pages.ts b/projects/plugins/boost/app/assets/src/js/features/foundation-pages/lib/stores/foundation-pages.ts index 88f80ab954991..cbcb863cb0396 100644 --- a/projects/plugins/boost/app/assets/src/js/features/foundation-pages/lib/stores/foundation-pages.ts +++ b/projects/plugins/boost/app/assets/src/js/features/foundation-pages/lib/stores/foundation-pages.ts @@ -25,7 +25,6 @@ export function useFoundationPages(): [ const FoundationPagesProperties = z.object( { max_pages: z.number(), - blog_url: z.string().nullable(), } ); type FoundationPagesProperties = z.infer< typeof FoundationPagesProperties >; diff --git a/projects/plugins/boost/app/assets/src/js/features/foundation-pages/meta/meta.tsx b/projects/plugins/boost/app/assets/src/js/features/foundation-pages/meta/meta.tsx index 71d8a2093fd17..b5f1abeb3b578 100644 --- a/projects/plugins/boost/app/assets/src/js/features/foundation-pages/meta/meta.tsx +++ b/projects/plugins/boost/app/assets/src/js/features/foundation-pages/meta/meta.tsx @@ -33,17 +33,19 @@ const Meta = () => { items={ foundationPages.join( '\n' ) } setItems={ updateFoundationPages } maxItems={ foundationPagesProperties.max_pages } - description={ - foundationPagesProperties.blog_url && + description={ createInterpolateElement( sprintf( - /* translators: %s is the blog URL. */ + /* translators: %s is the site URL. */ __( - 'No need to add the blog URL (%s) to the list, it is automatically kept up to date.', + 'Add one URL per line. Only URLs starting with %s will be included. Relative URLs are automatically expanded.', 'jetpack-boost' ), - foundationPagesProperties.blog_url - ) - } + Jetpack_Boost.site.url + ), + { + b: , + } + ) } /> ); } else { @@ -119,12 +121,20 @@ const List: React.FC< ListProps > = ( { items, setItems, maxItems, description } const [ inputValue, setInputValue ] = useState( items ); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [ inputInvalid, setInputInvalid ] = useState( false ); + const [ validationError, setValidationError ] = useState< Error | null >( null ); const inputRows = Math.min( maxItems, 10 ); const validateInputValue = ( value: string ) => { setInputValue( value ); - setInputInvalid( ! validateItems( value ) ); + try { + const isValid = validateItems( value ); + setInputInvalid( ! isValid ); + setValidationError( null ); + } catch ( e ) { + setInputInvalid( true ); + setValidationError( e as Error ); + } }; const validateItems = ( value: string ) => { @@ -133,14 +143,37 @@ const List: React.FC< ListProps > = ( { items, setItems, maxItems, description } .map( line => line.trim() ) .filter( line => line.trim() !== '' ); - // There should always be at least one foundation page. - if ( lines.length === 0 ) { - return false; - } - // Check if the number of items exceeds maxItems if ( lines.length > maxItems ) { - return false; + const message = sprintf( + /* translators: %d is the maximum number of foundation page URLs. */ + _n( + 'You can add only %d foundation page URL.', + 'You can add up to %d foundation page URLs.', + maxItems, + 'jetpack-boost' + ), + maxItems + ); + throw new Error( message ); + } + + for ( const line of lines ) { + let url: URL | undefined; + try { + url = new URL( line ); + } catch ( e ) { + // If the URL is invalid, they have provided a relative URL, which we will allow. + } + if ( + url && + url.origin.replace( /\/$/, '' ) !== Jetpack_Boost.site.url.replace( /\/$/, '' ) + ) { + throw new Error( + /* translators: %s is the URL that didn't match the site URL */ + sprintf( __( 'The URL seems to be a different site: %s', 'jetpack-boost' ), line ) + ); + } } return true; @@ -166,20 +199,7 @@ const List: React.FC< ListProps > = ( { items, setItems, maxItems, description } onChange={ e => validateInputValue( e.target.value ) } id="jb-foundation-pages" /> - { inputInvalid && ( -

- { sprintf( - /* translators: %d is the maximum number of foundation page URLs. */ - _n( - 'You must provide %d foundation page URL.', - 'You must provide between 1 and %d foundation page URLs.', - maxItems, - 'jetpack-boost' - ), - maxItems - ) } -

- ) } + { inputInvalid &&

{ validationError?.message }

} { description &&
{ description }
}