@tonconnect/ui 2.0.1-beta.0 → 2.0.1-beta.2

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/lib/index.mjs CHANGED
@@ -1749,7 +1749,32 @@ class TonConnectUIError extends TonConnectError {
1749
1749
  Object.setPrototypeOf(this, TonConnectUIError.prototype);
1750
1750
  }
1751
1751
  }
1752
+ function logDebug(...args) {
1753
+ {
1754
+ try {
1755
+ console.debug("[TON_CONNECT_UI]", ...args);
1756
+ } catch (e2) {
1757
+ }
1758
+ }
1759
+ }
1760
+ function logError(...args) {
1761
+ {
1762
+ try {
1763
+ console.error("[TON_CONNECT_UI]", ...args);
1764
+ } catch (e2) {
1765
+ }
1766
+ }
1767
+ }
1768
+ function logWarning(...args) {
1769
+ {
1770
+ try {
1771
+ console.warn("[TON_CONNECT_UI]", ...args);
1772
+ } catch (e2) {
1773
+ }
1774
+ }
1775
+ }
1752
1776
  function openLink(href, target = "_self") {
1777
+ logDebug("openLink", href, target);
1753
1778
  window.open(href, target, "noopener noreferrer");
1754
1779
  }
1755
1780
  function openLinkBlank(href) {
@@ -1757,7 +1782,7 @@ function openLinkBlank(href) {
1757
1782
  }
1758
1783
  function openDeeplinkWithFallback(href, fallback) {
1759
1784
  const doFallback = () => {
1760
- if (isBrowser("safari")) {
1785
+ if (isBrowser("safari") || isOS("android") && isBrowser("firefox")) {
1761
1786
  return;
1762
1787
  }
1763
1788
  fallback();
@@ -1877,6 +1902,9 @@ function getUserAgent() {
1877
1902
  case (browserName == null ? void 0 : browserName.includes("safari")):
1878
1903
  browser = "safari";
1879
1904
  break;
1905
+ case (browserName == null ? void 0 : browserName.includes("opera")):
1906
+ browser = "opera";
1907
+ break;
1880
1908
  }
1881
1909
  return {
1882
1910
  os,
@@ -2626,22 +2654,6 @@ const Image = (props) => {
2626
2654
  }
2627
2655
  })];
2628
2656
  };
2629
- function logError(...args) {
2630
- {
2631
- try {
2632
- console.error("[TON_CONNECT_UI]", ...args);
2633
- } catch (e2) {
2634
- }
2635
- }
2636
- }
2637
- function logWarning(...args) {
2638
- {
2639
- try {
2640
- console.warn("[TON_CONNECT_UI]", ...args);
2641
- } catch (e2) {
2642
- }
2643
- }
2644
- }
2645
2657
  let initParams = {};
2646
2658
  try {
2647
2659
  let locationHash = location.hash.toString();
@@ -2674,12 +2686,18 @@ function isInTMA() {
2674
2686
  function sendExpand() {
2675
2687
  postEvent("web_app_expand", {});
2676
2688
  }
2677
- function sendOpenTelegramLink(link) {
2689
+ function sendOpenTelegramLink(link, fallback) {
2678
2690
  const url = new URL(link);
2679
2691
  if (url.protocol !== "http:" && url.protocol !== "https:") {
2692
+ if (fallback) {
2693
+ return fallback();
2694
+ }
2680
2695
  throw new TonConnectUIError(`Url protocol is not supported: ${url}`);
2681
2696
  }
2682
2697
  if (url.hostname !== "t.me") {
2698
+ if (fallback) {
2699
+ return fallback();
2700
+ }
2683
2701
  throw new TonConnectUIError(`Url host is not supported: ${url}`);
2684
2702
  }
2685
2703
  const pathFull = url.pathname + url.search;
@@ -2707,12 +2725,15 @@ function postEvent(eventType, eventData) {
2707
2725
  throw new TonConnectUIError(`Can't post event to parent window: window is not defined`);
2708
2726
  }
2709
2727
  if (window2.TelegramWebviewProxy !== void 0) {
2728
+ logDebug("postEvent", eventType, eventData);
2710
2729
  window2.TelegramWebviewProxy.postEvent(eventType, JSON.stringify(eventData));
2711
2730
  } else if (window2.external && "notify" in window2.external) {
2731
+ logDebug("postEvent", eventType, eventData);
2712
2732
  window2.external.notify(JSON.stringify({ eventType, eventData }));
2713
2733
  } else if (isIframe()) {
2714
2734
  const trustedTarget = "*";
2715
2735
  const message = JSON.stringify({ eventType, eventData });
2736
+ logDebug("postEvent", eventType, eventData);
2716
2737
  window2.parent.postMessage(message, trustedTarget);
2717
2738
  } else {
2718
2739
  throw new TonConnectUIError(`Can't post event to TMA`);
@@ -2809,7 +2830,7 @@ function isDevice(device) {
2809
2830
  return width > maxWidth.mobile;
2810
2831
  default:
2811
2832
  case "mobile":
2812
- return width <= maxWidth.mobile;
2833
+ return width <= maxWidth.mobile || isOS("ios", "android");
2813
2834
  }
2814
2835
  }
2815
2836
  function media(device) {
@@ -8309,8 +8330,26 @@ function redirectToTelegram(universalLink, options) {
8309
8330
  }
8310
8331
  } else {
8311
8332
  if (isOS("ios", "android")) {
8312
- options.returnStrategy = "none";
8313
- openLinkBlank(addReturnStrategy(directLinkUrl.toString(), options.returnStrategy));
8333
+ if (options.returnStrategy === "back") {
8334
+ options.returnStrategy = location.href;
8335
+ }
8336
+ const isChrome = isBrowser("chrome");
8337
+ const isFirefox = isBrowser("firefox");
8338
+ const useDeepLink = (isChrome || isFirefox) && !options.forceRedirect;
8339
+ if (useDeepLink) {
8340
+ const linkWithStrategy = addReturnStrategy(
8341
+ directLinkUrl.toString(),
8342
+ options.returnStrategy
8343
+ );
8344
+ const deepLink = convertToTGDeepLink(linkWithStrategy);
8345
+ openDeeplinkWithFallback(deepLink, () => openLinkBlank(linkWithStrategy));
8346
+ } else {
8347
+ const linkWithStrategy = addReturnStrategy(
8348
+ directLinkUrl.toString(),
8349
+ options.returnStrategy
8350
+ );
8351
+ openLinkBlank(linkWithStrategy);
8352
+ }
8314
8353
  } else if (isOS("macos", "windows", "linux")) {
8315
8354
  options.returnStrategy = "none";
8316
8355
  options.twaReturnUrl = void 0;
@@ -8326,6 +8365,160 @@ function redirectToTelegram(universalLink, options) {
8326
8365
  }
8327
8366
  }
8328
8367
  }
8368
+ function redirectToWallet(universalLink, deepLink, options, setOpenMethod) {
8369
+ options = __spreadValues({}, options);
8370
+ if (isInTMA()) {
8371
+ if (isTmaPlatform("ios", "android")) {
8372
+ if (options.returnStrategy === "back") {
8373
+ options.returnStrategy = "tg://resolve";
8374
+ }
8375
+ setOpenMethod("universal-link");
8376
+ const linkWitStrategy = addReturnStrategy(universalLink, options.returnStrategy);
8377
+ sendOpenTelegramLink(linkWitStrategy, () => {
8378
+ setOpenMethod("universal-link");
8379
+ openLinkBlank(linkWitStrategy);
8380
+ });
8381
+ } else if (isTmaPlatform("macos", "tdesktop")) {
8382
+ if (options.returnStrategy === "back") {
8383
+ options.returnStrategy = "tg://resolve";
8384
+ }
8385
+ const linkWitStrategy = addReturnStrategy(universalLink, options.returnStrategy);
8386
+ const useDeepLink = !!deepLink && !options.forceRedirect;
8387
+ if (useDeepLink) {
8388
+ setOpenMethod("custom-deeplink");
8389
+ openDeeplinkWithFallback(toDeeplink(linkWitStrategy, deepLink), () => {
8390
+ setOpenMethod("universal-link");
8391
+ openLinkBlank(linkWitStrategy);
8392
+ });
8393
+ } else {
8394
+ setOpenMethod("universal-link");
8395
+ openLinkBlank(linkWitStrategy);
8396
+ }
8397
+ } else if (isTmaPlatform("weba")) {
8398
+ if (options.returnStrategy === "back") {
8399
+ if (isBrowser("safari")) {
8400
+ options.returnStrategy = location.href;
8401
+ } else if (isBrowser("chrome")) {
8402
+ options.returnStrategy = "googlechrome://";
8403
+ } else if (isBrowser("firefox")) {
8404
+ options.returnStrategy = "firefox://";
8405
+ } else if (isBrowser("opera")) {
8406
+ options.returnStrategy = "opera-http://";
8407
+ } else {
8408
+ options.returnStrategy = location.href;
8409
+ }
8410
+ }
8411
+ const linkWitStrategy = addReturnStrategy(universalLink, options.returnStrategy);
8412
+ const useDeepLink = !!deepLink && !options.forceRedirect;
8413
+ if (useDeepLink) {
8414
+ setOpenMethod("custom-deeplink");
8415
+ openDeeplinkWithFallback(toDeeplink(linkWitStrategy, deepLink), () => {
8416
+ setOpenMethod("universal-link");
8417
+ openLinkBlank(linkWitStrategy);
8418
+ });
8419
+ } else {
8420
+ setOpenMethod("universal-link");
8421
+ openLinkBlank(linkWitStrategy);
8422
+ }
8423
+ } else if (isTmaPlatform("web")) {
8424
+ if (options.returnStrategy === "back") {
8425
+ if (isBrowser("safari")) {
8426
+ options.returnStrategy = location.href;
8427
+ } else if (isBrowser("chrome")) {
8428
+ options.returnStrategy = "googlechrome://";
8429
+ } else if (isBrowser("firefox")) {
8430
+ options.returnStrategy = "firefox://";
8431
+ } else if (isBrowser("opera")) {
8432
+ options.returnStrategy = "opera-http://";
8433
+ } else {
8434
+ options.returnStrategy = location.href;
8435
+ }
8436
+ }
8437
+ const linkWitStrategy = addReturnStrategy(universalLink, options.returnStrategy);
8438
+ const useDeepLink = !!deepLink && !options.forceRedirect;
8439
+ if (useDeepLink) {
8440
+ setOpenMethod("custom-deeplink");
8441
+ openDeeplinkWithFallback(toDeeplink(linkWitStrategy, deepLink), () => {
8442
+ setOpenMethod("universal-link");
8443
+ openLinkBlank(linkWitStrategy);
8444
+ });
8445
+ } else {
8446
+ setOpenMethod("universal-link");
8447
+ openLinkBlank(linkWitStrategy);
8448
+ }
8449
+ } else {
8450
+ setOpenMethod("universal-link");
8451
+ const linkWitStrategy = addReturnStrategy(universalLink, options.returnStrategy);
8452
+ openLinkBlank(linkWitStrategy);
8453
+ }
8454
+ } else {
8455
+ if (isOS("ios")) {
8456
+ if (options.returnStrategy === "back") {
8457
+ if (isBrowser("safari")) {
8458
+ options.returnStrategy = location.href;
8459
+ } else if (isBrowser("chrome")) {
8460
+ options.returnStrategy = "googlechrome://";
8461
+ } else if (isBrowser("firefox")) {
8462
+ options.returnStrategy = "firefox://";
8463
+ } else if (isBrowser("opera")) {
8464
+ options.returnStrategy = "opera-http://";
8465
+ } else {
8466
+ options.returnStrategy = location.href;
8467
+ }
8468
+ }
8469
+ if (isBrowser("chrome")) {
8470
+ setOpenMethod("universal-link");
8471
+ openLink(addReturnStrategy(universalLink, options.returnStrategy), "_self");
8472
+ } else {
8473
+ setOpenMethod("universal-link");
8474
+ openLinkBlank(addReturnStrategy(universalLink, options.returnStrategy));
8475
+ }
8476
+ } else if (isOS("android")) {
8477
+ if (options.returnStrategy === "back") {
8478
+ if (isBrowser("chrome")) {
8479
+ options.returnStrategy = "googlechrome://";
8480
+ } else if (isBrowser("firefox")) {
8481
+ options.returnStrategy = "firefox://";
8482
+ } else if (isBrowser("opera")) {
8483
+ options.returnStrategy = "opera-http://";
8484
+ } else {
8485
+ options.returnStrategy = location.href;
8486
+ }
8487
+ }
8488
+ setOpenMethod("universal-link");
8489
+ openLinkBlank(addReturnStrategy(universalLink, options.returnStrategy));
8490
+ } else if (isOS("macos", "windows", "linux")) {
8491
+ if (options.returnStrategy === "back") {
8492
+ if (isBrowser("safari")) {
8493
+ options.returnStrategy = "none";
8494
+ } else if (isBrowser("chrome")) {
8495
+ options.returnStrategy = "googlechrome://";
8496
+ } else if (isBrowser("firefox")) {
8497
+ options.returnStrategy = "firefox://";
8498
+ } else if (isBrowser("opera")) {
8499
+ options.returnStrategy = "opera-http://";
8500
+ } else {
8501
+ options.returnStrategy = "none";
8502
+ }
8503
+ }
8504
+ const linkWitStrategy = addReturnStrategy(universalLink, options.returnStrategy);
8505
+ const useDeepLink = !!deepLink && !options.forceRedirect;
8506
+ if (useDeepLink) {
8507
+ setOpenMethod("custom-deeplink");
8508
+ openDeeplinkWithFallback(toDeeplink(linkWitStrategy, deepLink), () => {
8509
+ setOpenMethod("universal-link");
8510
+ openLinkBlank(linkWitStrategy);
8511
+ });
8512
+ } else {
8513
+ setOpenMethod("universal-link");
8514
+ openLinkBlank(linkWitStrategy);
8515
+ }
8516
+ } else {
8517
+ setOpenMethod("universal-link");
8518
+ openLinkBlank(addReturnStrategy(universalLink, options.returnStrategy));
8519
+ }
8520
+ }
8521
+ }
8329
8522
  function addQueryParameter(url, key, value) {
8330
8523
  const parsed = new URL(url);
8331
8524
  parsed.searchParams.append(key, value);
@@ -8345,7 +8538,6 @@ function convertToTGDeepLink(directLink) {
8345
8538
  const startapp = parsed.searchParams.get("startapp");
8346
8539
  return `tg://resolve?domain=${domain}&appname=${appname}&startapp=${startapp}`;
8347
8540
  }
8348
- let openDesktopDeeplinkAttempts = 0;
8349
8541
  const DesktopConnectionModal = (props) => {
8350
8542
  const [mode, setMode] = createSignal("mobile");
8351
8543
  const [connectionErrored, setConnectionErrored] = createSignal(false);
@@ -8386,25 +8578,16 @@ const DesktopConnectionModal = (props) => {
8386
8578
  generateUniversalLink();
8387
8579
  }
8388
8580
  setMode("desktop");
8389
- const linkWithStrategy = addReturnStrategy(universalLink(), appState.returnStrategy);
8390
- const haveTriedToOpenDeeplinkInSafari = isBrowser("safari") && openDesktopDeeplinkAttempts >= 1;
8391
- if (props.wallet.deepLink && !haveTriedToOpenDeeplinkInSafari) {
8392
- openDesktopDeeplinkAttempts++;
8393
- setLastSelectedWalletInfo(__spreadProps(__spreadValues({}, props.wallet), {
8394
- openMethod: "custom-deeplink"
8395
- }));
8396
- openDeeplinkWithFallback(toDeeplink(linkWithStrategy, props.wallet.deepLink), () => {
8397
- setLastSelectedWalletInfo(__spreadProps(__spreadValues({}, props.wallet), {
8398
- openMethod: "universal-link"
8399
- }));
8400
- openLinkBlank(linkWithStrategy);
8401
- });
8402
- } else {
8581
+ const forceRedirect = !firstClick();
8582
+ setFirstClick(false);
8583
+ redirectToWallet(universalLink(), props.wallet.deepLink, {
8584
+ returnStrategy: appState.returnStrategy,
8585
+ forceRedirect
8586
+ }, (method) => {
8403
8587
  setLastSelectedWalletInfo(__spreadProps(__spreadValues({}, props.wallet), {
8404
- openMethod: "universal-link"
8588
+ openMethod: method
8405
8589
  }));
8406
- openLinkBlank(linkWithStrategy);
8407
- }
8590
+ });
8408
8591
  };
8409
8592
  const onClickTelegram = () => {
8410
8593
  const forceRedirect = !firstClick();
@@ -8974,6 +9157,7 @@ const MobileConnectionQR = (props) => {
8974
9157
  };
8975
9158
  const MobileConnectionModal = (props) => {
8976
9159
  const theme = useTheme();
9160
+ const [firstClick, setFirstClick] = createSignal(true);
8977
9161
  const [showQR, setShowQR] = createSignal(false);
8978
9162
  const [connectionErrored, setConnectionErrored] = createSignal(false);
8979
9163
  const connector = useContext(ConnectorContext);
@@ -9002,10 +9186,16 @@ const MobileConnectionModal = (props) => {
9002
9186
  return onClickTelegram();
9003
9187
  }
9004
9188
  setConnectionErrored(false);
9005
- setLastSelectedWalletInfo(__spreadProps(__spreadValues({}, props.wallet), {
9006
- openMethod: "universal-link"
9007
- }));
9008
- openLinkBlank(addReturnStrategy(universalLink(), appState.returnStrategy));
9189
+ const forceRedirect = !firstClick();
9190
+ setFirstClick(false);
9191
+ redirectToWallet(universalLink(), props.wallet.deepLink, {
9192
+ returnStrategy: appState.returnStrategy,
9193
+ forceRedirect
9194
+ }, (method) => {
9195
+ setLastSelectedWalletInfo(__spreadProps(__spreadValues({}, props.wallet), {
9196
+ openMethod: method
9197
+ }));
9198
+ });
9009
9199
  };
9010
9200
  const onOpenQR = () => {
9011
9201
  setConnectionErrored(false);
@@ -9369,7 +9559,16 @@ const MobileUniversalModal = (props) => {
9369
9559
  setIsCopiedShown(timeoutId);
9370
9560
  });
9371
9561
  const onSelectUniversal = () => {
9372
- openLinkBlank(addReturnStrategy(getUniversalLink(), appState.returnStrategy));
9562
+ const forceRedirect = !firstClick();
9563
+ setFirstClick(false);
9564
+ redirectToWallet(getUniversalLink(), void 0, {
9565
+ returnStrategy: appState.returnStrategy,
9566
+ forceRedirect
9567
+ }, (method) => {
9568
+ setLastSelectedWalletInfo({
9569
+ openMethod: method
9570
+ });
9571
+ });
9373
9572
  };
9374
9573
  const onSelectTelegram = () => {
9375
9574
  const atWallet = props.walletsList.find((wallet) => wallet.appName === AT_WALLET_APP_NAME);
@@ -9891,19 +10090,27 @@ const ActionModal = (props) => {
9891
10090
  if ((tonConnectUI == null ? void 0 : tonConnectUI.wallet) && "universalLink" in tonConnectUI.wallet && (tonConnectUI.wallet.openMethod === "universal-link" || isTelegramUrl(tonConnectUI.wallet.universalLink) && isInTMA())) {
9892
10091
  universalLink = tonConnectUI.wallet.universalLink;
9893
10092
  }
10093
+ let deepLink;
10094
+ if ((tonConnectUI == null ? void 0 : tonConnectUI.wallet) && "deepLink" in tonConnectUI.wallet && (tonConnectUI.wallet.openMethod === "custom-deeplink" || isTelegramUrl(tonConnectUI.wallet.deepLink) && isInTMA())) {
10095
+ deepLink = tonConnectUI.wallet.deepLink;
10096
+ }
9894
10097
  const onOpenWallet = () => {
9895
10098
  const currentAction = action();
9896
10099
  const returnStrategy = "returnStrategy" in currentAction ? currentAction.returnStrategy : appState.returnStrategy;
10100
+ const forceRedirect = !firstClick();
10101
+ setFirstClick(false);
9897
10102
  if (isTelegramUrl(universalLink)) {
9898
- const forceRedirect = !firstClick();
9899
- setFirstClick(false);
9900
10103
  redirectToTelegram(universalLink, {
9901
10104
  returnStrategy,
9902
10105
  twaReturnUrl: "twaReturnUrl" in currentAction ? currentAction.twaReturnUrl : appState.twaReturnUrl,
9903
10106
  forceRedirect
9904
10107
  });
9905
10108
  } else {
9906
- openLinkBlank(addReturnStrategy(universalLink, returnStrategy));
10109
+ redirectToWallet(universalLink, deepLink, {
10110
+ returnStrategy,
10111
+ forceRedirect
10112
+ }, () => {
10113
+ });
9907
10114
  }
9908
10115
  };
9909
10116
  return createComponent(ActionModalStyled, mergeProps(dataAttrs, {