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 ,
2023-10-02 08:28:13 +00:00
useEffect ,
2024-02-14 08:07:55 +00:00
useContext ,
2023-09-19 05:54:19 +00:00
} 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-10-02 08:28:13 +00:00
import { useSelectedPattern } from '../hooks/use-selected-pattern' ;
2023-10-26 10:15:30 +00:00
import { useEditorScroll } from '../hooks/use-editor-scroll' ;
2024-02-14 08:07:55 +00:00
import { FlowType } from '~/customize-store/types' ;
import { CustomizeStoreContext } from '~/customize-store/assembler-hub' ;
2023-08-28 01:28:05 +00:00
export const SidebarNavigationScreenHomepage = ( ) = > {
2023-10-26 10:15:30 +00:00
const { scroll } = useEditorScroll ( {
editorSelector : '.woocommerce-customize-store__block-editor iframe' ,
scrollDirection : 'top' ,
} ) ;
2023-09-19 05:54:19 +00:00
const { isLoading , homeTemplates } = useHomeTemplates ( ) ;
2023-10-02 08:28:13 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
const { selectedPattern , setSelectedPattern } = useSelectedPattern ( ) ;
2023-09-19 05:54:19 +00:00
const [ blocks , , onChange ] = useEditorBlocks ( ) ;
const onClickPattern = useCallback (
2023-10-02 08:28:13 +00:00
( pattern , selectedBlocks ) = > {
setSelectedPattern ( pattern ) ;
2023-09-19 05:54:19 +00:00
onChange (
[ blocks [ 0 ] , . . . selectedBlocks , blocks [ blocks . length - 1 ] ] ,
{ selection : { } }
) ;
2023-10-26 10:15:30 +00:00
scroll ( ) ;
2023-09-19 05:54:19 +00:00
} ,
2023-10-26 10:15:30 +00:00
[ blocks , onChange , setSelectedPattern , scroll ]
2023-09-19 05:54:19 +00:00
) ;
const homePatterns = useMemo ( ( ) = > {
return Object . entries ( homeTemplates ) . map (
( [ templateName , patterns ] ) = > {
return {
name : templateName ,
2023-10-02 08:28:13 +00:00
title : templateName ,
2023-09-19 05:54:19 +00:00
blocks : patterns.reduce (
( acc : BlockInstance [ ] , pattern ) = > [
. . . acc ,
. . . pattern . blocks ,
] ,
[ ]
) ,
2023-10-02 08:28:13 +00:00
blockTypes : [ '' ] ,
categories : [ '' ] ,
content : '' ,
source : '' ,
2023-09-19 05:54:19 +00:00
} ;
}
) ;
} , [ homeTemplates ] ) ;
2023-10-02 08:28:13 +00:00
useEffect ( ( ) = > {
if ( selectedPattern || ! blocks . length || ! homePatterns . length ) {
return ;
}
const homeBlocks = blocks . slice ( 1 , - 1 ) ;
const _currentSelectedPattern = homePatterns . find ( ( pattern ) = > {
if ( homeBlocks . length !== pattern . blocks . length ) {
return false ;
}
return homeBlocks . every (
( block , index ) = > block . name === pattern . blocks [ index ] . name
) ;
} ) ;
setSelectedPattern ( _currentSelectedPattern ) ;
2023-10-26 10:15:30 +00:00
2023-10-02 08:28:13 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps -- we don't want to re-run this effect when currentSelectedPattern changes
} , [ blocks , homePatterns ] ) ;
2024-02-14 08:07:55 +00:00
const { context } = useContext ( CustomizeStoreContext ) ;
2024-02-22 08:48:49 +00:00
const aiOnline = context . flowType === FlowType . AIOnline ;
const title = aiOnline
? __ ( 'Change your homepage' , 'woocommerce' )
2024-02-26 09:15:02 +00:00
: __ ( 'Choose your homepage' , 'woocommerce' ) ;
2024-02-22 08:48:49 +00:00
const sidebarMessage = aiOnline
? __ (
'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'
)
: __ (
2024-02-26 09:15:02 +00:00
'Create an engaging homepage by selecting one of our pre-designed layouts. You can continue customizing this page, including the content, later via the <EditorLink>Editor</EditorLink>.' ,
2024-02-22 08:48:49 +00:00
'woocommerce'
) ;
2024-02-14 08:07:55 +00:00
2023-08-28 01:28:05 +00:00
return (
< SidebarNavigationScreen
2024-02-22 08:48:49 +00:00
title = { title }
2024-02-14 08:07:55 +00:00
description = { createInterpolateElement ( sidebarMessage , {
EditorLink : (
< Link
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
) ,
2024-02-14 08:07:55 +00:00
} ) }
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 }
2023-09-25 10:30:31 +00:00
label = { 'Homepage' }
2023-09-19 05:54:19 +00:00
orientation = "vertical"
category = { 'homepage' }
isDraggable = { false }
2023-10-26 10:15:30 +00:00
showTitlesAsTooltip = { false }
2023-09-19 05:54:19 +00:00
/ >
) }
< / div >
< / div >
2023-08-28 01:28:05 +00:00
}
/ >
) ;
} ;