2023-09-19 05:54:19 +00:00
/* eslint-disable @woocommerce/dependency-group */
/* eslint-disable @typescript-eslint/ban-ts-comment */
2023-08-28 01:28:05 +00:00
/ * *
* External dependencies
* /
import { __ } from '@wordpress/i18n' ;
2023-09-19 05:54:19 +00:00
import {
createInterpolateElement ,
useCallback ,
useMemo ,
} from '@wordpress/element' ;
2023-08-28 01:28:05 +00:00
import { Link } from '@woocommerce/components' ;
2023-09-19 05:54:19 +00:00
import { Spinner } from '@wordpress/components' ;
// @ts-expect-error Missing type in core-data.
import { __experimentalBlockPatternsList as BlockPatternList } from '@wordpress/block-editor' ;
2023-09-19 03:37:46 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2023-08-28 01:28:05 +00:00
/ * *
* Internal dependencies
* /
import { SidebarNavigationScreen } from './sidebar-navigation-screen' ;
import { ADMIN_URL } from '~/utils/admin-settings' ;
2023-09-19 05:54:19 +00:00
import { useEditorBlocks } from '../hooks/use-editor-blocks' ;
import { useHomeTemplates } from '../hooks/use-home-templates' ;
import { BlockInstance } from '@wordpress/blocks' ;
2023-08-28 01:28:05 +00:00
export const SidebarNavigationScreenHomepage = ( ) = > {
2023-09-19 05:54:19 +00:00
const { isLoading , homeTemplates } = useHomeTemplates ( ) ;
const [ blocks , , onChange ] = useEditorBlocks ( ) ;
const onClickPattern = useCallback (
( _pattern , selectedBlocks ) = > {
onChange (
[ blocks [ 0 ] , . . . selectedBlocks , blocks [ blocks . length - 1 ] ] ,
{ selection : { } }
) ;
} ,
[ blocks , onChange ]
) ;
const homePatterns = useMemo ( ( ) = > {
return Object . entries ( homeTemplates ) . map (
( [ templateName , patterns ] ) = > {
return {
name : templateName ,
blocks : patterns.reduce (
( acc : BlockInstance [ ] , pattern ) = > [
. . . acc ,
. . . pattern . blocks ,
] ,
[ ]
) ,
} ;
}
) ;
} , [ homeTemplates ] ) ;
2023-08-28 01:28:05 +00:00
return (
< SidebarNavigationScreen
title = { __ ( 'Change your homepage' , 'woocommerce' ) }
description = { createInterpolateElement (
__ (
'Based on the most successful stores in your industry and location, our AI tool has recommended this template for your business. Prefer a different layout? Choose from the templates below now, or later via the <EditorLink>Editor</EditorLink>.' ,
'woocommerce'
) ,
{
EditorLink : (
< Link
2023-09-19 03:37:46 +00:00
onClick = { ( ) = > {
recordEvent (
'customize_your_store_assembler_hub_editor_link_click' ,
{
source : 'homepage' ,
}
) ;
window . open (
` ${ ADMIN_URL } site-editor.php ` ,
'_blank'
) ;
return false ;
} }
href = ""
2023-08-28 01:28:05 +00:00
/ >
) ,
}
) }
content = {
2023-09-19 05:54:19 +00:00
< div className = "woocommerce-customize-store__sidebar-homepage-content" >
< div className = "edit-site-sidebar-navigation-screen-patterns__group-homepage" >
{ isLoading ? (
< span className = "components-placeholder__preview" >
< Spinner / >
< / span >
) : (
< BlockPatternList
shownPatterns = { homePatterns }
blockPatterns = { homePatterns }
onClickPattern = { onClickPattern }
label = { 'Hompeage' }
orientation = "vertical"
category = { 'homepage' }
isDraggable = { false }
showTitlesAsTooltip = { false }
/ >
) }
< / div >
< / div >
2023-08-28 01:28:05 +00:00
}
/ >
) ;
} ;