2023-09-14 08:24:46 +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-14 08:24:46 +00:00
import {
useCallback ,
createInterpolateElement ,
useContext ,
useEffect ,
2023-09-18 09:29:29 +00:00
useMemo ,
2023-09-14 08:24:46 +00:00
} from '@wordpress/element' ;
2023-08-28 01:28:05 +00:00
import { Link } from '@woocommerce/components' ;
2023-09-19 03:37:46 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2023-09-14 08:24:46 +00:00
import { Spinner } from '@wordpress/components' ;
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-14 08:24:46 +00:00
import { usePatternsByCategory } from '../hooks/use-patterns' ;
2023-10-02 08:28:13 +00:00
import { useSelectedPattern } from '../hooks/use-selected-pattern' ;
2023-09-14 08:24:46 +00:00
import { useEditorBlocks } from '../hooks/use-editor-blocks' ;
import { HighlightedBlockContext } from '../context/highlighted-block-context' ;
2023-09-18 09:29:29 +00:00
import { useEditorScroll } from '../hooks/use-editor-scroll' ;
2023-10-02 08:28:13 +00:00
import { findPatternByBlock } from './utils' ;
2023-11-01 11:03:04 +00:00
import BlockPatternList from '../block-pattern-list' ;
2024-02-22 08:48:49 +00:00
import { CustomizeStoreContext } from '~/customize-store/assembler-hub' ;
import { FlowType } from '~/customize-store/types' ;
2023-09-14 08:24:46 +00:00
const SUPPORTED_HEADER_PATTERNS = [
'woocommerce-blocks/header-essential' ,
'woocommerce-blocks/header-minimal' ,
2023-09-18 09:29:29 +00:00
'woocommerce-blocks/header-large' ,
2023-11-08 03:24:47 +00:00
'woocommerce-blocks/header-centered-menu' ,
2023-09-14 08:24:46 +00:00
] ;
2023-08-28 01:28:05 +00:00
export const SidebarNavigationScreenHeader = ( ) = > {
2023-10-26 10:15:30 +00:00
const { scroll } = useEditorScroll ( {
2023-09-19 05:54:19 +00:00
editorSelector : '.woocommerce-customize-store__block-editor iframe' ,
2023-09-18 09:29:29 +00:00
scrollDirection : 'top' ,
} ) ;
2023-09-14 08:24:46 +00:00
const { isLoading , patterns } = usePatternsByCategory ( 'woo-commerce' ) ;
const [ blocks , , onChange ] = useEditorBlocks ( ) ;
const { setHighlightedBlockIndex , resetHighlightedBlockIndex } = useContext (
HighlightedBlockContext
) ;
2023-10-02 08:28:13 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
const { selectedPattern , setSelectedPattern } = useSelectedPattern ( ) ;
2023-09-14 08:24:46 +00:00
useEffect ( ( ) = > {
setHighlightedBlockIndex ( 0 ) ;
} , [ setHighlightedBlockIndex ] ) ;
2023-09-18 09:29:29 +00:00
const headerPatterns = useMemo (
( ) = >
patterns
. filter ( ( pattern ) = >
SUPPORTED_HEADER_PATTERNS . includes ( pattern . name )
)
. sort (
( a , b ) = >
SUPPORTED_HEADER_PATTERNS . indexOf ( a . name ) -
SUPPORTED_HEADER_PATTERNS . indexOf ( b . name )
) ,
[ patterns ]
2023-09-14 08:24:46 +00:00
) ;
2023-10-02 08:28:13 +00:00
useEffect ( ( ) = > {
if ( selectedPattern || ! blocks . length || ! headerPatterns . length ) {
return ;
}
const currentSelectedPattern = findPatternByBlock (
headerPatterns ,
blocks [ 0 ]
) ;
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 , headerPatterns ] ) ;
2023-09-14 08:24:46 +00:00
const onClickHeaderPattern = useCallback (
2023-10-02 08:28:13 +00:00
( pattern , selectedBlocks ) = > {
setSelectedPattern ( pattern ) ;
2023-09-14 08:24:46 +00:00
onChange ( [ selectedBlocks [ 0 ] , . . . blocks . slice ( 1 ) ] , {
selection : { } ,
} ) ;
2023-10-26 10:15:30 +00:00
scroll ( ) ;
2023-09-14 08:24:46 +00:00
} ,
2023-10-26 10:15:30 +00:00
[ blocks , onChange , setSelectedPattern , scroll ]
2023-09-14 08:24:46 +00:00
) ;
2024-02-22 08:48:49 +00:00
const { context } = useContext ( CustomizeStoreContext ) ;
const aiOnline = context . flowType === FlowType . AIOnline ;
const title = aiOnline
? __ ( 'Change your header' , 'woocommerce' )
: __ ( 'Choose your header' , 'woocommerce' ) ;
2023-08-28 01:28:05 +00:00
return (
< SidebarNavigationScreen
2024-02-22 08:48:49 +00:00
title = { title }
2023-09-14 08:24:46 +00:00
onNavigateBackClick = { resetHighlightedBlockIndex }
2023-08-28 01:28:05 +00:00
description = { createInterpolateElement (
__ (
"Select a new header from the options below. Your header includes your site's navigation and will be added to every page. You can continue customizing this 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 : 'header' ,
}
) ;
window . open (
` ${ ADMIN_URL } site-editor.php ` ,
'_blank'
) ;
return false ;
} }
href = ""
2023-08-28 01:28:05 +00:00
/ >
) ,
}
) }
content = {
< >
2023-10-02 08:28:13 +00:00
< div className = "woocommerce-customize-store__sidebar-header-content" >
2023-09-14 08:24:46 +00:00
{ isLoading && (
< span className = "components-placeholder__preview" >
< Spinner / >
< / span >
) }
{ ! isLoading && (
< BlockPatternList
shownPatterns = { headerPatterns }
blockPatterns = { headerPatterns }
onClickPattern = { onClickHeaderPattern }
label = { 'Headers' }
orientation = "vertical"
isDraggable = { false }
2023-11-01 11:03:04 +00:00
onHover = { ( ) = > { } }
2023-09-14 08:24:46 +00:00
showTitlesAsTooltip = { true }
/ >
) }
< / div >
2023-08-28 01:28:05 +00:00
< / >
}
/ >
) ;
} ;