@tolinku/react-native-sdk 0.5.0 → 0.6.1

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/index.js CHANGED
@@ -2,15 +2,16 @@
2
2
 
3
3
  var reactNative = require('react-native');
4
4
  var AsyncStorage = require('@react-native-async-storage/async-storage');
5
- var react = require('react');
5
+ var React = require('react');
6
6
  var jsxRuntime = require('react/jsx-runtime');
7
7
 
8
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
9
 
10
10
  var AsyncStorage__default = /*#__PURE__*/_interopDefault(AsyncStorage);
11
+ var React__default = /*#__PURE__*/_interopDefault(React);
11
12
 
12
13
  // src/types.ts
13
- var SDK_VERSION = "0.5.0";
14
+ var SDK_VERSION = "0.6.1";
14
15
 
15
16
  // src/debug.ts
16
17
  var _debugEnabled = false;
@@ -74,6 +75,25 @@ var HttpClient = class {
74
75
  headers: this.publicHeaders()
75
76
  });
76
77
  }
78
+ /**
79
+ * POST to a host other than the configured one, without the API key.
80
+ *
81
+ * A few public endpoints work out which Appspace they belong to from the
82
+ * hostname the request arrived on rather than from a key or an id, so a
83
+ * question about a link on a customer's own domain has to be asked on that
84
+ * domain. Everything else about the request is unchanged: same timeout, same
85
+ * retries, same headers.
86
+ */
87
+ async postPublicToOrigin(origin, path, body) {
88
+ return this.executeWithRetry(origin.replace(/\/+$/, "") + path, {
89
+ method: "POST",
90
+ headers: {
91
+ ...this.publicHeaders(),
92
+ "Content-Type": "application/json"
93
+ },
94
+ body: body ? JSON.stringify(body) : void 0
95
+ });
96
+ }
77
97
  /** POST without API key auth (for public endpoints like deferred claim) */
78
98
  async postPublic(path, body) {
79
99
  return this.executeWithRetry(this.baseUrl + path, {
@@ -205,16 +225,44 @@ var TolinkuError = class extends Error {
205
225
  };
206
226
 
207
227
  // src/validation.ts
228
+ function urlScheme(url) {
229
+ if (!url || typeof url !== "string") return null;
230
+ const cleaned = url.replace(/[\t\n\r]/g, "").replace(/^[\u0000-\u0020]+|[\u0000-\u0020]+$/g, "");
231
+ const match = /^([a-zA-Z][a-zA-Z\d+\-._]*):/.exec(cleaned);
232
+ return match ? `${match[1].toLowerCase()}:` : null;
233
+ }
234
+ function parseHttpUrl(url) {
235
+ if (!url || typeof url !== "string") return null;
236
+ const cleaned = url.replace(/[\t\n\r]/g, "").trim();
237
+ const match = /^(https?):\/\/([^/?#]+)([^?#]*)/i.exec(cleaned);
238
+ if (!match) return null;
239
+ const [, scheme, authority, path] = match;
240
+ if (!authority) return null;
241
+ return {
242
+ origin: `${scheme.toLowerCase()}://${authority.toLowerCase()}`,
243
+ // A URL with no path means the root, which is what a server expects to be
244
+ // asked about.
245
+ pathname: path || "/"
246
+ };
247
+ }
208
248
  function isSafeUrl(url) {
209
- if (!url || typeof url !== "string") return false;
210
- const trimmed = url.trim();
211
- if (!trimmed) return false;
212
- try {
213
- const parsed = new URL(trimmed);
214
- return parsed.protocol === "http:" || parsed.protocol === "https:";
215
- } catch {
216
- return false;
217
- }
249
+ const scheme = urlScheme(url);
250
+ return scheme === "http:" || scheme === "https:";
251
+ }
252
+ var EXECUTABLE_SCHEMES = /* @__PURE__ */ new Set([
253
+ "javascript:",
254
+ "vbscript:",
255
+ "data:",
256
+ "blob:",
257
+ "file:",
258
+ "content:",
259
+ "jar:",
260
+ "filesystem:"
261
+ ]);
262
+ function isSafeActionUrl(url) {
263
+ const scheme = urlScheme(url);
264
+ if (!scheme) return false;
265
+ return !EXECUTABLE_SCHEMES.has(scheme);
218
266
  }
219
267
  function validateBaseUrl(baseUrl) {
220
268
  if (typeof baseUrl !== "string") {
@@ -311,13 +359,7 @@ var _Analytics = class _Analytics {
311
359
  if (this.appOpensDisabled) return;
312
360
  const trimmed = typeof url === "string" ? url.trim() : "";
313
361
  if (!trimmed) return;
314
- let parsed;
315
- try {
316
- parsed = new URL(trimmed);
317
- } catch {
318
- return;
319
- }
320
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return;
362
+ if (!isSafeUrl(trimmed)) return;
321
363
  const now = Date.now();
322
364
  if (this.lastOpenUrl === trimmed && now - this.lastOpenAt < _Analytics.OPEN_DEDUPE_MS) return;
323
365
  this.lastOpenUrl = trimmed;
@@ -849,6 +891,48 @@ function override(value) {
849
891
  }
850
892
  return void 0;
851
893
  }
894
+
895
+ // src/links.ts
896
+ function isRoutablePath(path) {
897
+ return typeof path === "string" && path.startsWith("/") && !path.startsWith("//");
898
+ }
899
+ var Links = class {
900
+ constructor(client) {
901
+ this.client = client;
902
+ }
903
+ /**
904
+ * What this link means, or null if it means nothing here.
905
+ *
906
+ * `url` is a whole link as the app received it. The question goes to the
907
+ * link's own host, because that is how the platform knows which Appspace is
908
+ * being asked about, which also means a link on a domain that is not yours
909
+ * simply answers nothing.
910
+ *
911
+ * Never throws. A link that cannot be resolved, for a bad network or any
912
+ * other reason, is one the app should fall back to its own handling for, and
913
+ * an exception in the middle of a cold start is no way to say so.
914
+ */
915
+ async resolve(url) {
916
+ if (!url || typeof url !== "string") return null;
917
+ const parsed = parseHttpUrl(url);
918
+ if (!parsed) {
919
+ debugWarn(`Links.resolve: not an http(s) URL: ${url}`);
920
+ return null;
921
+ }
922
+ try {
923
+ const result = await this.client.postPublicToOrigin(
924
+ parsed.origin,
925
+ "/v1/api/path",
926
+ { path: parsed.pathname }
927
+ );
928
+ if (!result || !result.route || !isRoutablePath(result.deep_link_path)) return null;
929
+ return result;
930
+ } catch (err) {
931
+ debugWarn(`Links.resolve failed: ${err.message}`);
932
+ return null;
933
+ }
934
+ }
935
+ };
852
936
  var BASE_KEY = "tolinku_message_dismissed";
853
937
  var IMPRESSIONS_BASE_KEY = "tolinku_message_impressions";
854
938
  var LAST_SHOWN_BASE_KEY = "tolinku_message_last_shown";
@@ -972,6 +1056,7 @@ var _Tolinku = class _Tolinku {
972
1056
  _Tolinku.ecommerceInstance = new Ecommerce(_Tolinku.client, () => _Tolinku._userId);
973
1057
  _Tolinku.referralsInstance = new Referrals(_Tolinku.client);
974
1058
  _Tolinku.deferredInstance = new Deferred(_Tolinku.client);
1059
+ _Tolinku.linksInstance = new Links(_Tolinku.client);
975
1060
  _Tolinku._initialized = true;
976
1061
  debugLog(`Tolinku SDK v${SDK_VERSION} initialized (baseUrl=${baseUrl})`);
977
1062
  }
@@ -1077,6 +1162,18 @@ var _Tolinku = class _Tolinku {
1077
1162
  }
1078
1163
  return _Tolinku.deferredInstance;
1079
1164
  }
1165
+ /**
1166
+ * Turning a link the app was handed into the route and token it means.
1167
+ *
1168
+ * Needed for short links, which arrive as an opaque code that nothing on the
1169
+ * device can interpret. See {@link Links.resolve}.
1170
+ */
1171
+ static get links() {
1172
+ if (!_Tolinku.linksInstance) {
1173
+ throw new Error("Tolinku: SDK not initialized. Call Tolinku.init() first.");
1174
+ }
1175
+ return _Tolinku.linksInstance;
1176
+ }
1080
1177
  /**
1081
1178
  * Shut down the SDK and release resources.
1082
1179
  * Flushes remaining analytics events, cancels timers, removes listeners,
@@ -1100,6 +1197,7 @@ var _Tolinku = class _Tolinku {
1100
1197
  _Tolinku.ecommerceInstance = null;
1101
1198
  _Tolinku.referralsInstance = null;
1102
1199
  _Tolinku.deferredInstance = null;
1200
+ _Tolinku.linksInstance = null;
1103
1201
  _Tolinku._initialized = false;
1104
1202
  _Tolinku._userId = null;
1105
1203
  setDebugEnabled(false);
@@ -1111,32 +1209,368 @@ _Tolinku.analyticsInstance = null;
1111
1209
  _Tolinku.ecommerceInstance = null;
1112
1210
  _Tolinku.referralsInstance = null;
1113
1211
  _Tolinku.deferredInstance = null;
1212
+ _Tolinku.linksInstance = null;
1114
1213
  _Tolinku._initialized = false;
1115
1214
  _Tolinku._userId = null;
1116
1215
  var Tolinku = _Tolinku;
1216
+ var DEFAULT_IMAGE_HEIGHT = 200;
1217
+ function dimension(raw) {
1218
+ if (typeof raw === "number" && isFinite(raw) && raw > 0) return raw;
1219
+ if (typeof raw !== "string") return void 0;
1220
+ const value = raw.trim();
1221
+ if (!value) return void 0;
1222
+ if (value.endsWith("%")) {
1223
+ return /^\d+(?:\.\d+)?%$/.test(value) ? value : void 0;
1224
+ }
1225
+ const match = /^(\d+(?:\.\d+)?)(?:px)?$/i.exec(value);
1226
+ if (!match) {
1227
+ debugWarn(`Dimension "${raw}" is not a size React Native understands; ignoring it.`);
1228
+ return void 0;
1229
+ }
1230
+ const parsed = parseFloat(match[1]);
1231
+ return parsed > 0 ? parsed : void 0;
1232
+ }
1233
+ function num(raw, fallback) {
1234
+ if (typeof raw === "number" && isFinite(raw)) return raw;
1235
+ if (typeof raw === "string" && raw.trim()) {
1236
+ const parsed = parseFloat(raw);
1237
+ if (isFinite(parsed)) return parsed;
1238
+ }
1239
+ return fallback;
1240
+ }
1241
+ function alignment(raw) {
1242
+ return raw === "center" || raw === "right" || raw === "justify" ? raw : "left";
1243
+ }
1244
+ var NAMED_COLORS = /* @__PURE__ */ new Set([
1245
+ "transparent",
1246
+ "aliceblue",
1247
+ "antiquewhite",
1248
+ "aqua",
1249
+ "aquamarine",
1250
+ "azure",
1251
+ "beige",
1252
+ "bisque",
1253
+ "black",
1254
+ "blanchedalmond",
1255
+ "blue",
1256
+ "blueviolet",
1257
+ "brown",
1258
+ "burlywood",
1259
+ "cadetblue",
1260
+ "chartreuse",
1261
+ "chocolate",
1262
+ "coral",
1263
+ "cornflowerblue",
1264
+ "cornsilk",
1265
+ "crimson",
1266
+ "cyan",
1267
+ "darkblue",
1268
+ "darkcyan",
1269
+ "darkgoldenrod",
1270
+ "darkgray",
1271
+ "darkgreen",
1272
+ "darkgrey",
1273
+ "darkkhaki",
1274
+ "darkmagenta",
1275
+ "darkolivegreen",
1276
+ "darkorange",
1277
+ "darkorchid",
1278
+ "darkred",
1279
+ "darksalmon",
1280
+ "darkseagreen",
1281
+ "darkslateblue",
1282
+ "darkslategray",
1283
+ "darkslategrey",
1284
+ "darkturquoise",
1285
+ "darkviolet",
1286
+ "deeppink",
1287
+ "deepskyblue",
1288
+ "dimgray",
1289
+ "dimgrey",
1290
+ "dodgerblue",
1291
+ "firebrick",
1292
+ "floralwhite",
1293
+ "forestgreen",
1294
+ "fuchsia",
1295
+ "gainsboro",
1296
+ "ghostwhite",
1297
+ "gold",
1298
+ "goldenrod",
1299
+ "gray",
1300
+ "green",
1301
+ "greenyellow",
1302
+ "grey",
1303
+ "honeydew",
1304
+ "hotpink",
1305
+ "indianred",
1306
+ "indigo",
1307
+ "ivory",
1308
+ "khaki",
1309
+ "lavender",
1310
+ "lavenderblush",
1311
+ "lawngreen",
1312
+ "lemonchiffon",
1313
+ "lightblue",
1314
+ "lightcoral",
1315
+ "lightcyan",
1316
+ "lightgoldenrodyellow",
1317
+ "lightgray",
1318
+ "lightgreen",
1319
+ "lightgrey",
1320
+ "lightpink",
1321
+ "lightsalmon",
1322
+ "lightseagreen",
1323
+ "lightskyblue",
1324
+ "lightslategray",
1325
+ "lightslategrey",
1326
+ "lightsteelblue",
1327
+ "lightyellow",
1328
+ "lime",
1329
+ "limegreen",
1330
+ "linen",
1331
+ "magenta",
1332
+ "maroon",
1333
+ "mediumaquamarine",
1334
+ "mediumblue",
1335
+ "mediumorchid",
1336
+ "mediumpurple",
1337
+ "mediumseagreen",
1338
+ "mediumslateblue",
1339
+ "mediumspringgreen",
1340
+ "mediumturquoise",
1341
+ "mediumvioletred",
1342
+ "midnightblue",
1343
+ "mintcream",
1344
+ "mistyrose",
1345
+ "moccasin",
1346
+ "navajowhite",
1347
+ "navy",
1348
+ "oldlace",
1349
+ "olive",
1350
+ "olivedrab",
1351
+ "orange",
1352
+ "orangered",
1353
+ "orchid",
1354
+ "palegoldenrod",
1355
+ "palegreen",
1356
+ "paleturquoise",
1357
+ "palevioletred",
1358
+ "papayawhip",
1359
+ "peachpuff",
1360
+ "peru",
1361
+ "pink",
1362
+ "plum",
1363
+ "powderblue",
1364
+ "purple",
1365
+ "rebeccapurple",
1366
+ "red",
1367
+ "rosybrown",
1368
+ "royalblue",
1369
+ "saddlebrown",
1370
+ "salmon",
1371
+ "sandybrown",
1372
+ "seagreen",
1373
+ "seashell",
1374
+ "sienna",
1375
+ "silver",
1376
+ "skyblue",
1377
+ "slateblue",
1378
+ "slategray",
1379
+ "slategrey",
1380
+ "snow",
1381
+ "springgreen",
1382
+ "steelblue",
1383
+ "tan",
1384
+ "teal",
1385
+ "thistle",
1386
+ "tomato",
1387
+ "turquoise",
1388
+ "violet",
1389
+ "wheat",
1390
+ "white",
1391
+ "whitesmoke",
1392
+ "yellow",
1393
+ "yellowgreen"
1394
+ ]);
1395
+ function color(raw, fallback) {
1396
+ if (typeof raw !== "string") return fallback;
1397
+ const value = raw.trim();
1398
+ if (!value) return fallback;
1399
+ if (/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value)) return value;
1400
+ if (/^(?:rgb|rgba|hsl|hsla)\(\s*[\d.]+\s*,[^)%]*\)$/i.test(value)) return value;
1401
+ if (NAMED_COLORS.has(value.toLowerCase())) return value.toLowerCase();
1402
+ debugWarn(`Colour "${raw}" is not one React Native can read; using ${fallback}.`);
1403
+ return fallback;
1404
+ }
1405
+ function softVariant(hex) {
1406
+ if (/^#[0-9a-f]{6}$/i.test(hex)) return `${hex}22`;
1407
+ if (/^#[0-9a-f]{3}$/i.test(hex)) {
1408
+ const [, r, g, b] = hex;
1409
+ return `#${r}${r}${g}${g}${b}${b}22`;
1410
+ }
1411
+ return null;
1412
+ }
1413
+ function richText(text, onPressUrl) {
1414
+ if (!text) return "";
1415
+ const regex = /\[([^\]]+)\]\(([^)]+)\)/g;
1416
+ const parts = [];
1417
+ let lastIndex = 0;
1418
+ let match;
1419
+ let key = 0;
1420
+ while ((match = regex.exec(text)) !== null) {
1421
+ if (match.index > lastIndex) parts.push(text.slice(lastIndex, match.index));
1422
+ const [full, label, rawUrl] = match;
1423
+ const url = (rawUrl || "").trim();
1424
+ if (isSafeActionUrl(url)) {
1425
+ parts.push(
1426
+ /* @__PURE__ */ jsxRuntime.jsx(
1427
+ reactNative.Text,
1428
+ {
1429
+ style: { textDecorationLine: "underline" },
1430
+ onPress: () => onPressUrl(url),
1431
+ children: label
1432
+ },
1433
+ `link-${key++}`
1434
+ )
1435
+ );
1436
+ } else {
1437
+ parts.push(full);
1438
+ }
1439
+ lastIndex = regex.lastIndex;
1440
+ }
1441
+ if (lastIndex < text.length) parts.push(text.slice(lastIndex));
1442
+ return parts.length === 1 ? parts[0] : parts;
1443
+ }
1444
+ function useImageRatio(uri, enabled) {
1445
+ const [ratio, setRatio] = React__default.default.useState(null);
1446
+ React__default.default.useEffect(() => {
1447
+ if (!enabled || !uri) return;
1448
+ let cancelled = false;
1449
+ reactNative.Image.getSize(
1450
+ uri,
1451
+ (w, h) => {
1452
+ if (!cancelled && w > 0 && h > 0) setRatio(w / h);
1453
+ },
1454
+ () => {
1455
+ }
1456
+ );
1457
+ return () => {
1458
+ cancelled = true;
1459
+ };
1460
+ }, [uri, enabled]);
1461
+ return ratio;
1462
+ }
1463
+ function MessageImage({
1464
+ uri,
1465
+ width,
1466
+ height,
1467
+ borderRadius,
1468
+ borderWidth,
1469
+ borderColor,
1470
+ alt
1471
+ }) {
1472
+ const wantsNaturalHeight = height === void 0;
1473
+ const ratio = useImageRatio(uri, wantsNaturalHeight);
1474
+ const style = {
1475
+ width: width ?? "100%",
1476
+ borderRadius,
1477
+ alignSelf: "center",
1478
+ marginBottom: 8,
1479
+ ...borderWidth > 0 ? { borderWidth, borderColor } : {},
1480
+ ...wantsNaturalHeight ? ratio ? { aspectRatio: ratio } : { height: DEFAULT_IMAGE_HEIGHT } : { height }
1481
+ };
1482
+ return /* @__PURE__ */ jsxRuntime.jsx(
1483
+ reactNative.Image,
1484
+ {
1485
+ source: { uri },
1486
+ style,
1487
+ resizeMode: wantsNaturalHeight && !ratio ? "contain" : "cover",
1488
+ accessibilityLabel: alt
1489
+ }
1490
+ );
1491
+ }
1492
+ function StoreBadge({
1493
+ uri,
1494
+ height,
1495
+ label,
1496
+ onPress
1497
+ }) {
1498
+ const usable = !!uri && isSafeUrl(uri);
1499
+ const ratio = useImageRatio(usable ? uri : "", usable);
1500
+ const usableHeight = height > 0 ? height : 44;
1501
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.TouchableOpacity, { onPress, activeOpacity: 0.7, accessibilityRole: "button", children: usable ? /* @__PURE__ */ jsxRuntime.jsx(
1502
+ reactNative.Image,
1503
+ {
1504
+ source: { uri },
1505
+ style: { height: usableHeight, width: usableHeight * (ratio || 3.47), marginHorizontal: 6 },
1506
+ resizeMode: "contain",
1507
+ accessibilityLabel: label
1508
+ }
1509
+ ) : (
1510
+ // A text link rather than a blank space, for the case where the badge
1511
+ // artwork itself cannot be loaded.
1512
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: { color: "#1B1B1B", fontSize: 15, fontWeight: "600", marginHorizontal: 6 }, children: label })
1513
+ ) });
1514
+ }
1515
+ var SUPPORTED = /* @__PURE__ */ new Set([
1516
+ "Heading",
1517
+ "TextBlock",
1518
+ "Image",
1519
+ "Button",
1520
+ "DeepLinkButton",
1521
+ "Section",
1522
+ "Spacer",
1523
+ "Divider",
1524
+ "AppIcon",
1525
+ "StoreButtons"
1526
+ ]);
1527
+ function drawsAnything(content, app) {
1528
+ return (content || []).some((c) => {
1529
+ if (!c || !SUPPORTED.has(c.type)) return false;
1530
+ const props = c.props || {};
1531
+ if (c.type === "Image") return isSafeUrl(props.url || "");
1532
+ if (c.type === "DeepLinkButton") return !!app?.deep_link_url;
1533
+ if (c.type === "StoreButtons") {
1534
+ const ios = props.showIos !== false && (props.iosUrlOverride || app?.ios_store_url);
1535
+ const android = props.showAndroid !== false && (props.androidUrlOverride || app?.android_store_url);
1536
+ return !!(ios || android);
1537
+ }
1538
+ return true;
1539
+ });
1540
+ }
1541
+ var MAX_SECTION_DEPTH = 20;
1117
1542
  function PuckComponentRenderer({ component, messageId, options }) {
1118
- const { props } = component;
1543
+ if (!component || typeof component !== "object" || typeof component.type !== "string") return null;
1544
+ const props = component.props && typeof component.props === "object" ? component.props : {};
1545
+ const app = options.app;
1546
+ const follow = (url) => {
1547
+ if (options.onButtonPress) options.onButtonPress(url, messageId);
1548
+ else reactNative.Linking.openURL(url).catch(() => {
1549
+ });
1550
+ };
1119
1551
  switch (component.type) {
1120
1552
  case "Heading": {
1553
+ const fontSize = num(props.fontSize, 28);
1121
1554
  const style = {
1122
- fontSize: props.fontSize || 28,
1555
+ fontSize,
1123
1556
  fontWeight: "700",
1124
- color: props.color || "#1B1B1B",
1125
- textAlign: props.alignment || "left",
1126
- lineHeight: (props.fontSize || 28) * 1.2,
1557
+ color: color(props.color, "#1B1B1B"),
1558
+ textAlign: alignment(props.alignment),
1559
+ lineHeight: fontSize * 1.2,
1127
1560
  marginBottom: 8
1128
1561
  };
1129
1562
  return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style, children: props.text || "" });
1130
1563
  }
1131
1564
  case "TextBlock": {
1565
+ const fontSize = num(props.fontSize, 15);
1132
1566
  const style = {
1133
- fontSize: props.fontSize || 15,
1134
- color: props.color || "#555555",
1135
- textAlign: props.alignment || "left",
1136
- lineHeight: (props.fontSize || 15) * 1.5,
1567
+ fontSize,
1568
+ color: color(props.color, "#555555"),
1569
+ textAlign: alignment(props.alignment),
1570
+ lineHeight: fontSize * 1.5,
1137
1571
  marginBottom: 8
1138
1572
  };
1139
- return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style, children: props.content || "" });
1573
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style, children: richText(props.content || "", follow) });
1140
1574
  }
1141
1575
  case "Image": {
1142
1576
  const imageUrl = props.url || "";
@@ -1148,82 +1582,85 @@ function PuckComponentRenderer({ component, messageId, options }) {
1148
1582
  debugWarn(`Image URL blocked (unsafe protocol): ${imageUrl}`);
1149
1583
  return null;
1150
1584
  }
1151
- const widthRaw = props.width || "100%";
1152
- let imageWidth = "100%";
1153
- if (widthRaw.endsWith("px")) {
1154
- imageWidth = parseInt(widthRaw, 10);
1155
- } else if (widthRaw.endsWith("%")) {
1156
- imageWidth = widthRaw;
1157
- } else {
1158
- const parsed = parseInt(widthRaw, 10);
1159
- imageWidth = isNaN(parsed) ? "100%" : parsed;
1160
- }
1161
- const style = {
1162
- width: imageWidth,
1163
- height: props.height || 200,
1164
- borderRadius: props.borderRadius || 8,
1165
- alignSelf: "center",
1166
- marginBottom: 8
1167
- };
1168
1585
  return /* @__PURE__ */ jsxRuntime.jsx(
1169
- reactNative.Image,
1586
+ MessageImage,
1170
1587
  {
1171
- source: { uri: imageUrl },
1172
- style,
1173
- resizeMode: "cover",
1174
- accessibilityLabel: props.alt || ""
1588
+ uri: imageUrl,
1589
+ width: dimension(props.width) ?? "100%",
1590
+ height: dimension(props.height),
1591
+ borderRadius: num(props.borderRadius, 8),
1592
+ borderWidth: num(props.borderWidth, 0),
1593
+ borderColor: color(props.borderColor, "#1B1B1B"),
1594
+ alt: props.alt || ""
1175
1595
  }
1176
1596
  );
1177
1597
  }
1178
- case "Button": {
1598
+ case "Button":
1599
+ case "DeepLinkButton": {
1600
+ const isDeepLink = component.type === "DeepLinkButton";
1601
+ const action = isDeepLink ? app?.deep_link_url || "" : props.action || "";
1602
+ const label = isDeepLink ? props.label || "Open in App" : props.label || "";
1603
+ if (isDeepLink && !action) return null;
1179
1604
  const handlePress = () => {
1180
- const action = props.action || "";
1181
1605
  if (!action) return;
1182
- if (!isSafeUrl(action)) {
1183
- debugWarn(`Button action URL blocked (unsafe protocol): ${action}`);
1606
+ if (action === "close") {
1607
+ options.onRequestClose?.();
1184
1608
  return;
1185
1609
  }
1186
- if (options.onButtonPress) {
1187
- options.onButtonPress(action, messageId);
1188
- } else {
1189
- reactNative.Linking.openURL(action).catch(() => {
1190
- });
1610
+ if (!isSafeActionUrl(action)) {
1611
+ debugWarn(`Button action URL blocked (unsafe protocol): ${action}`);
1612
+ return;
1191
1613
  }
1614
+ follow(action);
1192
1615
  };
1616
+ const variant = props.style || "filled";
1617
+ const isOutline = variant === "outline";
1618
+ const tint = variant === "soft" ? softVariant(color(props.bgColor, "#1B1B1B")) : null;
1619
+ const isSoft = tint !== null;
1620
+ const baseColor = color(props.bgColor, "#1B1B1B");
1621
+ const background = isOutline ? "transparent" : isSoft ? tint : baseColor;
1622
+ const labelColor = isOutline || isSoft ? baseColor : color(props.textColor, "#ffffff");
1193
1623
  const containerStyle = {
1194
- backgroundColor: props.bgColor || "#1B1B1B",
1195
- borderRadius: props.borderRadius || 8,
1196
- paddingVertical: 10,
1197
- paddingHorizontal: 20,
1624
+ backgroundColor: background,
1625
+ borderRadius: num(props.borderRadius, 8),
1626
+ paddingVertical: 12,
1627
+ paddingHorizontal: 24,
1198
1628
  marginVertical: 8,
1199
1629
  alignItems: "center",
1200
- ...props.fullWidth ? { width: "100%" } : {}
1630
+ ...isOutline ? { borderWidth: 2, borderColor: baseColor } : {},
1631
+ // A button that is not full width is a compact, centred one. Without
1632
+ // alignSelf it stretches to the column anyway, which made the setting
1633
+ // do nothing at all.
1634
+ ...props.fullWidth ? { width: "100%" } : { alignSelf: "center" }
1201
1635
  };
1202
1636
  const textStyle = {
1203
- color: props.textColor || "#ffffff",
1204
- fontSize: props.fontSize || 16,
1637
+ color: labelColor,
1638
+ fontSize: num(props.fontSize, 16),
1205
1639
  fontWeight: "600"
1206
1640
  };
1207
- return /* @__PURE__ */ jsxRuntime.jsx(reactNative.TouchableOpacity, { onPress: handlePress, style: containerStyle, activeOpacity: 0.7, children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: textStyle, children: props.label || "Click" }) });
1641
+ const emoji = props.emoji || "";
1642
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.TouchableOpacity, { onPress: handlePress, style: containerStyle, activeOpacity: 0.7, children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: textStyle, children: emoji ? `${emoji} ${label}` : label }) });
1208
1643
  }
1209
1644
  case "Section": {
1210
- const children = props.children || [];
1645
+ const sectionId = typeof props.id === "string" && props.id ? props.id : null;
1646
+ const depth = options.depth || 0;
1647
+ const children = sectionId && depth < MAX_SECTION_DEPTH ? options.zones?.[`${sectionId}:content`] || [] : [];
1211
1648
  const containerStyle = {
1212
- backgroundColor: props.bgColor || void 0,
1213
- padding: props.padding || 16,
1214
- borderRadius: props.borderRadius || 0,
1649
+ backgroundColor: typeof props.bgColor === "string" && props.bgColor ? color(props.bgColor, "transparent") : void 0,
1650
+ padding: num(props.padding, 16),
1651
+ borderRadius: num(props.borderRadius, 0),
1215
1652
  marginVertical: 8
1216
1653
  };
1217
- const bgImage = props.bgImage || "";
1218
1654
  const content = children.map((child, index) => /* @__PURE__ */ jsxRuntime.jsx(
1219
1655
  PuckComponentRenderer,
1220
1656
  {
1221
1657
  component: child,
1222
1658
  messageId,
1223
- options
1659
+ options: { ...options, depth: depth + 1 }
1224
1660
  },
1225
- `${messageId}-section-${index}-${child.type}`
1661
+ `${messageId}-section-${index}-${child?.type}`
1226
1662
  ));
1663
+ const bgImage = props.bgImage || "";
1227
1664
  if (bgImage) {
1228
1665
  if (!isSafeUrl(bgImage)) {
1229
1666
  debugWarn(`Section background image URL blocked (unsafe protocol): ${bgImage}`);
@@ -1235,32 +1672,93 @@ function PuckComponentRenderer({ component, messageId, options }) {
1235
1672
  source: { uri: bgImage },
1236
1673
  style: containerStyle,
1237
1674
  resizeMode: props.bgSize === "contain" ? "contain" : "cover",
1238
- imageStyle: { borderRadius: props.borderRadius || 0 },
1675
+ imageStyle: { borderRadius: num(props.borderRadius, 0) },
1239
1676
  children: content
1240
1677
  }
1241
1678
  );
1242
1679
  }
1243
1680
  return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: containerStyle, children: content });
1244
1681
  }
1245
- case "Spacer": {
1246
- const style = {
1247
- height: props.height || 24
1248
- };
1249
- return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style });
1682
+ case "Spacer":
1683
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: { height: num(props.height, 24) } });
1684
+ case "Divider":
1685
+ return /* @__PURE__ */ jsxRuntime.jsx(
1686
+ reactNative.View,
1687
+ {
1688
+ style: {
1689
+ borderTopWidth: num(props.thickness, 1),
1690
+ borderTopColor: color(props.color, "#e5e5e5"),
1691
+ marginVertical: 8
1692
+ }
1693
+ }
1694
+ );
1695
+ case "AppIcon": {
1696
+ const size = num(props.size, 80);
1697
+ const borderRadius = num(props.borderRadius, 16);
1698
+ const iconUrl = app?.icon_url || "";
1699
+ if (!iconUrl || !isSafeUrl(iconUrl)) {
1700
+ return /* @__PURE__ */ jsxRuntime.jsx(
1701
+ reactNative.View,
1702
+ {
1703
+ style: { width: size, height: size, borderRadius, backgroundColor: "#f0f0f0", alignSelf: "center", marginBottom: 8 }
1704
+ }
1705
+ );
1706
+ }
1707
+ return /* @__PURE__ */ jsxRuntime.jsx(
1708
+ reactNative.Image,
1709
+ {
1710
+ source: { uri: iconUrl },
1711
+ style: { width: size, height: size, borderRadius, alignSelf: "center", marginBottom: 8 },
1712
+ resizeMode: "cover",
1713
+ accessibilityLabel: app?.name || "App icon"
1714
+ }
1715
+ );
1250
1716
  }
1251
- case "Divider": {
1252
- const style = {
1253
- borderTopWidth: props.thickness || 1,
1254
- borderTopColor: props.color || "#e5e5e5",
1255
- marginVertical: 8
1256
- };
1257
- return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style });
1717
+ case "StoreButtons": {
1718
+ const iosUrl = props.iosUrlOverride || app?.ios_store_url || "";
1719
+ const androidUrl = props.androidUrlOverride || app?.android_store_url || "";
1720
+ const showIos = props.showIos !== false && !!iosUrl;
1721
+ const showAndroid = props.showAndroid !== false && !!androidUrl;
1722
+ if (!showIos && !showAndroid) return null;
1723
+ const height = num(props.height, 44);
1724
+ const alignment2 = props.alignment;
1725
+ const justifyContent = alignment2 === "left" ? "flex-start" : alignment2 === "right" ? "flex-end" : "center";
1726
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: { flexDirection: "row", justifyContent, alignItems: "center", flexWrap: "wrap", marginVertical: 8 }, children: [
1727
+ showIos ? /* @__PURE__ */ jsxRuntime.jsx(
1728
+ StoreBadge,
1729
+ {
1730
+ uri: app?.ios_badge_url,
1731
+ height,
1732
+ label: "Download on the App Store",
1733
+ onPress: () => follow(iosUrl)
1734
+ },
1735
+ "ios"
1736
+ ) : null,
1737
+ showAndroid ? /* @__PURE__ */ jsxRuntime.jsx(
1738
+ StoreBadge,
1739
+ {
1740
+ uri: app?.android_badge_url,
1741
+ height,
1742
+ label: "Get it on Google Play",
1743
+ onPress: () => follow(androidUrl)
1744
+ },
1745
+ "android"
1746
+ ) : null
1747
+ ] });
1258
1748
  }
1259
1749
  default:
1750
+ debugWarn(`Message component "${component.type}" is not supported here; skipping it.`);
1260
1751
  return null;
1261
1752
  }
1262
1753
  }
1263
- function MessageModal({ message, visible, onClose, options }) {
1754
+ function gradientStartColor(gradient) {
1755
+ if (typeof gradient !== "string" || !gradient) return null;
1756
+ const match = /#[0-9a-f]{3,8}|rgba?\([^)]*\)/i.exec(gradient);
1757
+ if (!match) return null;
1758
+ const validated = color(match[0], "");
1759
+ return validated || null;
1760
+ }
1761
+ function MessageModal({ message, visible, onClose, options, app }) {
1264
1762
  if (!message) {
1265
1763
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
1266
1764
  }
@@ -1269,6 +1767,18 @@ function MessageModal({ message, visible, onClose, options }) {
1269
1767
  options.onDismiss?.(message.id);
1270
1768
  onClose();
1271
1769
  };
1770
+ const rootProps = message.content?.root?.props || {};
1771
+ const background = gradientStartColor(rootProps.bgGradient) || (typeof rootProps.bgColor === "string" && rootProps.bgColor ? color(rootProps.bgColor, "") : "") || message.background_color || "#ffffff";
1772
+ const cardStyle = {
1773
+ backgroundColor: background,
1774
+ padding: Math.max(0, num(rootProps.padding, 20)),
1775
+ // Clamped, unlike the other authored numbers. A padding of zero is a
1776
+ // choice; a width of zero is the whole message gone, and the field's own
1777
+ // minimum is zero. The bounds and the default match the server renderer.
1778
+ maxWidth: Math.min(2e3, Math.max(100, num(rootProps.contentWidth, 480)))
1779
+ };
1780
+ const content = message.content?.content || [];
1781
+ const hasDesignedContent = drawsAnything(content, app);
1272
1782
  return /* @__PURE__ */ jsxRuntime.jsx(
1273
1783
  reactNative.Modal,
1274
1784
  {
@@ -1276,7 +1786,7 @@ function MessageModal({ message, visible, onClose, options }) {
1276
1786
  transparent: true,
1277
1787
  animationType: "fade",
1278
1788
  onRequestClose: handleDismiss,
1279
- children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.Pressable, { style: styles.overlay, onPress: handleDismiss, children: /* @__PURE__ */ jsxRuntime.jsxs(reactNative.Pressable, { style: [styles.card, { backgroundColor: message.background_color || "#ffffff" }], onPress: () => {
1789
+ children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.Pressable, { style: styles.overlay, onPress: handleDismiss, children: /* @__PURE__ */ jsxRuntime.jsxs(reactNative.Pressable, { style: [styles.card, cardStyle], onPress: () => {
1280
1790
  }, children: [
1281
1791
  /* @__PURE__ */ jsxRuntime.jsx(
1282
1792
  reactNative.TouchableOpacity,
@@ -1293,15 +1803,23 @@ function MessageModal({ message, visible, onClose, options }) {
1293
1803
  {
1294
1804
  style: styles.scrollContent,
1295
1805
  showsVerticalScrollIndicator: false,
1296
- children: message.content?.content?.map((component, index) => /* @__PURE__ */ jsxRuntime.jsx(
1806
+ children: hasDesignedContent ? content.map((component, index) => /* @__PURE__ */ jsxRuntime.jsx(
1297
1807
  PuckComponentRenderer,
1298
1808
  {
1299
1809
  component,
1300
1810
  messageId: message.id,
1301
- options
1811
+ options: {
1812
+ ...options,
1813
+ zones: message.content?.zones,
1814
+ app,
1815
+ onRequestClose: handleDismiss
1816
+ }
1302
1817
  },
1303
1818
  `${message.id}-${index}-${component.type}`
1304
- ))
1819
+ )) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1820
+ message.title ? /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: styles.fallbackTitle, children: message.title }) : null,
1821
+ message.body ? /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: styles.fallbackBody, children: message.body }) : null
1822
+ ] })
1305
1823
  }
1306
1824
  )
1307
1825
  ] }) })
@@ -1317,11 +1835,9 @@ var styles = reactNative.StyleSheet.create({
1317
1835
  },
1318
1836
  card: {
1319
1837
  position: "relative",
1320
- maxWidth: 375,
1321
1838
  width: "90%",
1322
1839
  maxHeight: "80%",
1323
1840
  borderRadius: 16,
1324
- padding: 24,
1325
1841
  shadowColor: "#000",
1326
1842
  shadowOffset: { width: 0, height: 20 },
1327
1843
  shadowOpacity: 0.3,
@@ -1347,6 +1863,19 @@ var styles = reactNative.StyleSheet.create({
1347
1863
  },
1348
1864
  scrollContent: {
1349
1865
  marginTop: 8
1866
+ },
1867
+ // Used only when a message has no designed content, so its title and body
1868
+ // are all there is to show.
1869
+ fallbackTitle: {
1870
+ fontSize: 20,
1871
+ fontWeight: "700",
1872
+ color: "#1B1B1B",
1873
+ marginBottom: 8
1874
+ },
1875
+ fallbackBody: {
1876
+ fontSize: 15,
1877
+ lineHeight: 22,
1878
+ color: "#555555"
1350
1879
  }
1351
1880
  });
1352
1881
  function TolinkuMessages({
@@ -1355,19 +1884,20 @@ function TolinkuMessages({
1355
1884
  onDismiss,
1356
1885
  onButtonPress
1357
1886
  }) {
1358
- const [message, setMessage] = react.useState(null);
1359
- const [visible, setVisible] = react.useState(false);
1360
- const onDismissRef = react.useRef(onDismiss);
1887
+ const [message, setMessage] = React.useState(null);
1888
+ const [visible, setVisible] = React.useState(false);
1889
+ const [app, setApp] = React.useState(null);
1890
+ const onDismissRef = React.useRef(onDismiss);
1361
1891
  onDismissRef.current = onDismiss;
1362
- const onButtonPressRef = react.useRef(onButtonPress);
1892
+ const onButtonPressRef = React.useRef(onButtonPress);
1363
1893
  onButtonPressRef.current = onButtonPress;
1364
- const options = react.useMemo(() => ({
1894
+ const options = React.useMemo(() => ({
1365
1895
  trigger,
1366
1896
  triggerValue,
1367
1897
  onDismiss: (messageId) => onDismissRef.current?.(messageId),
1368
1898
  onButtonPress: (action, messageId) => onButtonPressRef.current?.(action, messageId)
1369
1899
  }), [trigger, triggerValue]);
1370
- react.useEffect(() => {
1900
+ React.useEffect(() => {
1371
1901
  let cancelled = false;
1372
1902
  async function fetchMessages() {
1373
1903
  try {
@@ -1377,7 +1907,9 @@ function TolinkuMessages({
1377
1907
  const userId = Tolinku.getUserId();
1378
1908
  if (userId) params.user_id = userId;
1379
1909
  const data = await client.get("/v1/api/messages", params);
1380
- if (cancelled || !data.messages || data.messages.length === 0) return;
1910
+ if (cancelled) return;
1911
+ if (data.app) setApp(data.app);
1912
+ if (!data.messages || data.messages.length === 0) return;
1381
1913
  const candidates = [];
1382
1914
  for (const m of data.messages) {
1383
1915
  if (triggerValue && m.trigger_value !== triggerValue) continue;
@@ -1401,7 +1933,7 @@ function TolinkuMessages({
1401
1933
  cancelled = true;
1402
1934
  };
1403
1935
  }, [trigger, triggerValue]);
1404
- const handleClose = react.useCallback(() => {
1936
+ const handleClose = React.useCallback(() => {
1405
1937
  setVisible(false);
1406
1938
  setMessage(null);
1407
1939
  }, []);
@@ -1411,7 +1943,8 @@ function TolinkuMessages({
1411
1943
  message,
1412
1944
  visible,
1413
1945
  onClose: handleClose,
1414
- options
1946
+ options,
1947
+ app
1415
1948
  }
1416
1949
  );
1417
1950
  }