@@ -79,6 +79,8 @@ type OptimizelyComponentProps = {
7979 /** Display template tag (if any) */
8080 __tag ?: string ;
8181
82+ displayTemplateKey ?: string | null ;
83+
8284 /** Preview context */
8385 __context ?: { edit : boolean ; preview_token : string } ;
8486
@@ -96,10 +98,9 @@ export async function OptimizelyComponent({
9698 if ( ! componentRegistry ) {
9799 throw new Error ( 'You should call `initReactComponentRegistry` first' ) ;
98100 }
99-
101+ const dtKey = opti . composition ?. displayTemplateKey ?? opti . displayTemplateKey ;
100102 const Component = await componentRegistry . getComponent ( opti . __typename , {
101- tag :
102- opti . __tag ?? getDisplayTemplateTag ( opti . composition ?. displayTemplateKey ) ,
103+ tag : opti . __tag ?? getDisplayTemplateTag ( dtKey ) ,
103104 } ) ;
104105
105106 if ( ! Component ) {
@@ -230,46 +231,60 @@ function FallbackComponent({ children }: { children: ReactNode }) {
230231}
231232
232233type OptimizelyGridSectionProps = {
233- nodes ? : ExperienceNode [ ] ;
234+ nodes : ExperienceNode [ ] ;
234235 row ?: StructureContainer ;
235236 column ?: StructureContainer ;
236237 displaySettings ?: DisplaySettingsType [ ] ;
237238} ;
238239
240+ const fallbacks : Record < string , StructureContainer > = {
241+ row : FallbackRow ,
242+ column : FallbackColumn ,
243+ } ;
244+
239245export function OptimizelyGridSection ( {
240246 nodes,
241- row = FallbackRow ,
242- column = FallbackColumn ,
247+ row,
248+ column,
243249} : OptimizelyGridSectionProps ) {
244- if ( ! nodes ) {
245- // TODO: Handle beter
246- throw new Error ( 'Nodes must be an array' ) ;
247- }
250+ const locallyDefined : Record < string , StructureContainer | undefined > = {
251+ row,
252+ column,
253+ } ;
254+
248255 return nodes . map ( ( node , i ) => {
249- // get component key(tag) from the display template
250- const key = getDisplayTemplateTag ( node . displayTemplateKey ) ;
251- // get the parsed display settings (stlyes, classes etc.)
256+ const tag = getDisplayTemplateTag ( node . displayTemplateKey ) ;
252257 const parsedDisplaySettings = parseDisplaySettings ( node . displaySettings ) ;
253258
254259 if ( isComponentNode ( node ) ) {
255260 return (
256261 < OptimizelyComponent
257262 opti = { {
258263 ...node . component ,
259- __tag : key ,
264+ __tag : tag ,
260265 } }
261266 key = { node . key }
262267 displaySettings = { parsedDisplaySettings }
263268 />
264269 ) ;
265270 }
266271
267- const { nodes, nodeType } = node ;
268-
269- const mapper : Record < string , StructureContainer > = { row, column } ;
270-
271- // TODO: default component
272- const Component = mapper [ nodeType ] ?? React . Fragment ;
272+ const { nodeType } = node ;
273+ const globalNames : Record < string , string > = {
274+ row : '_Row' ,
275+ column : '_Column' ,
276+ } ;
277+
278+ // Pick the component in the following order:
279+ // 1. Explicitly defined in this component
280+ // 2. Globally defined (in the registry)
281+ // 3. Fallback
282+ // 4. React.Fragment
283+ const Component =
284+ locallyDefined [ nodeType ] ??
285+ componentRegistry . getComponent ( globalNames [ nodeType ] , { tag } ) ??
286+ fallbacks [ nodeType ] ??
287+ React . Fragment ;
273288
274289 return (
275290 < Component
@@ -278,7 +293,11 @@ export function OptimizelyGridSection({
278293 key = { node . key }
279294 displaySettings = { parsedDisplaySettings }
280295 >
281- < OptimizelyGridSection row = { row } column = { column } nodes = { nodes } />
296+ < OptimizelyGridSection
297+ row = { row }
298+ column = { column }
299+ nodes = { node . nodes ?? [ ] }
300+ />
282301 </ Component >
283302 ) ;
284303 } ) ;
0 commit comments