@rabbitio/ui-kit 1.0.0-beta.14 → 1.0.0-beta.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabbitio/ui-kit",
3
- "version": "1.0.0-beta.14",
3
+ "version": "1.0.0-beta.16",
4
4
  "description": "Rabbit.io react.js components kit",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -0,0 +1,26 @@
1
+ import { useCallback, useState } from "react";
2
+
3
+ import { Logger } from "../../common/utils/logging/logger.js";
4
+
5
+ export function useCallHandlingErrors() {
6
+ const [, setState] = useState();
7
+ return useCallback(
8
+ async (functionToBeCalled, event) => {
9
+ try {
10
+ await functionToBeCalled(event);
11
+ } catch (error) {
12
+ Logger.logError(
13
+ error,
14
+ functionToBeCalled?.name || "errorBoundaryTrigger",
15
+ "Caught by ErrorBoundary"
16
+ );
17
+ // Triggering ErrorBoundary
18
+ setState(() => {
19
+ throw error;
20
+ });
21
+ }
22
+ },
23
+
24
+ []
25
+ );
26
+ }
@@ -0,0 +1,24 @@
1
+ import React from "react";
2
+
3
+ /**
4
+ * Adds reference to standard state variable. It is helpful to be able to use state variable value inside
5
+ * event handlers and other callbacks without the need to call setState(prev => { value = prev; return prev; }).
6
+ *
7
+ * @param initialValue {any} to be passed to useState
8
+ * @return {[React.Ref, function]} reference to state variable and its setter
9
+ */
10
+ export function useReferredState(initialValue) {
11
+ const [state, setState] = React.useState(initialValue);
12
+ const reference = React.useRef(state);
13
+
14
+ const setReferredState = (value) => {
15
+ if (value && {}.toString.call(value) === "[object Function]") {
16
+ value = value(reference.current);
17
+ }
18
+
19
+ reference.current = value;
20
+ setState(value);
21
+ };
22
+
23
+ return [reference, setReferredState];
24
+ }
package/src/index.js CHANGED
@@ -4,6 +4,9 @@ export { LoadingDots } from "./components/atoms/LoadingDots/LoadingDots.jsx";
4
4
  export { SupportChat } from "./components/atoms/SupportChat/SupportChat.jsx";
5
5
  export { AssetIcon } from "./components/atoms/AssetIcon/AssetIcon.jsx";
6
6
 
7
+ export { useCallHandlingErrors } from "./components/hooks/useCallHandlingErrors.js";
8
+ export { useReferredState } from "./components/hooks/useReferredState.js";
9
+
7
10
  // Common code lib (to be extracted later to dedicated lib)
8
11
  export { improveAndRethrow } from "./common/errorUtils.js";
9
12
  export { FiatCurrenciesService } from "./common/fiatCurrenciesService.js";
@@ -23,7 +26,7 @@ export { EmailsApi } from "./common/utils/emailAPI.js";
23
26
  // Swaps lib (to be extracted later to dedicated lib)
24
27
  export { ExistingSwap } from "./swaps-lib/models/existingSwap.js";
25
28
  export { ExistingSwapWithFiatData } from "./swaps-lib/models/existingSwapWithFiatData.js";
26
- export { PublicSwapCreationInfo } from "./swaps-lib/models/publicSwapCreationInfo.js";
29
+ export { BaseSwapCreationInfo } from "./swaps-lib/models/baseSwapCreationInfo.js";
27
30
  export { SwapProvider } from "./swaps-lib/external-apis/swapProvider.js";
28
31
  export { SwapspaceSwapProvider } from "./swaps-lib/external-apis/swapspaceSwapProvider.js";
29
32
  export { SwapUtils } from "./swaps-lib/utils/swapUtils.js";
@@ -1,4 +1,4 @@
1
- export class PublicSwapCreationInfo {
1
+ export class BaseSwapCreationInfo {
2
2
  /**
3
3
  * @param fromCoin {Coin}
4
4
  * @param toCoin {Coin}
@@ -1,33 +1,19 @@
1
1
  import { BigNumber } from "bignumber.js";
2
- import EventBusInstance from "eventbusjs";
3
2
 
4
- import { Cache } from "../../common/utils/cache.js";
5
3
  import { FiatCurrenciesService } from "../../common/fiatCurrenciesService.js";
6
4
  import { improveAndRethrow } from "../../common/errorUtils.js";
7
5
  import { safeStringify } from "../../common/utils/safeStringify.js";
8
6
  import { Logger } from "../../common/utils/logging/logger.js";
9
7
  import { Coin } from "../../common/models/coin.js";
10
8
  import { AmountUtils } from "../../common/amountUtils.js";
11
- import { PublicSwapCreationInfo } from "../models/publicSwapCreationInfo.js";
9
+ import { BaseSwapCreationInfo } from "../models/baseSwapCreationInfo.js";
12
10
  import { SwapUtils } from "../utils/swapUtils.js";
13
11
  import { SwapspaceSwapProvider } from "../external-apis/swapspaceSwapProvider.js";
14
12
  import { SwapProvider } from "../external-apis/swapProvider.js";
15
13
 
16
- const API_KEYS_PROXY_URL = `${
17
- window.location.protocol + "//" + window.location.host
18
- }/api/v1/proxy`;
19
- const cache = new Cache(EventBusInstance);
20
-
21
14
  export class PublicSwapService {
22
15
  static PUBLIC_SWAP_CREATED_EVENT = "publicSwapCreatedEvent";
23
16
 
24
- static _swapProvider = new SwapspaceSwapProvider(
25
- API_KEYS_PROXY_URL,
26
- cache,
27
- () => null,
28
- false
29
- );
30
-
31
17
  static PUBLIC_SWAPS_COMMON_ERRORS = {
32
18
  REQUESTS_LIMIT_EXCEEDED: "requestsLimitExceeded",
33
19
  };
@@ -41,7 +27,16 @@ export class PublicSwapService {
41
27
  static _fiatDecimalsCount =
42
28
  FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
43
29
 
44
- static async initialize() {
30
+ constructor(API_KEYS_PROXY_URL, cache) {
31
+ this._swapProvider = new SwapspaceSwapProvider(
32
+ API_KEYS_PROXY_URL,
33
+ cache,
34
+ () => null,
35
+ false
36
+ );
37
+ }
38
+
39
+ async initialize() {
45
40
  try {
46
41
  await this._swapProvider.initialize();
47
42
  SwapUtils.safeHandleRequestsLimitExceeding();
@@ -50,9 +45,7 @@ export class PublicSwapService {
50
45
  }
51
46
  }
52
47
 
53
- static async getCurrenciesListForPublicSwap(
54
- currencyThatShouldNotBeFirst = null
55
- ) {
48
+ async getCurrenciesListForPublicSwap(currencyThatShouldNotBeFirst = null) {
56
49
  const loggerSource = "getCurrenciesListForPublicSwap";
57
50
  try {
58
51
  // TODO: [dev] this is temporary hack, change it to use dedicated lists inside UI and also here
@@ -107,7 +100,7 @@ export class PublicSwapService {
107
100
  * reason: string
108
101
  * }>}
109
102
  */
110
- static async getInitialPublicSwapData(fromCoin, toCoin) {
103
+ async getInitialPublicSwapData(fromCoin, toCoin) {
111
104
  try {
112
105
  const result = await SwapUtils.getInitialSwapData(
113
106
  this._swapProvider,
@@ -159,10 +152,10 @@ export class PublicSwapService {
159
152
  * fiatMax: (number|null)
160
153
  * }|{
161
154
  * result: true,
162
- * swapCreationInfo: PublicSwapCreationInfo
155
+ * swapCreationInfo: BaseSwapCreationInfo
163
156
  * }>}
164
157
  */
165
- static async getPublicSwapDetails(fromCoin, toCoin, fromAmountCoins) {
158
+ async getPublicSwapDetails(fromCoin, toCoin, fromAmountCoins) {
166
159
  const loggerSource = "getPublicSwapDetails";
167
160
  try {
168
161
  const coinUsdtRate =
@@ -239,7 +232,7 @@ export class PublicSwapService {
239
232
  );
240
233
  const result = {
241
234
  result: true,
242
- swapCreationInfo: new PublicSwapCreationInfo(
235
+ swapCreationInfo: new BaseSwapCreationInfo(
243
236
  fromCoin,
244
237
  toCoin,
245
238
  fromAmountCoins,
@@ -277,7 +270,7 @@ export class PublicSwapService {
277
270
  * @param fromCoin {Coin}
278
271
  * @param toCoin {Coin}
279
272
  * @param fromAmount {string}
280
- * @param swapCreationInfo {PublicSwapCreationInfo}
273
+ * @param swapCreationInfo {BaseSwapCreationInfo}
281
274
  * @param toAddress {string}
282
275
  * @param refundAddress {string}
283
276
  * @return {Promise<{
@@ -299,7 +292,7 @@ export class PublicSwapService {
299
292
  * reason: string
300
293
  * }>}
301
294
  */
302
- static async createPublicSwap(
295
+ async createPublicSwap(
303
296
  fromCoin,
304
297
  toCoin,
305
298
  fromAmount,
@@ -316,7 +309,7 @@ export class PublicSwapService {
316
309
  typeof fromAmount !== "string" ||
317
310
  typeof toAddress !== "string" ||
318
311
  typeof refundAddress !== "string" ||
319
- !(swapCreationInfo instanceof PublicSwapCreationInfo)
312
+ !(swapCreationInfo instanceof BaseSwapCreationInfo)
320
313
  ) {
321
314
  throw new Error(
322
315
  `Wrong input: ${fromCoin.ticker} ${toCoin.ticker} ${fromAmount} ${swapCreationInfo}`
@@ -459,7 +452,7 @@ export class PublicSwapService {
459
452
  * }>}
460
453
  * error reason is one of PUBLIC_SWAPS_COMMON_ERRORS
461
454
  */
462
- static async getPublicExistingSwapDetailsAndStatus(swapIds) {
455
+ async getPublicExistingSwapDetailsAndStatus(swapIds) {
463
456
  const loggerSource = "getPublicExistingSwapDetailsAndStatus";
464
457
  try {
465
458
  const result =
@@ -499,7 +492,7 @@ export class PublicSwapService {
499
492
  * reason: string
500
493
  * }>}
501
494
  */
502
- static async getPublicSwapsHistory() {
495
+ async getPublicSwapsHistory() {
503
496
  try {
504
497
  const swapIds = this._getPublicSwapIdsSavedLocally();
505
498
  if (swapIds.length) {
@@ -517,7 +510,7 @@ export class PublicSwapService {
517
510
  * @param swapId {string}
518
511
  * @private
519
512
  */
520
- static _savePublicSwapIdLocally(swapId) {
513
+ _savePublicSwapIdLocally(swapId) {
521
514
  if (typeof window !== "undefined") {
522
515
  try {
523
516
  const saved = localStorage.getItem("publicSwapIds");
@@ -537,7 +530,7 @@ export class PublicSwapService {
537
530
  * @private
538
531
  * @return {string[]}
539
532
  */
540
- static _getPublicSwapIdsSavedLocally() {
533
+ _getPublicSwapIdsSavedLocally() {
541
534
  if (typeof window !== "undefined") {
542
535
  try {
543
536
  const saved = localStorage.getItem("publicSwapIds");
@@ -554,7 +547,7 @@ export class PublicSwapService {
554
547
  * @param coinOrTicker {Coin|string}
555
548
  * @return {string} icon URL (ready to use)
556
549
  */
557
- static getAssetIconUrl(coinOrTicker) {
550
+ getAssetIconUrl(coinOrTicker) {
558
551
  return this._swapProvider.getIconUrl(coinOrTicker);
559
552
  }
560
553
 
@@ -562,7 +555,7 @@ export class PublicSwapService {
562
555
  * @param ticker {string}
563
556
  * @return {Coin|null}
564
557
  */
565
- static getCoinByTickerIfPresent(ticker) {
558
+ getCoinByTickerIfPresent(ticker) {
566
559
  return this._swapProvider.getCoinByTickerIfPresent(ticker);
567
560
  }
568
561
 
@@ -571,7 +564,7 @@ export class PublicSwapService {
571
564
  * @param asset {Coin}
572
565
  * @return {Promise<string|null>}
573
566
  */
574
- static async getAssetToUsdtRate(asset) {
567
+ async getAssetToUsdtRate(asset) {
575
568
  try {
576
569
  const result = await this._swapProvider.getCoinToUSDTRate(asset);
577
570
  return result?.rate ?? null;
@@ -585,49 +578,11 @@ export class PublicSwapService {
585
578
  * @param address {string}
586
579
  * @return {boolean}
587
580
  */
588
- static isAddressValidForAsset(asset, address) {
581
+ isAddressValidForAsset(asset, address) {
589
582
  try {
590
583
  return this._swapProvider.isAddressValidForAsset(asset, address);
591
584
  } catch (e) {
592
585
  improveAndRethrow(e, "isAddressValidForAsset");
593
586
  }
594
587
  }
595
-
596
- // TODO: [dev] Remove if we don't need this inside the public swap steps
597
- // /**
598
- // * TODO: [feature, moderate] add other fiat currencies support. task_id=5490e21b8b9c4f89a2247b28db3c9e0a
599
- // * @param asset {Coin}
600
- // * @param amount {string}
601
- // * @return {Promise<string|null>}
602
- // */
603
- // static async convertSingleCoinAmountToUsdtOrNull(asset, amount) {
604
- // try {
605
- // const result = await this._swapProvider.getCoinToUSDTRate(asset);
606
- // if (result?.rate != null) {
607
- // const decimals = FiatCurrenciesService.getCurrencyDecimalCountByCode("USD");
608
- // return BigNumber(amount).div(result?.rate).toFixed(decimals);
609
- // }
610
- // return null;
611
- // } catch (e) {
612
- // improveAndRethrow(e, "convertSingleCoinAmountToUsdtOrNull");
613
- // }
614
- // }
615
- //
616
- // /**
617
- // * TODO: [feature, moderate] add other fiat currencies support. task_id=5490e21b8b9c4f89a2247b28db3c9e0a
618
- // * @param asset {Coin}
619
- // * @param amount {string}
620
- // * @return {Promise<string|null>}
621
- // */
622
- // static async convertSingleUsdtAmountToCoinOrNull(asset, amount) {
623
- // try {
624
- // const result = await this._swapProvider.getCoinToUSDTRate(asset);
625
- // if (result?.rate != null) {
626
- // return BigNumber(amount).times(result?.rate).toFixed(asset.digits);
627
- // }
628
- // return null;
629
- // } catch (e) {
630
- // improveAndRethrow(e, "convertSingleUsdtAmountToCoinOrNull");
631
- // }
632
- // }
633
588
  }