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-14 08:24:46 +00:00
import { Spinner } from '@wordpress/components' ;
// @ts-ignore No types for this exist yet.
import { __experimentalBlockPatternsList as BlockPatternList } from '@wordpress/block-editor' ;
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' ;
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-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' ,
'woocommerce-blocks/header-centered-menu-with-search' ,
2023-09-14 08:24:46 +00:00
] ;
2023-08-28 01:28:05 +00:00
export const SidebarNavigationScreenHeader = ( ) = > {
2023-09-18 09:29:29 +00:00
useEditorScroll ( {
editorSelector :
'.interface-navigable-region.interface-interface-skeleton__content' ,
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
) ;
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
) ;
const onClickHeaderPattern = useCallback (
( _pattern , selectedBlocks ) = > {
onChange ( [ selectedBlocks [ 0 ] , . . . blocks . slice ( 1 ) ] , {
selection : { } ,
} ) ;
} ,
[ blocks , onChange ]
) ;
2023-08-28 01:28:05 +00:00
return (
< SidebarNavigationScreen
title = { __ ( 'Change your header' , 'woocommerce' ) }
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-11 09:48:23 +00:00
href = { ` ${ ADMIN_URL } site-editor.php ` }
2023-08-28 01:28:05 +00:00
type = "external"
/ >
) ,
}
) }
content = {
< >
2023-09-14 08:24:46 +00:00
< div className = "edit-site-sidebar-navigation-screen-patterns__group-header" >
{ isLoading && (
< span className = "components-placeholder__preview" >
< Spinner / >
< / span >
) }
{ ! isLoading && (
< BlockPatternList
shownPatterns = { headerPatterns }
blockPatterns = { headerPatterns }
onClickPattern = { onClickHeaderPattern }
label = { 'Headers' }
orientation = "vertical"
category = { 'header' }
isDraggable = { false }
showTitlesAsTooltip = { true }
/ >
) }
< / div >
2023-08-28 01:28:05 +00:00
< / >
}
/ >
) ;
} ;