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 &&