@tonconnect/ui 2.0.2 → 2.0.3-beta.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/lib/index.cjs CHANGED
@@ -10036,18 +10036,26 @@ const WalletsModal = () => {
10036
10036
  return (_a2 = appState.connectRequestParameters) == null ? void 0 : _a2.value;
10037
10037
  });
10038
10038
  const onClose = (closeReason) => {
10039
- setWalletsModalState({
10040
- status: "closed",
10041
- closeReason
10042
- });
10043
- setSelectedWalletInfo(null);
10044
- setInfoTab(false);
10039
+ tonConnectUI.closeModal(closeReason);
10045
10040
  };
10046
10041
  const unsubscribe = connector.onStatusChange((wallet) => {
10047
10042
  if (wallet) {
10048
10043
  onClose("wallet-selected");
10049
10044
  }
10050
10045
  });
10046
+ const onSelectAllWallets = () => {
10047
+ setSelectedTab("all-wallets");
10048
+ };
10049
+ const onSelectUniversal = () => {
10050
+ setSelectedTab("universal");
10051
+ };
10052
+ const clearSelectedWalletInfo = () => {
10053
+ setSelectedWalletInfo(null);
10054
+ };
10055
+ onCleanup(() => {
10056
+ setSelectedWalletInfo(null);
10057
+ setInfoTab(false);
10058
+ });
10051
10059
  onCleanup(unsubscribe);
10052
10060
  return createComponent(StyledModal, {
10053
10061
  get opened() {
@@ -10112,7 +10120,7 @@ const WalletsModal = () => {
10112
10120
  get additionalRequest() {
10113
10121
  return additionalRequest();
10114
10122
  },
10115
- onBackClick: () => setSelectedWalletInfo(null)
10123
+ onBackClick: clearSelectedWalletInfo
10116
10124
  });
10117
10125
  }
10118
10126
  }), createComponent(Match, {
@@ -10131,7 +10139,7 @@ const WalletsModal = () => {
10131
10139
  get additionalRequest() {
10132
10140
  return additionalRequest();
10133
10141
  },
10134
- onSelectAllWallets: () => setSelectedTab("all-wallets")
10142
+ onSelectAllWallets
10135
10143
  });
10136
10144
  }
10137
10145
  }), createComponent(Match, {
@@ -10143,7 +10151,7 @@ const WalletsModal = () => {
10143
10151
  get walletsList() {
10144
10152
  return walletsList();
10145
10153
  },
10146
- onBack: () => setSelectedTab("universal"),
10154
+ onBack: onSelectUniversal,
10147
10155
  onSelect: setSelectedWalletInfo
10148
10156
  });
10149
10157
  }
@@ -10398,6 +10406,7 @@ const SingleWalletModal = () => {
10398
10406
  }
10399
10407
  });
10400
10408
  const connector = useContext(ConnectorContext);
10409
+ const tonConnectUI = useContext(TonConnectUiContext);
10401
10410
  const [infoTab, setInfoTab] = createSignal(false);
10402
10411
  const additionalRequestLoading = () => {
10403
10412
  var _a2;
@@ -10411,11 +10420,7 @@ const SingleWalletModal = () => {
10411
10420
  return (_a2 = appState.connectRequestParameters) == null ? void 0 : _a2.value;
10412
10421
  });
10413
10422
  const onClose = (closeReason) => {
10414
- setSingleWalletModalState({
10415
- status: "closed",
10416
- closeReason
10417
- });
10418
- setInfoTab(false);
10423
+ tonConnectUI.closeSingleWalletModal(closeReason);
10419
10424
  };
10420
10425
  const unsubscribe = connector.onStatusChange((wallet) => {
10421
10426
  if (wallet) {
@@ -10423,6 +10428,9 @@ const SingleWalletModal = () => {
10423
10428
  }
10424
10429
  });
10425
10430
  onCleanup(unsubscribe);
10431
+ onCleanup(() => {
10432
+ setInfoTab(false);
10433
+ });
10426
10434
  return createComponent(StyledModal, {
10427
10435
  get opened() {
10428
10436
  return getSingleWalletModalIsOpened();
@@ -10572,8 +10580,10 @@ class WalletsModalManager {
10572
10580
  __publicField(this, "connector");
10573
10581
  __publicField(this, "setConnectRequestParametersCallback");
10574
10582
  __publicField(this, "consumers", []);
10583
+ __publicField(this, "tracker");
10575
10584
  __publicField(this, "state", walletsModalState());
10576
10585
  this.connector = options.connector;
10586
+ this.tracker = options.tracker;
10577
10587
  this.setConnectRequestParametersCallback = options.setConnectRequestParametersCallback;
10578
10588
  createEffect(() => {
10579
10589
  const state = walletsModalState();
@@ -10583,6 +10593,7 @@ class WalletsModalManager {
10583
10593
  }
10584
10594
  open() {
10585
10595
  return __async(this, null, function* () {
10596
+ this.tracker.trackConnectionStarted();
10586
10597
  const walletsList = yield this.connector.getWallets();
10587
10598
  const embeddedWallet = walletsList.find(sdk.isWalletInfoCurrentlyEmbedded);
10588
10599
  if (embeddedWallet) {
@@ -10592,8 +10603,11 @@ class WalletsModalManager {
10592
10603
  }
10593
10604
  });
10594
10605
  }
10595
- close() {
10596
- widgetController.closeWalletsModal("action-cancelled");
10606
+ close(reason = "action-cancelled") {
10607
+ if (reason === "action-cancelled") {
10608
+ this.tracker.trackConnectionError("Connection was cancelled");
10609
+ }
10610
+ widgetController.closeWalletsModal(reason);
10597
10611
  }
10598
10612
  onStateChange(onChange) {
10599
10613
  this.consumers.push(onChange);
@@ -10653,8 +10667,10 @@ class SingleWalletModalManager {
10653
10667
  __publicField(this, "connector");
10654
10668
  __publicField(this, "setConnectRequestParametersCallback");
10655
10669
  __publicField(this, "consumers", []);
10670
+ __publicField(this, "tracker");
10656
10671
  __publicField(this, "state", singleWalletModalState());
10657
10672
  this.connector = options.connector;
10673
+ this.tracker = options.tracker;
10658
10674
  this.setConnectRequestParametersCallback = options.setConnectRequestParametersCallback;
10659
10675
  createEffect(() => {
10660
10676
  const state = singleWalletModalState();
@@ -10664,6 +10680,7 @@ class SingleWalletModalManager {
10664
10680
  }
10665
10681
  open(wallet) {
10666
10682
  return __async(this, null, function* () {
10683
+ this.tracker.trackConnectionStarted();
10667
10684
  const fetchedWalletsList = yield this.connector.getWallets();
10668
10685
  const walletsList = applyWalletsListConfiguration(
10669
10686
  fetchedWalletsList,
@@ -10680,10 +10697,15 @@ class SingleWalletModalManager {
10680
10697
  if (isExternalWalletExist) {
10681
10698
  return this.openSingleWalletModal(externalWallet);
10682
10699
  }
10683
- throw new TonConnectUIError(`Trying to open modal window with unknown wallet "${wallet}".`);
10700
+ const error = `Trying to open modal window with unknown wallet "${wallet}".`;
10701
+ this.tracker.trackConnectionError(error);
10702
+ throw new TonConnectUIError(error);
10684
10703
  });
10685
10704
  }
10686
- close() {
10705
+ close(reason = "action-cancelled") {
10706
+ if (reason === "action-cancelled") {
10707
+ this.tracker.trackConnectionError("Connection was cancelled");
10708
+ }
10687
10709
  widgetController.closeSingleWalletModal("action-cancelled");
10688
10710
  }
10689
10711
  onStateChange(onChange) {
@@ -10722,10 +10744,96 @@ class SingleWalletModalManager {
10722
10744
  });
10723
10745
  }
10724
10746
  }
10747
+ class TonConnectUITracker {
10748
+ constructor(eventDispatcher) {
10749
+ __publicField(this, "eventPrefix", "ton-connect-ui-");
10750
+ __publicField(this, "eventDispatcher");
10751
+ this.eventDispatcher = eventDispatcher != null ? eventDispatcher : new sdk.BrowserEventDispatcher();
10752
+ }
10753
+ dispatchUserActionEvent(eventDetails) {
10754
+ var _a2;
10755
+ try {
10756
+ const eventName = `${this.eventPrefix}${eventDetails.type}`;
10757
+ (_a2 = this.eventDispatcher) == null ? void 0 : _a2.dispatchEvent(eventName, eventDetails).catch();
10758
+ } catch (e2) {
10759
+ }
10760
+ }
10761
+ trackConnectionStarted(...args) {
10762
+ try {
10763
+ const event = sdk.createConnectionStartedEvent(...args);
10764
+ this.dispatchUserActionEvent(event);
10765
+ } catch (e2) {
10766
+ }
10767
+ }
10768
+ trackConnectionCompleted(...args) {
10769
+ try {
10770
+ const event = sdk.createConnectionCompletedEvent(...args);
10771
+ this.dispatchUserActionEvent(event);
10772
+ } catch (e2) {
10773
+ }
10774
+ }
10775
+ trackConnectionError(...args) {
10776
+ try {
10777
+ const event = sdk.createConnectionErrorEvent(...args);
10778
+ this.dispatchUserActionEvent(event);
10779
+ } catch (e2) {
10780
+ }
10781
+ }
10782
+ trackConnectionRestoringStarted(...args) {
10783
+ try {
10784
+ const event = sdk.createConnectionRestoringStartedEvent(...args);
10785
+ this.dispatchUserActionEvent(event);
10786
+ } catch (e2) {
10787
+ }
10788
+ }
10789
+ trackConnectionRestoringCompleted(...args) {
10790
+ try {
10791
+ const event = sdk.createConnectionRestoringCompletedEvent(...args);
10792
+ this.dispatchUserActionEvent(event);
10793
+ } catch (e2) {
10794
+ }
10795
+ }
10796
+ trackConnectionRestoringError(...args) {
10797
+ try {
10798
+ const event = sdk.createConnectionRestoringErrorEvent(...args);
10799
+ this.dispatchUserActionEvent(event);
10800
+ } catch (e2) {
10801
+ }
10802
+ }
10803
+ trackDisconnection(...args) {
10804
+ try {
10805
+ const event = sdk.createDisconnectionEvent(...args);
10806
+ this.dispatchUserActionEvent(event);
10807
+ } catch (e2) {
10808
+ }
10809
+ }
10810
+ trackTransactionSentForSignature(...args) {
10811
+ try {
10812
+ const event = sdk.createTransactionSentForSignatureEvent(...args);
10813
+ this.dispatchUserActionEvent(event);
10814
+ } catch (e2) {
10815
+ }
10816
+ }
10817
+ trackTransactionSigned(...args) {
10818
+ try {
10819
+ const event = sdk.createTransactionSignedEvent(...args);
10820
+ this.dispatchUserActionEvent(event);
10821
+ } catch (e2) {
10822
+ }
10823
+ }
10824
+ trackTransactionSigningFailed(...args) {
10825
+ try {
10826
+ const event = sdk.createTransactionSigningFailedEvent(...args);
10827
+ this.dispatchUserActionEvent(event);
10828
+ } catch (e2) {
10829
+ }
10830
+ }
10831
+ }
10725
10832
  class TonConnectUI {
10726
10833
  constructor(options) {
10727
10834
  __publicField(this, "walletInfoStorage", new WalletInfoStorage());
10728
10835
  __publicField(this, "preferredWalletStorage", new PreferredWalletStorage());
10836
+ __publicField(this, "tracker");
10729
10837
  __publicField(this, "walletInfo", null);
10730
10838
  __publicField(this, "systemThemeChangeUnsubscribe", null);
10731
10839
  __publicField(this, "actionsConfiguration");
@@ -10739,20 +10847,26 @@ class TonConnectUI {
10739
10847
  if (options && "connector" in options && options.connector) {
10740
10848
  this.connector = options.connector;
10741
10849
  } else if (options && "manifestUrl" in options && options.manifestUrl) {
10742
- this.connector = new sdk.TonConnect({ manifestUrl: options.manifestUrl });
10850
+ this.connector = new sdk.TonConnect({
10851
+ manifestUrl: options.manifestUrl,
10852
+ eventDispatcher: options == null ? void 0 : options.eventDispatcher
10853
+ });
10743
10854
  } else {
10744
10855
  throw new TonConnectUIError(
10745
10856
  "You have to specify a `manifestUrl` or a `connector` in the options."
10746
10857
  );
10747
10858
  }
10859
+ this.tracker = new TonConnectUITracker(options == null ? void 0 : options.eventDispatcher);
10748
10860
  this.modal = new WalletsModalManager({
10749
10861
  connector: this.connector,
10862
+ tracker: this.tracker,
10750
10863
  setConnectRequestParametersCallback: (callback) => {
10751
10864
  this.connectRequestParametersCallback = callback;
10752
10865
  }
10753
10866
  });
10754
10867
  this.singleWalletModal = new SingleWalletModalManager({
10755
10868
  connector: this.connector,
10869
+ tracker: this.tracker,
10756
10870
  setConnectRequestParametersCallback: (callback) => {
10757
10871
  this.connectRequestParametersCallback = callback;
10758
10872
  }
@@ -10765,10 +10879,14 @@ class TonConnectUI {
10765
10879
  const rootId = this.normalizeWidgetRoot(options == null ? void 0 : options.widgetRootId);
10766
10880
  this.subscribeToWalletChange();
10767
10881
  if ((options == null ? void 0 : options.restoreConnection) !== false) {
10882
+ this.tracker.trackConnectionRestoringStarted();
10768
10883
  this.connectionRestored = new Promise((resolve) => __async(this, null, function* () {
10769
10884
  yield this.connector.restoreConnection();
10770
10885
  if (!this.connector.connected) {
10886
+ this.tracker.trackConnectionRestoringError("Connection was not restored");
10771
10887
  this.walletInfoStorage.removeWalletInfo();
10888
+ } else {
10889
+ this.tracker.trackConnectionRestoringCompleted(this.wallet);
10772
10890
  }
10773
10891
  resolve(this.connector.connected);
10774
10892
  }));
@@ -10866,8 +10984,8 @@ class TonConnectUI {
10866
10984
  return this.modal.open();
10867
10985
  });
10868
10986
  }
10869
- closeModal() {
10870
- this.modal.close();
10987
+ closeModal(reason) {
10988
+ this.modal.close(reason);
10871
10989
  }
10872
10990
  onModalStateChange(onChange) {
10873
10991
  return this.modal.onStateChange(onChange);
@@ -10880,8 +10998,8 @@ class TonConnectUI {
10880
10998
  return this.singleWalletModal.open(wallet);
10881
10999
  });
10882
11000
  }
10883
- closeSingleWalletModal() {
10884
- this.singleWalletModal.close();
11001
+ closeSingleWalletModal(closeReason) {
11002
+ this.singleWalletModal.close(closeReason);
10885
11003
  }
10886
11004
  onSingleWalletModalStateChange(onChange) {
10887
11005
  return this.singleWalletModal.onStateChange(onChange);
@@ -10901,6 +11019,7 @@ class TonConnectUI {
10901
11019
  });
10902
11020
  }
10903
11021
  disconnect() {
11022
+ this.tracker.trackDisconnection(this.wallet, "dapp");
10904
11023
  widgetController.clearAction();
10905
11024
  widgetController.removeSelectedWalletInfo();
10906
11025
  this.walletInfoStorage.removeWalletInfo();
@@ -10908,7 +11027,9 @@ class TonConnectUI {
10908
11027
  }
10909
11028
  sendTransaction(tx, options) {
10910
11029
  return __async(this, null, function* () {
11030
+ this.tracker.trackTransactionSentForSignature(this.wallet, tx);
10911
11031
  if (!this.connected) {
11032
+ this.tracker.trackTransactionSigningFailed(this.wallet, tx, "Wallet was not connected");
10912
11033
  throw new TonConnectUIError("Connect wallet to send a transaction.");
10913
11034
  }
10914
11035
  if (isInTMA()) {
@@ -10970,6 +11091,7 @@ class TonConnectUI {
10970
11091
  },
10971
11092
  onRequestSent
10972
11093
  );
11094
+ this.tracker.trackTransactionSigned(this.wallet, tx, result);
10973
11095
  widgetController.setAction({
10974
11096
  name: "transaction-sent",
10975
11097
  showNotification: notifications2.includes("success"),
@@ -10977,6 +11099,7 @@ class TonConnectUI {
10977
11099
  });
10978
11100
  return result;
10979
11101
  } catch (e2) {
11102
+ this.tracker.trackTransactionSigningFailed(this.wallet, tx, e2.message);
10980
11103
  widgetController.setAction({
10981
11104
  name: "transaction-canceled",
10982
11105
  showNotification: notifications2.includes("error"),
@@ -11033,23 +11156,28 @@ class TonConnectUI {
11033
11156
  waitForWalletConnection(options) {
11034
11157
  return __async(this, null, function* () {
11035
11158
  return new Promise((resolve, reject) => {
11159
+ this.tracker.trackConnectionStarted();
11036
11160
  const { ignoreErrors = false, signal = null } = options;
11037
11161
  if (signal && signal.aborted) {
11162
+ this.tracker.trackConnectionError("Connection was cancelled");
11038
11163
  return reject(new TonConnectUIError("Wallet was not connected"));
11039
11164
  }
11040
11165
  const onStatusChangeHandler = (wallet) => __async(this, null, function* () {
11041
11166
  if (!wallet) {
11167
+ this.tracker.trackConnectionError("Connection was cancelled");
11042
11168
  if (ignoreErrors) {
11043
11169
  return;
11044
11170
  }
11045
11171
  unsubscribe();
11046
11172
  reject(new TonConnectUIError("Wallet was not connected"));
11047
11173
  } else {
11174
+ this.tracker.trackConnectionCompleted(wallet);
11048
11175
  unsubscribe();
11049
11176
  resolve(wallet);
11050
11177
  }
11051
11178
  });
11052
11179
  const onErrorsHandler = (reason) => {
11180
+ this.tracker.trackConnectionError(reason.message);
11053
11181
  if (ignoreErrors) {
11054
11182
  return;
11055
11183
  }