@amplifyup/sdk 0.1.23 → 0.1.24

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.
package/dist/react.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use client';
2
- import { createContext, useState, useEffect, useContext, Fragment as Fragment$1 } from 'react';
2
+ import { createContext, useState, useEffect, useContext, Fragment as Fragment$1, Component } from 'react';
3
3
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
4
+ import { marked } from 'marked';
4
5
 
5
6
  /* @amplifyup/sdk - https://amplifyup.ai */
6
7
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -2659,6 +2660,53 @@ function getSDK(adapter) {
2659
2660
  var trackContentView = (contentId, contentType, properties) => getSDK().trackContentView(contentId, contentType, properties);
2660
2661
  var refreshEventTracking = () => getSDK().refreshEventTracking();
2661
2662
  getSDK();
2663
+ var ComponentRenderBoundary = class extends Component {
2664
+ constructor() {
2665
+ super(...arguments);
2666
+ this.state = { error: null };
2667
+ }
2668
+ static getDerivedStateFromError(error) {
2669
+ return { error };
2670
+ }
2671
+ componentDidCatch(error) {
2672
+ console.error(
2673
+ `[AmplifyRenderer] "${this.props.componentId}" (${this.props.nodeId}) threw while rendering:`,
2674
+ error
2675
+ );
2676
+ }
2677
+ render() {
2678
+ if (this.state.error) {
2679
+ if (process.env.NODE_ENV !== "production") {
2680
+ return /* @__PURE__ */ jsxs(
2681
+ "div",
2682
+ {
2683
+ "data-amplifyup-render-error": "",
2684
+ "data-component-id": this.props.componentId,
2685
+ "data-node-id": this.props.nodeId,
2686
+ style: {
2687
+ margin: "1rem 0",
2688
+ padding: "0.75rem 1rem",
2689
+ border: "1px solid #fca5a5",
2690
+ borderRadius: "0.5rem",
2691
+ background: "#fef2f2",
2692
+ color: "#991b1b",
2693
+ fontSize: "0.875rem"
2694
+ },
2695
+ children: [
2696
+ 'Component "',
2697
+ this.props.componentId,
2698
+ '" failed to render:',
2699
+ " ",
2700
+ this.state.error.message
2701
+ ]
2702
+ }
2703
+ );
2704
+ }
2705
+ return null;
2706
+ }
2707
+ return this.props.children;
2708
+ }
2709
+ };
2662
2710
  function AmplifyRenderer({
2663
2711
  renderComponent,
2664
2712
  pageConfig: serverPageConfig,
@@ -3015,11 +3063,20 @@ function AmplifyRenderer({
3015
3063
  const slotsToPass = Object.keys(slotChildren).length > 0 ? slotChildren : void 0;
3016
3064
  console.log(`[AmplifyRenderer] Calling renderComponent for ${node.componentId}`, {
3017
3065
  hasSlots: !!slotsToPass,
3018
- slotKeys: slotsToPass ? Object.keys(slotsToPass) : []
3066
+ slotKeys: slotsToPass ? Object.keys(slotsToPass) : [],
3067
+ propKeys: Object.keys(node.props || {}),
3068
+ props: node.props || {}
3019
3069
  });
3020
- return /* @__PURE__ */ jsx("div", { "data-node-id": node.id, "data-amplifyup-node": "", children: renderComponent(node.componentId, node.props || {}, slotsToPass, {
3021
- layoutNodeId: node.id
3022
- }) });
3070
+ return /* @__PURE__ */ jsx("div", { "data-node-id": node.id, "data-amplifyup-node": "", children: /* @__PURE__ */ jsx(
3071
+ ComponentRenderBoundary,
3072
+ {
3073
+ componentId: node.componentId,
3074
+ nodeId: node.id,
3075
+ children: renderComponent(node.componentId, node.props || {}, slotsToPass, {
3076
+ layoutNodeId: node.id
3077
+ })
3078
+ }
3079
+ ) });
3023
3080
  };
3024
3081
  const rawTree = pageConfig.layoutTree;
3025
3082
  const layoutTree = Array.isArray(rawTree) ? rawTree : rawTree && typeof rawTree === "object" && rawTree.componentId ? [rawTree] : [];
@@ -3265,6 +3322,25 @@ function Slot({
3265
3322
  }
3266
3323
  );
3267
3324
  }
3325
+ function formatFieldValue(value) {
3326
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
3327
+ return String(value);
3328
+ }
3329
+ if (Array.isArray(value)) {
3330
+ const parts = value.map((item) => {
3331
+ if (typeof item === "string") return item;
3332
+ if (item && typeof item === "object" && "children" in item && Array.isArray(item.children)) {
3333
+ return item.children.map((c) => typeof c?.text === "string" ? c.text : "").join("");
3334
+ }
3335
+ return "";
3336
+ }).filter(Boolean);
3337
+ if (parts.length > 0) return parts.join(" ");
3338
+ }
3339
+ if (value && typeof value === "object") {
3340
+ return "";
3341
+ }
3342
+ return "";
3343
+ }
3268
3344
  function Field({
3269
3345
  name,
3270
3346
  fallback,
@@ -3283,7 +3359,51 @@ function Field({
3283
3359
  if (render) {
3284
3360
  return /* @__PURE__ */ jsx("span", { className, children: render(fieldValue) });
3285
3361
  }
3286
- return /* @__PURE__ */ jsx("span", { className, children: String(fieldValue) });
3362
+ const text = formatFieldValue(fieldValue);
3363
+ if (!text) {
3364
+ return fallback ? /* @__PURE__ */ jsx("span", { className, children: fallback }) : null;
3365
+ }
3366
+ return /* @__PURE__ */ jsx("span", { className, children: text });
3367
+ }
3368
+ function markdownToHtml(markdown) {
3369
+ try {
3370
+ const result = marked.parse(markdown, { async: false });
3371
+ return typeof result === "string" ? result : "";
3372
+ } catch {
3373
+ return markdown;
3374
+ }
3375
+ }
3376
+ function RichText({
3377
+ name,
3378
+ fallback,
3379
+ className,
3380
+ as: Tag = "div"
3381
+ }) {
3382
+ const context = useContext(ComponentContext);
3383
+ if (!context) {
3384
+ console.warn(`[AmplifyUp SDK] RichText "${name}" used outside ComponentContextProvider`);
3385
+ return fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
3386
+ }
3387
+ const fieldValue = context.props?.[name];
3388
+ if (fieldValue === void 0 || fieldValue === null || fieldValue === "") {
3389
+ return fallback ? /* @__PURE__ */ jsx(Tag, { className, children: fallback }) : null;
3390
+ }
3391
+ let html = "";
3392
+ if (typeof fieldValue === "string") {
3393
+ const trimmed = fieldValue.trim();
3394
+ if (trimmed.startsWith("<") && trimmed.includes(">")) {
3395
+ html = fieldValue;
3396
+ } else {
3397
+ html = markdownToHtml(fieldValue);
3398
+ }
3399
+ } else {
3400
+ const text = formatFieldValue(fieldValue);
3401
+ html = text ? markdownToHtml(text) : "";
3402
+ }
3403
+ if (!html) {
3404
+ return fallback ? /* @__PURE__ */ jsx(Tag, { className, children: fallback }) : null;
3405
+ }
3406
+ return /* @__PURE__ */ jsx(Tag, { className, dangerouslySetInnerHTML: { __html: html } });
3287
3407
  }
3288
3408
  var usePathname = null;
3289
3409
  if (typeof window !== "undefined") {
@@ -3442,6 +3562,6 @@ function useAmplifyUp() {
3442
3562
  return context;
3443
3563
  }
3444
3564
 
3445
- export { AmplifyPageContent, AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Slot, fetchPageConfigServer, isComposerPreview, isPreviewRequest, useAmplifyUp };
3565
+ export { AmplifyPageContent, AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, RichText, Slot, fetchPageConfigServer, isComposerPreview, isPreviewRequest, useAmplifyUp };
3446
3566
  //# sourceMappingURL=react.mjs.map
3447
3567
  //# sourceMappingURL=react.mjs.map