/* eslint-disable @woocommerce/dependency-group */ /* eslint-disable @typescript-eslint/ban-ts-comment */ /** * External dependencies */ import { __ } from '@wordpress/i18n'; import { useCallback, createInterpolateElement, useContext, useEffect, useMemo, } from '@wordpress/element'; import { Link } from '@woocommerce/components'; import { recordEvent } from '@woocommerce/tracks'; import { Spinner } from '@wordpress/components'; // @ts-ignore No types for this exist yet. import { __experimentalBlockPatternsList as BlockPatternList } from '@wordpress/block-editor'; /** * Internal dependencies */ import { SidebarNavigationScreen } from './sidebar-navigation-screen'; import { ADMIN_URL } from '~/utils/admin-settings'; import { usePatternsByCategory } from '../hooks/use-patterns'; import { useSelectedPattern } from '../hooks/use-selected-pattern'; import { useEditorBlocks } from '../hooks/use-editor-blocks'; import { HighlightedBlockContext } from '../context/highlighted-block-context'; import { useEditorScroll } from '../hooks/use-editor-scroll'; import { findPatternByBlock } from './utils'; const SUPPORTED_HEADER_PATTERNS = [ 'woocommerce-blocks/header-essential', 'woocommerce-blocks/header-minimal', 'woocommerce-blocks/header-large', 'woocommerce-blocks/header-centered-menu-with-search', ]; export const SidebarNavigationScreenHeader = () => { const { scroll } = useEditorScroll( { editorSelector: '.woocommerce-customize-store__block-editor iframe', scrollDirection: 'top', } ); const { isLoading, patterns } = usePatternsByCategory( 'woo-commerce' ); const [ blocks, , onChange ] = useEditorBlocks(); const { setHighlightedBlockIndex, resetHighlightedBlockIndex } = useContext( HighlightedBlockContext ); // eslint-disable-next-line react-hooks/exhaustive-deps const { selectedPattern, setSelectedPattern } = useSelectedPattern(); useEffect( () => { setHighlightedBlockIndex( 0 ); }, [ setHighlightedBlockIndex ] ); 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 ] ); useEffect( () => { if ( selectedPattern || ! blocks.length || ! headerPatterns.length ) { return; } const currentSelectedPattern = findPatternByBlock( headerPatterns, blocks[ 0 ] ); setSelectedPattern( currentSelectedPattern ); // eslint-disable-next-line react-hooks/exhaustive-deps -- we don't want to re-run this effect when currentSelectedPattern changes }, [ blocks, headerPatterns ] ); const onClickHeaderPattern = useCallback( ( pattern, selectedBlocks ) => { setSelectedPattern( pattern ); onChange( [ selectedBlocks[ 0 ], ...blocks.slice( 1 ) ], { selection: {}, } ); scroll(); }, [ blocks, onChange, setSelectedPattern, scroll ] ); return ( Editor.", 'woocommerce' ), { EditorLink: ( { recordEvent( 'customize_your_store_assembler_hub_editor_link_click', { source: 'header', } ); window.open( `${ ADMIN_URL }site-editor.php`, '_blank' ); return false; } } href="" /> ), } ) } content={ <>
{ isLoading && ( ) } { ! isLoading && ( ) }
} /> ); };