-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathWidgetAreaRenderer.js
331 lines (299 loc) · 8.98 KB
/
WidgetAreaRenderer.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* WidgetAreaRenderer component.
*
* Site Kit by Google, Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useEffect, useRef, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useDispatch, useSelect } from 'googlesitekit-data';
import { getWidgetLayout, combineWidgets, HIDDEN_CLASS } from '../util';
import { getStickyHeaderHeight } from '../../../util/scroll';
import { CORE_WIDGETS, WIDGET_AREA_STYLES } from '../datastore/constants';
import { CORE_UI, ACTIVE_CONTEXT_ID } from '../../datastore/ui/constants';
import { Cell, Grid, Row } from '../../../material-components';
import {
useBreakpoint,
BREAKPOINT_XLARGE,
BREAKPOINT_DESKTOP,
BREAKPOINT_TABLET,
BREAKPOINT_SMALL,
} from '../../../hooks/useBreakpoint';
import ErrorHandler from '../../../components/ErrorHandler';
import InViewProvider from '../../../components/InViewProvider';
import WidgetRenderer from './WidgetRenderer';
import WidgetCellWrapper from './WidgetCellWrapper';
import useViewOnly from '../../../hooks/useViewOnly';
import { CORE_USER } from '../../datastore/user/constants';
import useLatestIntersection from '../../../hooks/useLatestIntersection';
import NewBadge from '../../../components/NewBadge';
import { WEEK_IN_SECONDS } from '../../../util';
/**
* Gets root margin value for the intersection hook.
*
* @since 1.69.0
*
* @param {string} breakpoint The current breakpoint.
* @return {string} The root margin.
*/
function getRootMargin( breakpoint ) {
const gridGaps = {
[ BREAKPOINT_XLARGE ]: 48,
[ BREAKPOINT_DESKTOP ]: 48,
[ BREAKPOINT_TABLET ]: 32,
[ BREAKPOINT_SMALL ]: 32,
};
const gap = gridGaps[ breakpoint ];
const top = Math.abs( getStickyHeaderHeight( breakpoint ) + gap );
return `${ -top }px ${ -gap }px ${ -gap }px ${ -gap }px`;
}
export default function WidgetAreaRenderer( { slug, contextID } ) {
const viewOnlyDashboard = useViewOnly();
const viewableModules = useSelect( ( select ) => {
if ( ! viewOnlyDashboard ) {
return null;
}
return select( CORE_USER ).getViewableModules();
} );
const breakpoint = useBreakpoint();
const widgetAreaRef = useRef();
const intersectionEntry = useLatestIntersection( widgetAreaRef, {
rootMargin: getRootMargin( breakpoint ),
threshold: 0, // Trigger "in-view" as soon as one pixel is visible.
} );
const widgetArea = useSelect( ( select ) =>
select( CORE_WIDGETS ).getWidgetArea( slug )
);
const { Icon, title, style, subtitle, hasNewBadge, CTA, Footer } =
widgetArea;
const widgets = useSelect( ( select ) =>
select( CORE_WIDGETS ).getWidgets( slug, {
modules: viewableModules ? viewableModules : undefined,
} )
);
const widgetStates = useSelect( ( select ) =>
select( CORE_WIDGETS ).getWidgetStates()
);
const isActive = useSelect( ( select ) =>
select( CORE_WIDGETS ).isWidgetAreaActive( slug, {
modules: viewableModules ? viewableModules : undefined,
} )
);
const activeContextID = useSelect( ( select ) =>
select( CORE_UI ).getValue( ACTIVE_CONTEXT_ID )
);
const [ inViewState, setInViewState ] = useState( {
key: `WidgetAreaRenderer-${ slug }`,
value: activeContextID
? activeContextID === contextID
: !! intersectionEntry?.intersectionRatio,
} );
useEffect( () => {
setInViewState( {
key: `WidgetAreaRenderer-${ slug }`,
value: activeContextID
? activeContextID === contextID
: !! intersectionEntry?.intersectionRatio,
} );
}, [ intersectionEntry, slug, activeContextID, contextID ] );
// NewBadge Expirable Item
const expirableBadgeSlug = `widget-area-expirable-new-badge-${ slug }`;
const hasBadgeBeenSeen = useSelect( ( select ) =>
select( CORE_USER ).hasExpirableItem( expirableBadgeSlug )
);
const isExpiredBadgeActive = useSelect( ( select ) =>
select( CORE_USER ).isExpirableItemActive( expirableBadgeSlug )
);
// Show the new badge if this widget area allows new badges, it's new badge
// has not been seen yet, or the badge has been seen and is still active.
const showNewBadge =
hasNewBadge && ( hasBadgeBeenSeen === false || isExpiredBadgeActive );
const { setExpirableItemTimers } = useDispatch( CORE_USER );
useEffect( () => {
// Wait until the selectors have resolved.
if (
hasBadgeBeenSeen !== undefined &&
isExpiredBadgeActive !== undefined
) {
// Only set the expirable item if the badge is new and the user is viewing it for the first time.
if ( hasNewBadge && ! hasBadgeBeenSeen ) {
setExpirableItemTimers( [
{
slug: expirableBadgeSlug,
expiresInSeconds: WEEK_IN_SECONDS * 4,
},
] );
}
}
}, [
hasNewBadge,
expirableBadgeSlug,
hasBadgeBeenSeen,
isExpiredBadgeActive,
setExpirableItemTimers,
] );
if ( viewableModules === undefined ) {
return null;
}
// Compute the layout.
const { columnWidths, rowIndexes } = getWidgetLayout(
widgets,
widgetStates
);
// Combine widgets with similar CTAs and prepare final props to pass to
// `WidgetRenderer` below. Only one consecutive instance of a similar CTA
// will be maintained (via an "override component"), and all other similar
// ones will receive a CSS class to hide them.
// A combined CTA will span the combined width of all widgets that it was
// combined from.
const { gridColumnWidths, overrideComponents } = combineWidgets(
widgets,
widgetStates,
{
columnWidths,
rowIndexes,
}
);
// Render all widgets.
const widgetsOutput = widgets.map( ( widget, i ) => (
<WidgetCellWrapper
key={ `${ widget.slug }-wrapper` }
gridColumnWidth={ gridColumnWidths[ i ] }
>
<ErrorHandler>
<WidgetRenderer
OverrideComponent={
overrideComponents[ i ]
? () => {
const { Component, metadata } =
overrideComponents[ i ];
return <Component { ...metadata } />;
}
: undefined
}
slug={ widget.slug }
/>
</ErrorHandler>
</WidgetCellWrapper>
) );
return (
<InViewProvider value={ inViewState }>
{ !! isActive && (
<Grid
className={ classnames(
'googlesitekit-widget-area',
`googlesitekit-widget-area--${ slug }`,
`googlesitekit-widget-area--${ style }`
) }
ref={ widgetAreaRef }
>
<Row>
<Cell
className="googlesitekit-widget-area-header"
size={ 12 }
>
{ Icon && <Icon width={ 33 } height={ 33 } /> }
{ title && (
<h3 className="googlesitekit-widget-area-header__title googlesitekit-heading-3">
{ title }
{ showNewBadge && <NewBadge /> }
</h3>
) }
{ ( subtitle || CTA ) && (
<div className="googlesitekit-widget-area-header__details">
{ subtitle && (
<h4 className="googlesitekit-widget-area-header__subtitle">
{ subtitle }
{ showNewBadge && ! title && (
<NewBadge />
) }
</h4>
) }
{ CTA && (
<div className="googlesitekit-widget-area-header__cta">
<CTA />
</div>
) }
</div>
) }
</Cell>
</Row>
<div className="googlesitekit-widget-area-widgets">
<Row>
{ style === WIDGET_AREA_STYLES.BOXES &&
widgetsOutput }
{ style === WIDGET_AREA_STYLES.COMPOSITE && (
<Cell size={ 12 }>
<Grid>
<Row>{ widgetsOutput }</Row>
</Grid>
</Cell>
) }
</Row>
</div>
{ Footer && (
<Row>
<Cell
className="googlesitekit-widget-area-footer"
size={ 12 }
>
<Footer />
</Cell>
</Row>
) }
</Grid>
) }
{
// Here we render the bare output as it is guaranteed to render empty.
// This is important compared to returning `null` so that the area
// can maybe render later if conditions change for widgets to become
// active.
//
// Returning `null` here however would have the side-effect of making
// all widgets active again, which is why we must return the "null"
// output.
}
{ ! isActive && (
<Grid
className={ classnames(
HIDDEN_CLASS,
'googlesitekit-widget-area',
{
[ `googlesitekit-widget-area--${ slug }` ]: !! slug,
[ `googlesitekit-widget-area--${ style }` ]:
!! style,
}
) }
ref={ widgetAreaRef }
>
{ widgetsOutput }
</Grid>
) }
</InViewProvider>
);
}
WidgetAreaRenderer.propTypes = {
slug: PropTypes.string.isRequired,
contextID: PropTypes.string,
};