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 {
useCallback ,
useContext ,
useEffect ,
useMemo ,
} from '@wordpress/element' ;
import { Spinner } from '@wordpress/components' ;
2024-02-29 14:32:44 +00:00
// @ts-expect-error No types for this exist yet.
import { store as coreStore } from '@wordpress/core-data' ;
2023-09-18 09:29:29 +00:00
2023-08-28 01:28:05 +00:00
/ * *
* Internal dependencies
* /
2024-07-19 12:52:26 +00:00
import { SidebarNavigationScreen } from '../sidebar-navigation-screen' ;
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' ;
import { useSelectedPattern } from '../../hooks/use-selected-pattern' ;
import { findPatternByBlock } from '../utils' ;
import {
__experimentalBlockPatternsList as BlockPatternList ,
// @ts-expect-error No types for this exist yet.
} from '@wordpress/block-editor' ;
2024-02-22 08:48:49 +00:00
import { CustomizeStoreContext } from '~/customize-store/assembler-hub' ;
import { FlowType } from '~/customize-store/types' ;
2024-02-29 14:32:44 +00:00
import { footerTemplateId } from '~/customize-store/data/homepageTemplates' ;
import { useSelect } from '@wordpress/data' ;
2023-09-18 09:29:29 +00:00
2024-07-19 12:52:26 +00:00
import './style.scss' ;
2023-09-18 09:29:29 +00:00
const SUPPORTED_FOOTER_PATTERNS = [
'woocommerce-blocks/footer-with-3-menus' ,
2024-03-08 11:03:19 +00:00
'woocommerce-blocks/footer-simple-menu' ,
2023-09-18 09:29:29 +00:00
'woocommerce-blocks/footer-large' ,
] ;
2023-08-28 01:28:05 +00:00
2024-06-11 13:26:04 +00:00
export const SidebarNavigationScreenFooter = ( {
onNavigateBackClick ,
} : {
onNavigateBackClick : ( ) = > void ;
} ) = > {
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 : 'bottom' ,
} ) ;
const { isLoading , patterns } = usePatternsByCategory ( 'woo-commerce' ) ;
2024-02-29 14:32:44 +00:00
const currentTemplate = useSelect (
( select ) = >
// @ts-expect-error No types for this exist yet.
select ( coreStore ) . __experimentalGetTemplateForLink ( '/' ) ,
[ ]
) ;
const [ mainTemplateBlocks ] = useEditorBlocks (
'wp_template' ,
2024-06-11 13:26:04 +00:00
currentTemplate ? . id ? ? ''
2024-02-29 14:32:44 +00:00
) ;
const [ blocks , , onChange ] = useEditorBlocks (
'wp_template_part' ,
footerTemplateId
2023-09-18 09:29:29 +00:00
) ;
2024-02-29 14:32:44 +00:00
const footerTemplatePartBlockClientId = mainTemplateBlocks . find (
( block ) = > block . attributes . slug === 'footer'
) ;
const { setHighlightedBlockClientId , resetHighlightedBlockClientId } =
useContext ( HighlightedBlockContext ) ;
2023-10-02 08:28:13 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
const { selectedPattern , setSelectedPattern } = useSelectedPattern ( ) ;
2023-09-18 09:29:29 +00:00
useEffect ( ( ) = > {
2024-02-29 14:32:44 +00:00
setHighlightedBlockClientId (
footerTemplatePartBlockClientId ? . clientId ? ? null
) ;
} , [
footerTemplatePartBlockClientId ? . clientId ,
setHighlightedBlockClientId ,
] ) ;
2023-09-18 09:29:29 +00:00
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 ]
) ;
2023-10-02 08:28:13 +00:00
useEffect ( ( ) = > {
// Set the selected pattern when the footer screen is loaded.
if ( selectedPattern || ! blocks . length || ! footerPatterns . length ) {
return ;
}
const currentSelectedPattern = findPatternByBlock (
footerPatterns ,
blocks [ blocks . length - 1 ]
) ;
setSelectedPattern ( currentSelectedPattern ) ;
// eslint-disable-next-line react-hooks/exhaustive-deps -- we don't want to re-run this effect when currentSelectedPattern changes
} , [ blocks , footerPatterns ] ) ;
2023-09-18 09:29:29 +00:00
const onClickFooterPattern = useCallback (
2023-10-02 08:28:13 +00:00
( pattern , selectedBlocks ) = > {
setSelectedPattern ( pattern ) ;
2023-09-18 09:29:29 +00:00
onChange ( [ . . . blocks . slice ( 0 , - 1 ) , selectedBlocks [ 0 ] ] , {
selection : { } ,
} ) ;
2023-10-26 10:15:30 +00:00
scroll ( ) ;
2023-09-18 09:29:29 +00:00
} ,
2023-10-26 10:15:30 +00:00
[ blocks , onChange , setSelectedPattern , scroll ]
2023-09-18 09:29:29 +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 footer' , 'woocommerce' )
: __ ( 'Choose your footer' , 'woocommerce' ) ;
2024-02-26 09:15:02 +00:00
const description = aiOnline
? __ (
2024-08-07 14:16:42 +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 Editor." ,
2024-02-26 09:15:02 +00:00
'woocommerce'
)
: __ (
2024-08-07 14:16:42 +00:00
"Select a 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 Editor later." ,
2024-02-26 09:15:02 +00:00
'woocommerce'
) ;
2023-08-28 01:28:05 +00:00
return (
< SidebarNavigationScreen
2024-02-22 08:48:49 +00:00
title = { title }
2024-06-11 13:26:04 +00:00
onNavigateBackClick = { ( ) = > {
resetHighlightedBlockClientId ( ) ;
onNavigateBackClick ( ) ;
} }
2024-08-07 14:16:42 +00:00
description = { description }
2023-08-28 01:28:05 +00:00
content = {
< >
2023-10-02 08:28:13 +00:00
< div className = "woocommerce-customize-store__sidebar-footer-content" >
2023-09-18 09:29:29 +00:00
{ isLoading && (
< span className = "components-placeholder__preview" >
< Spinner / >
< / span >
) }
{ ! isLoading && (
< BlockPatternList
shownPatterns = { footerPatterns }
blockPatterns = { footerPatterns }
onClickPattern = { onClickFooterPattern }
label = { 'Footers' }
orientation = "vertical"
isDraggable = { false }
2023-11-01 11:03:04 +00:00
onHover = { ( ) = > { } }
2023-09-18 09:29:29 +00:00
showTitlesAsTooltip = { true }
/ >
) }
< / div >
2023-08-28 01:28:05 +00:00
< / >
}
/ >
) ;
} ;