-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathindex.tsx
More file actions
330 lines (286 loc) · 8.52 KB
/
index.tsx
File metadata and controls
330 lines (286 loc) · 8.52 KB
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
import { mergeDeepRight, reduce } from 'ramda'
import React, { FC, Fragment, Suspense, useMemo } from 'react'
import { Route } from '../../typings/runtime'
import { useTreePath } from '../../utils/treePath'
import { withErrorBoundary } from '../ErrorBoundary'
import FoldableContainer from '../FoldableContainer'
import { LazyImages } from '../LazyImages'
import LazyRender from '../LazyRender'
import Loading from '../Loading'
import LoadingBar from '../LoadingBar'
import NoSSR from '../NoSSR'
import GenericPreview from '../Preview/GenericPreview'
import type { RenderContext } from '../RenderContext'
import { useRuntime } from '../RenderContext'
import ComponentLoader from './ComponentLoader'
// TODO: Export components separately on @vtex/blocks-inspector, so this import can be simplified
const InspectBlockWrapper = React.lazy(
() =>
new Promise<{ default: any }>((resolve) => {
import('@vtex/blocks-inspector').then((BlocksInspector) => {
resolve({ default: BlocksInspector.default.ExtensionPointWrapper })
})
})
)
interface Props {
id: string
key?: string
params?: any
query?: any
preview?: boolean
treePath?: string
blockProps?: object
[key: string]: any
}
function mountTreePath(currentId: string, parentTreePath: string) {
if (parentTreePath === currentId) {
return parentTreePath
}
if (parentTreePath && currentId) {
return `${parentTreePath}/${currentId}`
}
return parentTreePath || currentId
}
export function getChildExtensions(runtime: RenderContext, treePath: string) {
const extension = runtime.extensions && runtime.extensions[treePath]
if (!extension || !extension.blocks) {
return
}
const childBlocks = extension.blocks.filter((block) => {
/* This weird conditional check is for backwards compatibility.
* Blocks that were built prior to https://github.com/vtex/builder-hub/pull/856
* would not have the 'children' property (block.children === undefined).
*/
const isChild =
block.children === undefined ||
block.children === true ||
block.blockRole === 'children'
const isNotSlot = block.blockRole !== 'slot'
return isChild && isNotSlot
})
/** Rudimentary detection of __fold__ blocks. Shouldn't be a problem
* because other types of fold blocks aren't used inside other blocks
* anyway. Could be improved if necessary.
*/
const foldIndex = childBlocks.findIndex((child) =>
child.extensionPointId.includes('_fold_')
)
const childExtensions = childBlocks.map((child, i) => {
const childTreePath = mountTreePath(child.extensionPointId, treePath)
const childExtension = runtime?.extensions[childTreePath]
const childProps = childExtension?.props ?? {}
return (
<ExtensionPoint
key={`around-${treePath}-${i}`}
id={child.extensionPointId}
blockProps={childProps}
treePath={treePath}
/>
)
})
const queryString = runtime?.route?.queryString ?? {}
if (Object.keys(queryString).includes('__siteEditor')) {
return childExtensions
}
if (foldIndex > -1) {
return (
<FoldableContainer foldIndex={foldIndex}>
{childExtensions}
</FoldableContainer>
)
}
return childExtensions
}
function withOuterExtensions(
after: string[],
around: string[],
before: string[],
treePath: string,
props: any,
element: JSX.Element,
// TODO: these args are getting ridiculous, maybe group them in an object
lazyFooter: boolean,
route: Route
) {
if (before.length === 0 && after.length === 0 && around.length === 0) {
return element
}
const beforeElements = before.map((beforeId) => (
<ExtensionPoint
id={beforeId}
key={beforeId}
treePath={treePath}
params={props.params}
query={props.query}
/>
))
const afterElements = after.map((afterId) => {
const extension = (
<ExtensionPoint
id={afterId}
key={afterId}
treePath={treePath}
params={props.params}
query={props.query}
/>
)
const shouldLazyRender =
lazyFooter &&
afterId === '$after_footer' &&
!route?.path.includes('__siteEditor')
return shouldLazyRender ? (
<LazyRender key={afterId}>{extension}</LazyRender>
) : (
extension
)
})
const isRootTreePath = treePath.indexOf('/') === -1
const wrapped = (
<Fragment key={`wrapped-${treePath}`}>
<LazyImages>{beforeElements}</LazyImages>
{element}
{isRootTreePath && <div className="flex flex-grow-1 bg-below-element" />}
<LazyImages>{afterElements}</LazyImages>
</Fragment>
)
return around.reduce((acc, aroundId) => {
return (
<ExtensionPoint
{...props}
id={aroundId}
key={aroundId}
treePath={treePath}
beforeElements={beforeElements}
afterElements={afterElements}
>
{acc}
</ExtensionPoint>
)
}, wrapped)
}
const ExtensionPoint: FC<Props> = (props) => {
const runtime = useRuntime()
const { inspect, getSettings } = runtime
const treePathFromHook = useTreePath()
const { children, params, query, id, blockProps, ...parentProps } = props
const newTreePath = React.useMemo(
() => mountTreePath(id, props.treePath || treePathFromHook.treePath),
[id, props.treePath, treePathFromHook.treePath]
)
const extension = runtime.extensions && runtime.extensions[newTreePath]
const {
component = null,
after = [],
around = [],
before = [],
content = {},
render: renderStrategy = null,
hydration = 'always',
props: extensionProps = {},
} = extension || {}
const appName = component?.substr(0, component.indexOf('@'))
const appSettings = appName ? getSettings(appName) : {}
const mergedProps = React.useMemo(() => {
return reduce(mergeDeepRight, {} as any, [
appSettings ? { appSettings } : {},
/** Extra props passed to the ExtensionPoint component
* e.g. <ExtensionPoint foo="bar" />
*/
parentProps,
/** Props that are read from runtime.extensions, that come from the blocks files
*/
extensionProps,
/** Props from the blockProps prop, used when the user wants to prevent overriding
* the native ExtensionPoint props (such as `id`)
*/
content,
blockProps || {},
{ params, query },
])
}, [
parentProps,
extensionProps,
blockProps,
content,
params,
query,
appSettings,
])
const componentChildren = useMemo(() => {
const isCompositionChildren =
extension && extension.composition === 'children'
return isCompositionChildren && extension?.blocks
? getChildExtensions(runtime, newTreePath)
: children
}, [children, extension, newTreePath, runtime])
if (
/* Stops rendering if the extension is not found. Useful for optional ExtensionPoints */
!extension
) {
return null
}
const isRootTreePath = newTreePath.indexOf('/') === -1
const componentLoader = (
<ComponentLoader
component={component}
props={mergedProps}
runtime={runtime}
treePath={newTreePath}
hydration={hydration}
>
{component ? (
componentChildren
) : isRootTreePath ? (
<GenericPreview />
) : (
<Loading />
)}
</ComponentLoader>
)
const isLazyFooterEnabled = Boolean(
getSettings('vtex.store')?.enableLazyFooter
)
const extensionPointComponent = withOuterExtensions(
after,
around,
before,
newTreePath,
mergedProps,
componentLoader,
isLazyFooterEnabled,
runtime?.route
)
/**
* "client" component assets are sent to server side rendering,
* but they should display a loading animation.
* "lazy" components might never be used, so they don't necessarily
* need a loading animation.
*/
const maybeClientExtension = (
<Fragment>
{runtime.preview && isRootTreePath && <LoadingBar />}
{renderStrategy === 'client' && !runtime.amp ? (
<NoSSR onSSR={<Loading />}>{extensionPointComponent}</NoSSR>
) : (
extensionPointComponent
)}
</Fragment>
)
/** If it's on inspect mode (?__inspect on querystring) wraps the block
* on a block-inspector wrapper */
if (inspect) {
return (
<Suspense fallback={maybeClientExtension}>
<InspectBlockWrapper extension={extension} treePath={newTreePath}>
{maybeClientExtension}
</InspectBlockWrapper>
</Suspense>
)
}
return maybeClientExtension
}
ExtensionPoint.defaultProps = {
blockProps: {},
treePath: '',
}
export default withErrorBoundary(ExtensionPoint)