@teleporthq/teleport-plugin-common 0.44.15 → 0.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -282,6 +282,7 @@ const trackGlobalRefsInExpression = (expression: string, params: JSXGenerationPa
282
282
  const KNOWN_GLOBAL_REF_IDS = new Set([
283
283
  'locale',
284
284
  'locales',
285
+ 'company',
285
286
  'currentUser',
286
287
  'userIsLoggedIn',
287
288
  'ecommerce',
@@ -358,6 +359,55 @@ const isReactChartjs2Chart = (
358
359
  return REACT_CHARTJS2_CHART_TAGS.has(elementType)
359
360
  }
360
361
 
362
+ /**
363
+ * A chart whose data did not arrive — a query the data source refused, a
364
+ * state still holding an empty fallback — draws an empty chart instead of
365
+ * taking the page down: react-chartjs-2 reads `data.labels` on its first
366
+ * render, and on the server that throw fails the render of the whole page.
367
+ * Rewrites `data={X}` into
368
+ * data={Array.isArray(X?.datasets) ? X : { labels: [], datasets: [] }}
369
+ * A data object written out in place, or hoisted to a constant of the
370
+ * module, needs no guard and is left as is.
371
+ */
372
+ const guardReactChartJs2DataAttr = (
373
+ chartElement: types.JSXElement,
374
+ hoistedConstants: JSXGenerationParams['hoistedConstants']
375
+ ): void => {
376
+ const attr = chartElement.openingElement.attributes.find(
377
+ (a): a is types.JSXAttribute =>
378
+ a.type === 'JSXAttribute' && a.name.type === 'JSXIdentifier' && a.name.name === 'data'
379
+ )
380
+ if (!attr || attr.value?.type !== 'JSXExpressionContainer') {
381
+ return
382
+ }
383
+ const source = attr.value.expression
384
+ const hoisted =
385
+ types.isIdentifier(source) && hoistedConstants.some((constant) => constant.name === source.name)
386
+ if (!types.isExpression(source) || types.isObjectExpression(source) || hoisted) {
387
+ return
388
+ }
389
+ attr.value = types.jsxExpressionContainer(
390
+ types.conditionalExpression(
391
+ types.callExpression(
392
+ types.memberExpression(types.identifier('Array'), types.identifier('isArray')),
393
+ [
394
+ types.optionalMemberExpression(
395
+ types.cloneNode(source),
396
+ types.identifier('datasets'),
397
+ false,
398
+ true
399
+ ),
400
+ ]
401
+ ),
402
+ types.cloneNode(source),
403
+ types.objectExpression([
404
+ types.objectProperty(types.identifier('labels'), types.arrayExpression([])),
405
+ types.objectProperty(types.identifier('datasets'), types.arrayExpression([])),
406
+ ])
407
+ )
408
+ )
409
+ }
410
+
361
411
  /**
362
412
  * Chart.js with maintainAspectRatio:false fills the parent; if height is on the chart
363
413
  * component via className, react-chartjs-2 may not apply it to the canvas host correctly
@@ -1034,6 +1084,7 @@ const generateElementNode: NodeToJSX<UIDLElementNode, types.JSXElement> = (
1034
1084
  }
1035
1085
 
1036
1086
  if (dependency && isReactChartjs2Chart(dependency, originalElementName)) {
1087
+ guardReactChartJs2DataAttr(elementTag, params.hoistedConstants)
1037
1088
  const wrapped = wrapReactChartJs2Element(elementTag, node)
1038
1089
  nodesLookup[key] = wrapped
1039
1090
  return wrapped
@@ -1208,6 +1208,7 @@ export const GLOBAL_REF_ID_MAP: Record<string, string> = {
1208
1208
  'E-commerce': 'ecommerce',
1209
1209
  Cart: 'cart',
1210
1210
  'Current User': 'currentUser',
1211
+ Company: 'company',
1211
1212
  }
1212
1213
 
1213
1214
  export const resolveGlobalRefId = (