-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
sidebar-navigation-screen-footer.tsx
165 lines (151 loc) · 4.88 KB
/
sidebar-navigation-screen-footer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable @woocommerce/dependency-group */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import {
createInterpolateElement,
useCallback,
useContext,
useEffect,
useMemo,
} from '@wordpress/element';
import { Link } from '@woocommerce/components';
import { recordEvent } from '@woocommerce/tracks';
import { Spinner } from '@wordpress/components';
/**
* Internal dependencies
*/
import { SidebarNavigationScreen } from './sidebar-navigation-screen';
import { ADMIN_URL } from '~/utils/admin-settings';
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 BlockPatternList from '../block-pattern-list';
import { CustomizeStoreContext } from '~/customize-store/assembler-hub';
import { FlowType } from '~/customize-store/types';
const SUPPORTED_FOOTER_PATTERNS = [
'woocommerce-blocks/footer-simple-menu',
'woocommerce-blocks/footer-with-3-menus',
'woocommerce-blocks/footer-large',
];
export const SidebarNavigationScreenFooter = () => {
const { scroll } = useEditorScroll( {
editorSelector: '.woocommerce-customize-store__block-editor iframe',
scrollDirection: 'bottom',
} );
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( () => {
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 ]
);
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 ] );
const onClickFooterPattern = useCallback(
( pattern, selectedBlocks ) => {
setSelectedPattern( pattern );
onChange( [ ...blocks.slice( 0, -1 ), selectedBlocks[ 0 ] ], {
selection: {},
} );
scroll();
},
[ blocks, onChange, setSelectedPattern, scroll ]
);
const { context } = useContext( CustomizeStoreContext );
const aiOnline = context.flowType === FlowType.AIOnline;
const title = aiOnline
? __( 'Change your footer', 'woocommerce' )
: __( 'Choose your footer', 'woocommerce' );
const description = aiOnline
? __(
"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>.",
'woocommerce'
)
: __(
"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 <EditorLink>Editor</EditorLink> later.",
'woocommerce'
);
return (
<SidebarNavigationScreen
title={ title }
onNavigateBackClick={ resetHighlightedBlockIndex }
description={ createInterpolateElement( description, {
EditorLink: (
<Link
onClick={ () => {
recordEvent(
'customize_your_store_assembler_hub_editor_link_click',
{
source: 'footer',
}
);
window.open(
`${ ADMIN_URL }site-editor.php`,
'_blank'
);
return false;
} }
href=""
/>
),
} ) }
content={
<>
<div className="woocommerce-customize-store__sidebar-footer-content">
{ isLoading && (
<span className="components-placeholder__preview">
<Spinner />
</span>
) }
{ ! isLoading && (
<BlockPatternList
shownPatterns={ footerPatterns }
blockPatterns={ footerPatterns }
onClickPattern={ onClickFooterPattern }
label={ 'Footers' }
orientation="vertical"
isDraggable={ false }
onHover={ () => {} }
showTitlesAsTooltip={ true }
/>
) }
</div>
</>
}
/>
);
};