2023-09-18 09:29:29 +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-18 09:29:29 +00:00
import {
createInterpolateElement ,
useCallback ,
useContext ,
useEffect ,
useMemo ,
} 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-18 09:29:29 +00:00
import { Spinner } from '@wordpress/components' ;
// @ts-expect-error Missing type in core-data.
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-18 09:29:29 +00:00
import { useEditorBlocks } from '../hooks/use-editor-blocks' ;
import { usePatternsByCategory } from '../hooks/use-patterns' ;
import { HighlightedBlockContext } from '../context/highlighted-block-context' ;
import { useEditorScroll } from '../hooks/use-editor-scroll' ;
const SUPPORTED_FOOTER_PATTERNS = [
'woocommerce-blocks/footer-simple-menu-and-cart' ,
'woocommerce-blocks/footer-with-3-menus' ,
'woocommerce-blocks/footer-large' ,
] ;
2023-08-28 01:28:05 +00:00
export const SidebarNavigationScreenFooter = ( ) = > {
2023-09-18 09:29:29 +00:00
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 : 'bottom' ,
} ) ;
const { isLoading , patterns } = usePatternsByCategory ( 'woo-commerce' ) ;
const [ blocks , , onChange ] = useEditorBlocks ( ) ;
const { setHighlightedBlockIndex , resetHighlightedBlockIndex } = useContext (
HighlightedBlockContext
) ;
useEffect ( ( ) = > {
if ( blocks && blocks . length ) {
setHighlightedBlockIndex ( blocks . length - 1 ) ;
}
} , [ setHighlightedBlockIndex , blocks ] ) ;
const footerPatterns = useMemo (
( ) = >
patterns
. filter ( ( pattern ) = >
SUPPORTED_FOOTER_PATTERNS . includes ( pattern . name )
)
. sort (
( a , b ) = >
SUPPORTED_FOOTER_PATTERNS . indexOf ( a . name ) -
SUPPORTED_FOOTER_PATTERNS . indexOf ( b . name )
) ,
[ patterns ]
) ;
const onClickFooterPattern = useCallback (
( _pattern , selectedBlocks ) = > {
onChange ( [ . . . blocks . slice ( 0 , - 1 ) , selectedBlocks [ 0 ] ] , {
selection : { } ,
} ) ;
} ,
[ blocks , onChange ]
) ;
2023-08-28 01:28:05 +00:00
return (
< SidebarNavigationScreen
title = { __ ( 'Change your footer' , 'woocommerce' ) }
2023-09-18 09:29:29 +00:00
onNavigateBackClick = { resetHighlightedBlockIndex }
2023-08-28 01:28:05 +00:00
description = { createInterpolateElement (
__ (
2023-09-18 09:29:29 +00:00
"Select a new footer from the options below. Your footer includes your site's secondary navigation and will be added to every page. You can continue customizing this via the <EditorLink>Editor</EditorLink>." ,
2023-08-28 01:28:05 +00:00
'woocommerce'
) ,
{
EditorLink : (
< Link
2023-09-19 03:37:46 +00:00
onClick = { ( ) = > {
recordEvent (
'customize_your_store_assembler_hub_editor_link_click' ,
{
source : 'footer' ,
}
) ;
window . open (
` ${ ADMIN_URL } site-editor.php ` ,
'_blank'
) ;
return false ;
} }
href = ""
2023-08-28 01:28:05 +00:00
/ >
) ,
}
) }
content = {
< >
2023-09-18 09:29:29 +00:00
< div className = "edit-site-sidebar-navigation-screen-patterns__group-footer" >
{ isLoading && (
< span className = "components-placeholder__preview" >
< Spinner / >
< / span >
) }
{ ! isLoading && (
< BlockPatternList
shownPatterns = { footerPatterns }
blockPatterns = { footerPatterns }
onClickPattern = { onClickFooterPattern }
label = { 'Footers' }
orientation = "vertical"
category = { 'footer' }
isDraggable = { false }
showTitlesAsTooltip = { true }
/ >
) }
< / div >
2023-08-28 01:28:05 +00:00
< / >
}
/ >
) ;
} ;