@amplifyup/sdk 0.1.75 → 0.1.77

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,5 +1,5 @@
1
1
  'use client';
2
- import React3, { createContext, memo, useState, useEffect, Fragment as Fragment$1, useContext, useRef, Component, useMemo, useSyncExternalStore, useCallback } from 'react';
2
+ import React3, { createContext, memo, useState, useEffect, Fragment as Fragment$1, useContext, useRef, Component, useSyncExternalStore, useCallback } from 'react';
3
3
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
4
4
  import { marked } from 'marked';
5
5
 
@@ -11,7 +11,121 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
11
11
  throw Error('Dynamic require of "' + x + '" is not supported');
12
12
  });
13
13
 
14
+ // src/storage.ts
15
+ var INSTANCE_ID_KEY = "amplifyup_instance_id";
16
+ var USER_ID_KEY = "amplifyup_user_id";
17
+ var SESSION_ID_KEY = "amplifyup_session_id";
18
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
19
+ var VISITOR_FIRST_SEEN_KEY = "amplifyup_visitor_first_seen";
20
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
21
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
22
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
23
+ function generateGuid() {
24
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
25
+ return crypto.randomUUID();
26
+ }
27
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
28
+ const r = Math.random() * 16 | 0;
29
+ const v = c === "x" ? r : r & 3 | 8;
30
+ return v.toString(16);
31
+ });
32
+ }
33
+ function readCookie(name) {
34
+ if (typeof document === "undefined") return null;
35
+ const parts = document.cookie.split(";");
36
+ for (const part of parts) {
37
+ const idx = part.indexOf("=");
38
+ if (idx === -1) continue;
39
+ if (part.slice(0, idx).trim() !== name) continue;
40
+ const value = part.slice(idx + 1).trim();
41
+ try {
42
+ return value ? decodeURIComponent(value) : null;
43
+ } catch {
44
+ return value || null;
45
+ }
46
+ }
47
+ return null;
48
+ }
49
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
50
+ if (typeof document === "undefined") return;
51
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
52
+ }
53
+ function getInstanceId() {
54
+ if (typeof window === "undefined") {
55
+ return "";
56
+ }
57
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
58
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
59
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
60
+ if (fromLs !== instanceId) {
61
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
62
+ }
63
+ if (fromCookie !== instanceId) {
64
+ writeCookie(ANON_COOKIE_NAME, instanceId);
65
+ }
66
+ return instanceId;
67
+ }
68
+ function getUserId() {
69
+ if (typeof window === "undefined") {
70
+ return null;
71
+ }
72
+ return localStorage.getItem(USER_ID_KEY);
73
+ }
74
+ function getSessionId() {
75
+ if (typeof window === "undefined") {
76
+ return "";
77
+ }
78
+ const now = Date.now();
79
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
80
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
81
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
82
+ let sessionId = fromLs || fromCookie || "";
83
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
84
+ sessionId = generateGuid();
85
+ }
86
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
87
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
88
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
89
+ return sessionId;
90
+ }
91
+ function getVisitorFirstSeenAt() {
92
+ if (typeof window === "undefined") {
93
+ return null;
94
+ }
95
+ try {
96
+ const existing = localStorage.getItem(VISITOR_FIRST_SEEN_KEY);
97
+ if (existing) return existing;
98
+ const firstSeenAt = (/* @__PURE__ */ new Date()).toISOString();
99
+ localStorage.setItem(VISITOR_FIRST_SEEN_KEY, firstSeenAt);
100
+ return firstSeenAt;
101
+ } catch {
102
+ return null;
103
+ }
104
+ }
105
+ function setUserId(userId) {
106
+ if (typeof window === "undefined") {
107
+ return;
108
+ }
109
+ localStorage.setItem(USER_ID_KEY, userId);
110
+ }
111
+ function clearIds() {
112
+ if (typeof window === "undefined") {
113
+ return;
114
+ }
115
+ localStorage.removeItem(INSTANCE_ID_KEY);
116
+ localStorage.removeItem(USER_ID_KEY);
117
+ localStorage.removeItem(SESSION_ID_KEY);
118
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
119
+ localStorage.removeItem(VISITOR_FIRST_SEEN_KEY);
120
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
121
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
122
+ }
123
+
14
124
  // src/adapters/aws.ts
125
+ function visitorContext() {
126
+ const firstSeenAt = getVisitorFirstSeenAt();
127
+ return firstSeenAt ? { firstSeenAt } : void 0;
128
+ }
15
129
  var AWSAdapter = class {
16
130
  constructor() {
17
131
  this.initialized = false;
@@ -51,7 +165,9 @@ var AWSAdapter = class {
51
165
  console.warn("AmplifyUp SDK: AWS configuration missing");
52
166
  return;
53
167
  }
54
- const url = `${this.apiGatewayUrl}${endpoint}`;
168
+ const trackingId = eventData?.trackingId || this.config?.trackingId;
169
+ const query = trackingId ? `?tid=${encodeURIComponent(trackingId)}` : "";
170
+ const url = `${this.apiGatewayUrl}${endpoint}${query}`;
55
171
  try {
56
172
  const response = await fetch(url, {
57
173
  method: "POST",
@@ -113,7 +229,8 @@ var AWSAdapter = class {
113
229
  page: {
114
230
  path: properties.path || properties.url,
115
231
  referrer: properties.referrer
116
- }
232
+ },
233
+ visitor: visitorContext()
117
234
  },
118
235
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
119
236
  };
@@ -151,7 +268,8 @@ var AWSAdapter = class {
151
268
  ...properties
152
269
  },
153
270
  context: {
154
- url: properties.url || (typeof window !== "undefined" ? window.location.href : "")
271
+ url: properties.url || (typeof window !== "undefined" ? window.location.href : ""),
272
+ visitor: visitorContext()
155
273
  },
156
274
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
157
275
  };
@@ -207,100 +325,6 @@ var AWSAdapter = class {
207
325
  }
208
326
  };
209
327
 
210
- // src/storage.ts
211
- var INSTANCE_ID_KEY = "amplifyup_instance_id";
212
- var USER_ID_KEY = "amplifyup_user_id";
213
- var SESSION_ID_KEY = "amplifyup_session_id";
214
- var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
215
- var ANON_COOKIE_NAME = "amplifyup_anon_id";
216
- var SESSION_COOKIE_NAME = "amplifyup_session_id";
217
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
218
- function generateGuid() {
219
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
220
- return crypto.randomUUID();
221
- }
222
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
223
- const r = Math.random() * 16 | 0;
224
- const v = c === "x" ? r : r & 3 | 8;
225
- return v.toString(16);
226
- });
227
- }
228
- function readCookie(name) {
229
- if (typeof document === "undefined") return null;
230
- const parts = document.cookie.split(";");
231
- for (const part of parts) {
232
- const idx = part.indexOf("=");
233
- if (idx === -1) continue;
234
- if (part.slice(0, idx).trim() !== name) continue;
235
- const value = part.slice(idx + 1).trim();
236
- try {
237
- return value ? decodeURIComponent(value) : null;
238
- } catch {
239
- return value || null;
240
- }
241
- }
242
- return null;
243
- }
244
- function writeCookie(name, value, maxAgeSeconds = 31536e3) {
245
- if (typeof document === "undefined") return;
246
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
247
- }
248
- function getInstanceId() {
249
- if (typeof window === "undefined") {
250
- return "";
251
- }
252
- const fromCookie = readCookie(ANON_COOKIE_NAME);
253
- const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
254
- const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
255
- if (fromLs !== instanceId) {
256
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
257
- }
258
- if (fromCookie !== instanceId) {
259
- writeCookie(ANON_COOKIE_NAME, instanceId);
260
- }
261
- return instanceId;
262
- }
263
- function getUserId() {
264
- if (typeof window === "undefined") {
265
- return null;
266
- }
267
- return localStorage.getItem(USER_ID_KEY);
268
- }
269
- function getSessionId() {
270
- if (typeof window === "undefined") {
271
- return "";
272
- }
273
- const now = Date.now();
274
- const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
275
- const fromCookie = readCookie(SESSION_COOKIE_NAME);
276
- const fromLs = localStorage.getItem(SESSION_ID_KEY);
277
- let sessionId = fromLs || fromCookie || "";
278
- if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
279
- sessionId = generateGuid();
280
- }
281
- localStorage.setItem(SESSION_ID_KEY, sessionId);
282
- localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
283
- writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
284
- return sessionId;
285
- }
286
- function setUserId(userId) {
287
- if (typeof window === "undefined") {
288
- return;
289
- }
290
- localStorage.setItem(USER_ID_KEY, userId);
291
- }
292
- function clearIds() {
293
- if (typeof window === "undefined") {
294
- return;
295
- }
296
- localStorage.removeItem(INSTANCE_ID_KEY);
297
- localStorage.removeItem(USER_ID_KEY);
298
- localStorage.removeItem(SESSION_ID_KEY);
299
- localStorage.removeItem(SESSION_ACTIVITY_KEY);
300
- document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
301
- document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
302
- }
303
-
304
328
  // src/targets.ts
305
329
  var AWS_DEV_API = "https://api-dev.amplifyup.ai";
306
330
  var AWS_PROD_API = "https://api.amplifyup.ai";
@@ -1065,9 +1089,8 @@ function dedupeLayoutProviderFetch(key, fetchFn) {
1065
1089
  return pending;
1066
1090
  }
1067
1091
  function previewLayoutReady(opts) {
1068
- if (opts.hasClientLayout) return true;
1069
- if (opts.clientLoading) return false;
1070
- return true;
1092
+ if (opts.hasClientLayout || opts.hasServerLayout) return true;
1093
+ return !opts.clientLoading;
1071
1094
  }
1072
1095
 
1073
1096
  // src/edge/variantKeys.ts
@@ -3986,14 +4009,16 @@ function applyComposerPatch(store, name, value, ref, layoutNodeId) {
3986
4009
  store.set(composerStoreKey(name, ref, layoutNodeId), value);
3987
4010
  }
3988
4011
  function ComposerProvider({
3989
- children
4012
+ children,
4013
+ enabled = true
3990
4014
  }) {
3991
4015
  const existing = useContext(ComposerStoreContext);
3992
4016
  const storeRef = useRef(null);
3993
4017
  if (!storeRef.current) storeRef.current = new ComposerFieldStore();
3994
- const store = existing || storeRef.current;
4018
+ const ownsStore = !existing && enabled;
4019
+ const store = existing || (enabled ? storeRef.current : null);
3995
4020
  useEffect(() => {
3996
- if (existing) return;
4021
+ if (!ownsStore || !store) return;
3997
4022
  const applyField = (fieldName, value, nodeId, ref) => {
3998
4023
  if (!fieldName || typeof fieldName !== "string") return;
3999
4024
  const fieldRef = isFieldRef(ref) ? ref : void 0;
@@ -4051,8 +4076,7 @@ function ComposerProvider({
4051
4076
  window.removeEventListener("amplifyup:props-patch", onPropsPatch);
4052
4077
  window.removeEventListener("message", onMessage);
4053
4078
  };
4054
- }, [existing, store]);
4055
- if (existing) return /* @__PURE__ */ jsx(Fragment, { children });
4079
+ }, [ownsStore, store]);
4056
4080
  return /* @__PURE__ */ jsx(ComposerStoreContext.Provider, { value: store, children });
4057
4081
  }
4058
4082
  function useComposerFieldValue(name, fallback, ref) {
@@ -4079,9 +4103,7 @@ function MaybeComposerProvider({
4079
4103
  layoutNodeId,
4080
4104
  children
4081
4105
  }) {
4082
- const content = useMemo(() => children, [children]);
4083
- if (!enabled) return /* @__PURE__ */ jsx(Fragment, { children: content });
4084
- return /* @__PURE__ */ jsx(ComposerProvider, { children: /* @__PURE__ */ jsx(ComposerNodeContext.Provider, { value: layoutNodeId, children }) });
4106
+ return /* @__PURE__ */ jsx(ComposerProvider, { enabled, children: /* @__PURE__ */ jsx(ComposerNodeContext.Provider, { value: enabled ? layoutNodeId : void 0, children }) });
4085
4107
  }
4086
4108
  function nodeRenderProps(node) {
4087
4109
  const settings = node.settings || {};
@@ -4514,17 +4536,16 @@ function AmplifyRendererInner({
4514
4536
  if (layoutTree.length === 0) {
4515
4537
  return /* @__PURE__ */ jsx("main", { children: emptyComponent || /* @__PURE__ */ jsx("div", { className: "container mx-auto px-4 py-16", children: /* @__PURE__ */ jsx("p", { className: "text-muted-foreground", children: "No content configured for this page." }) }) });
4516
4538
  }
4517
- return /* @__PURE__ */ jsx(ComposerProvider, { children: /* @__PURE__ */ jsx("main", { children: composerPreviewFlag ? /* @__PURE__ */ jsx(
4518
- "div",
4539
+ return /* @__PURE__ */ jsx(ComposerProvider, { children: /* @__PURE__ */ jsx(
4540
+ "main",
4519
4541
  {
4520
- "data-slot-id": "main",
4521
- "data-amplifyup-slot": "main",
4522
- "data-slot-source": "page",
4523
- "data-slot-state": layoutTree.length === 0 ? "empty" : "filled",
4524
- "data-slot-empty": layoutTree.length === 0 ? "true" : void 0,
4542
+ "data-slot-id": composerPreviewFlag ? "main" : void 0,
4543
+ "data-amplifyup-slot": composerPreviewFlag ? "main" : void 0,
4544
+ "data-slot-source": composerPreviewFlag ? "page" : void 0,
4545
+ "data-slot-state": composerPreviewFlag ? "filled" : void 0,
4525
4546
  children: layoutTree.map((node) => /* @__PURE__ */ jsx(Fragment$1, { children: renderNode(node) }, node.id))
4526
4547
  }
4527
- ) : layoutTree.map((node) => /* @__PURE__ */ jsx(Fragment$1, { children: renderNode(node) }, node.id)) }) });
4548
+ ) });
4528
4549
  }
4529
4550
  var AmplifyRenderer = memo(AmplifyRendererInner);
4530
4551
  function AddComponentIcon({ size = 28 }) {
@@ -4700,11 +4721,14 @@ function AmplifyPageContent({
4700
4721
  if (preview) {
4701
4722
  const ready = previewLayoutReady({
4702
4723
  clientLoading: client.loading,
4703
- hasClientLayout: !!clientLayout});
4724
+ hasClientLayout: !!clientLayout,
4725
+ hasServerLayout: !!serverLayout
4726
+ });
4704
4727
  if (!ready) {
4705
4728
  return /* @__PURE__ */ jsx(ComposerLayoutStatusProvider, { ready: false, children: previewLoading });
4706
4729
  }
4707
- return /* @__PURE__ */ jsx(ComposerLayoutStatusProvider, { ready: true, children: /* @__PURE__ */ jsx(
4730
+ const layoutSettled = !client.loading || !!clientLayout;
4731
+ return /* @__PURE__ */ jsx(ComposerLayoutStatusProvider, { ready: layoutSettled, children: /* @__PURE__ */ jsx(
4708
4732
  AmplifyRenderer,
4709
4733
  {
4710
4734
  pageConfig: pageConfig ?? void 0,
@@ -4726,7 +4750,7 @@ function AmplifyPageContent({
4726
4750
  if (!hasAmplifyLayout(pageConfig)) {
4727
4751
  return fallback ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
4728
4752
  }
4729
- return /* @__PURE__ */ jsx(
4753
+ return /* @__PURE__ */ jsx(ComposerLayoutStatusProvider, { ready: true, children: /* @__PURE__ */ jsx(
4730
4754
  AmplifyRenderer,
4731
4755
  {
4732
4756
  pageConfig: pageConfig ?? void 0,
@@ -4735,7 +4759,7 @@ function AmplifyPageContent({
4735
4759
  emptyComponent,
4736
4760
  errorComponent
4737
4761
  }
4738
- );
4762
+ ) });
4739
4763
  }
4740
4764
 
4741
4765
  // src/slot-state.ts