@amplifyup/sdk 0.1.5 → 0.1.7

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
@@ -283,6 +283,32 @@ function resolveAmplifyUpUrls(target) {
283
283
  };
284
284
  }
285
285
 
286
+ // src/preview.ts
287
+ function isPreviewRequest(searchParams) {
288
+ if (searchParams instanceof URLSearchParams) {
289
+ return searchParams.get("preview") === "true" || searchParams.get("visualEditor") === "true";
290
+ }
291
+ const preview = searchParams.preview;
292
+ const visualEditor = searchParams.visualEditor;
293
+ return preview === "true" || visualEditor === "true";
294
+ }
295
+ function isComposerPreview() {
296
+ if (typeof window === "undefined") return false;
297
+ if (window.__AMPLIFYUP_PREVIEW_MODE__ === true) return true;
298
+ const params = new URLSearchParams(window.location.search);
299
+ if (isPreviewRequest(params)) return true;
300
+ if (window.self !== window.top && document.referrer) {
301
+ try {
302
+ const refUrl = new URL(document.referrer);
303
+ if (refUrl.hostname === "amplifyup.ai" || refUrl.hostname.endsWith(".amplifyup.ai") || refUrl.hostname === "localhost" && refUrl.port === "5001" || refUrl.pathname.includes("/dashboard/composer") || refUrl.pathname.includes("/dashboard/routes/")) {
304
+ return true;
305
+ }
306
+ } catch {
307
+ }
308
+ }
309
+ return false;
310
+ }
311
+
286
312
  // src/index.ts
287
313
  function readPreviewQueryOverrides() {
288
314
  if (typeof window === "undefined") return {};
@@ -405,6 +431,7 @@ var AmplifyUpSDK = class {
405
431
  */
406
432
  async init(config) {
407
433
  if (this.initialized && this.adapter?.isInitialized()) {
434
+ this.ensureWindowBridge();
408
435
  return;
409
436
  }
410
437
  if (this.initPromise) {
@@ -417,6 +444,32 @@ var AmplifyUpSDK = class {
417
444
  this.initPromise = null;
418
445
  }
419
446
  }
447
+ /** Attach console helpers to window (idempotent; survives Fast Refresh). */
448
+ ensureWindowBridge() {
449
+ if (typeof window === "undefined") return;
450
+ window.AmplifyUp = {
451
+ getInstanceId: () => this.getInstanceId(),
452
+ getUserId: () => this.getUserId(),
453
+ getTrackingId: () => this.getTrackingId(),
454
+ getTarget: () => this.getTarget(),
455
+ getDiagnostics: () => this.getDiagnostics(),
456
+ getStatus: () => {
457
+ console.log(this.getStatus());
458
+ return this.getDiagnostics();
459
+ },
460
+ testTracking: () => this.testTracking(),
461
+ verifyAWSConnection: () => {
462
+ const result = this.verifyAWSConnection();
463
+ console.log("AWS Connection Verification:", result.message);
464
+ console.log("Full details:", result);
465
+ return result;
466
+ },
467
+ isInitialized: () => this.isInitialized(),
468
+ page: (url, properties) => this.page(url, properties),
469
+ track: (eventName, properties) => this.track(eventName, properties),
470
+ identify: (userId, traits) => this.identify(userId, traits)
471
+ };
472
+ }
420
473
  async performInit(config) {
421
474
  if (!this.adapter) {
422
475
  this.adapter = new AWSAdapter();
@@ -455,34 +508,10 @@ var AmplifyUpSDK = class {
455
508
  };
456
509
  await this.adapter.init(adapterConfig);
457
510
  this.initialized = this.adapter.isInitialized();
458
- if (typeof window !== "undefined") {
459
- window.AmplifyUp = {
460
- getInstanceId: () => this.getInstanceId(),
461
- getUserId: () => this.getUserId(),
462
- getTrackingId: () => this.getTrackingId(),
463
- getTarget: () => this.getTarget(),
464
- getDiagnostics: () => this.getDiagnostics(),
465
- getStatus: () => {
466
- console.log(this.getStatus());
467
- return this.getDiagnostics();
468
- },
469
- testTracking: () => this.testTracking(),
470
- verifyAWSConnection: () => {
471
- const result = this.verifyAWSConnection();
472
- console.log("AWS Connection Verification:", result.message);
473
- console.log("Full details:", result);
474
- return result;
475
- },
476
- isInitialized: () => this.isInitialized(),
477
- // Re-export main tracking methods for convenience
478
- page: (url, properties) => this.page(url, properties),
479
- track: (eventName, properties) => this.track(eventName, properties),
480
- identify: (userId, traits) => this.identify(userId, traits)
481
- };
482
- if (this.config?.debug) {
483
- console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
484
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
485
- }
511
+ this.ensureWindowBridge();
512
+ if (this.config?.debug) {
513
+ console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
514
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
486
515
  }
487
516
  this.clearTrackingIntervals();
488
517
  if (this.isPreviewMode()) {
@@ -1493,10 +1522,17 @@ Available Console Commands:
1493
1522
  }
1494
1523
  };
1495
1524
  var sdkInstance = null;
1525
+ if (typeof window !== "undefined" && window.__AMPLIFYUP_SDK__) {
1526
+ sdkInstance = window.__AMPLIFYUP_SDK__;
1527
+ }
1496
1528
  function getSDK(adapter) {
1497
1529
  if (!sdkInstance) {
1498
1530
  sdkInstance = new AmplifyUpSDK(adapter);
1531
+ if (typeof window !== "undefined") {
1532
+ window.__AMPLIFYUP_SDK__ = sdkInstance;
1533
+ }
1499
1534
  }
1535
+ sdkInstance.ensureWindowBridge();
1500
1536
  return sdkInstance;
1501
1537
  }
1502
1538
  var trackContentView = (contentId, contentType, properties) => getSDK().trackContentView(contentId, contentType, properties);
@@ -1799,6 +1835,62 @@ function AmplifyRenderer({
1799
1835
  }
1800
1836
  return /* @__PURE__ */ jsx("main", { children: layoutTree.map((node) => /* @__PURE__ */ jsx("div", { children: renderNode(node) }, node.id)) });
1801
1837
  }
1838
+ function defaultPreviewCanvas() {
1839
+ return /* @__PURE__ */ jsx("main", { className: "min-h-[50vh]", "data-amplifyup-composer-canvas": true });
1840
+ }
1841
+ function hasAmplifyLayout(pageConfig) {
1842
+ return !!(pageConfig && pageConfig.id !== "default" && Array.isArray(pageConfig.layoutTree) && pageConfig.layoutTree.length > 0);
1843
+ }
1844
+ function AmplifyPageContent({
1845
+ fallback,
1846
+ renderComponent,
1847
+ pageConfig: pageConfigProp,
1848
+ previewLoadingComponent,
1849
+ previewEmptyComponent,
1850
+ loadingComponent,
1851
+ emptyComponent,
1852
+ errorComponent
1853
+ }) {
1854
+ const client = useAmplifyUp();
1855
+ const preview = isComposerPreview();
1856
+ const pageConfig = pageConfigProp ?? client.pageConfig;
1857
+ const loading = pageConfigProp ? false : client.loading;
1858
+ const error = pageConfigProp ? null : client.error;
1859
+ const previewCanvas = previewEmptyComponent ?? defaultPreviewCanvas();
1860
+ const previewLoading = previewLoadingComponent ?? previewCanvas;
1861
+ if (preview) {
1862
+ return /* @__PURE__ */ jsx(
1863
+ AmplifyRenderer,
1864
+ {
1865
+ pageConfig: pageConfig ?? void 0,
1866
+ renderComponent,
1867
+ loadingComponent: loadingComponent ?? previewLoading,
1868
+ emptyComponent: emptyComponent ?? previewCanvas,
1869
+ errorComponent: errorComponent ?? (() => previewCanvas)
1870
+ }
1871
+ );
1872
+ }
1873
+ if (loading) {
1874
+ return loadingComponent ? /* @__PURE__ */ jsx(Fragment, { children: loadingComponent }) : null;
1875
+ }
1876
+ if (error) {
1877
+ if (errorComponent) return /* @__PURE__ */ jsx(Fragment, { children: errorComponent(error) });
1878
+ return fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
1879
+ }
1880
+ if (!hasAmplifyLayout(pageConfig)) {
1881
+ return fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
1882
+ }
1883
+ return /* @__PURE__ */ jsx(
1884
+ AmplifyRenderer,
1885
+ {
1886
+ pageConfig: pageConfig ?? void 0,
1887
+ renderComponent,
1888
+ loadingComponent,
1889
+ emptyComponent,
1890
+ errorComponent
1891
+ }
1892
+ );
1893
+ }
1802
1894
 
1803
1895
  // src/server.tsx
1804
1896
  async function fetchPageConfigServer(route, trackingId, preview = false, options) {
@@ -2028,7 +2120,7 @@ function AmplifyUpProvider({ children, config }) {
2028
2120
  return () => {
2029
2121
  cancelled = true;
2030
2122
  };
2031
- }, []);
2123
+ }, [config.trackingId, config.target, config.debug, config.dev]);
2032
2124
  useEffect(() => {
2033
2125
  if (!initialized) return;
2034
2126
  const fetchConfig = async () => {
@@ -2041,24 +2133,7 @@ function AmplifyUpProvider({ children, config }) {
2041
2133
  return;
2042
2134
  }
2043
2135
  const sdk = getSDK();
2044
- const isPreviewContext = (() => {
2045
- if (typeof window === "undefined") return false;
2046
- if (window.__AMPLIFYUP_PREVIEW_MODE__ === true) return true;
2047
- const params = new URLSearchParams(window.location.search);
2048
- if (params.get("preview") === "true" || params.get("visualEditor") === "true") return true;
2049
- if (window.self !== window.top) {
2050
- if (document.referrer) {
2051
- try {
2052
- const refUrl = new URL(document.referrer);
2053
- if (refUrl.hostname === "localhost" && refUrl.port === "5001" || refUrl.pathname.includes("/dashboard/composer") || refUrl.pathname.includes("/dashboard/routes/")) {
2054
- return true;
2055
- }
2056
- } catch {
2057
- }
2058
- }
2059
- }
2060
- return false;
2061
- })();
2136
+ const isPreviewContext = isComposerPreview();
2062
2137
  const page = await sdk.fetchPageConfig(actualRoute, isPreviewContext);
2063
2138
  if (!page) {
2064
2139
  setPageConfig({
@@ -2090,6 +2165,6 @@ function useAmplifyUp() {
2090
2165
  return context;
2091
2166
  }
2092
2167
 
2093
- export { AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Slot, fetchPageConfigServer, useAmplifyUp };
2168
+ export { AmplifyPageContent, AmplifyRenderer, AmplifyUpProvider, ComponentContextProvider, Field, Slot, fetchPageConfigServer, isComposerPreview, isPreviewRequest, useAmplifyUp };
2094
2169
  //# sourceMappingURL=react.mjs.map
2095
2170
  //# sourceMappingURL=react.mjs.map