@rabbitio/ui-kit 1.0.0-beta.15 → 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/dist/index.cjs CHANGED
@@ -1479,6 +1479,198 @@ AssetIcon.defaultProps = {
1479
1479
  small: false
1480
1480
  };
1481
1481
 
1482
+ var LogsStorage = /*#__PURE__*/function () {
1483
+ function LogsStorage() {}
1484
+ LogsStorage.saveLog = function saveLog(log) {
1485
+ this._inMemoryStorage.push(log);
1486
+ };
1487
+ LogsStorage.getInMemoryLogs = function getInMemoryLogs() {
1488
+ return this._inMemoryStorage;
1489
+ };
1490
+ LogsStorage.getAllLogs = function getAllLogs() {
1491
+ var storedLogs = "";
1492
+ if (typeof window !== "undefined") {
1493
+ storedLogs = localStorage.getItem(this._logsStorageId);
1494
+ }
1495
+ return storedLogs + "\n" + this._inMemoryStorage.join("\n");
1496
+ }
1497
+
1498
+ /**
1499
+ * @param logger {Logger}
1500
+ */;
1501
+ LogsStorage.saveToTheDisk = function saveToTheDisk(logger) {
1502
+ try {
1503
+ var MAX_LOCAL_STORAGE_VOLUME_BYTES = 5 * 1024 * 1024;
1504
+ var MAX_LOGS_STORAGE_BYTES = MAX_LOCAL_STORAGE_VOLUME_BYTES * 0.65;
1505
+ if (typeof window !== "undefined") {
1506
+ var existingLogs = localStorage.getItem(this._logsStorageId);
1507
+ var logsString = existingLogs + "\n" + this._inMemoryStorage.join("\n");
1508
+ var lettersCountToRemove = logsString.length - Math.round(MAX_LOGS_STORAGE_BYTES / 2);
1509
+ if (lettersCountToRemove > 0) {
1510
+ localStorage.setItem(this._logsStorageId, logsString.slice(lettersCountToRemove, logsString.length));
1511
+ } else {
1512
+ localStorage.setItem(this._logsStorageId, logsString);
1513
+ }
1514
+ this._inMemoryStorage = [];
1515
+ }
1516
+ } catch (e) {
1517
+ logger == null || logger.logError(e, "saveToTheDisk", "Failed to save logs to disk");
1518
+ }
1519
+ };
1520
+ LogsStorage.removeAllClientLogs = function removeAllClientLogs() {
1521
+ if (typeof window !== "undefined") {
1522
+ if (localStorage.getItem("doNotRemoveClientLogsWhenSignedOut") !== "true") {
1523
+ localStorage.removeItem(this._logsStorageId);
1524
+ }
1525
+ }
1526
+ this._inMemoryStorage = [];
1527
+ };
1528
+ LogsStorage.setDoNotRemoveClientLogsWhenSignedOut = function setDoNotRemoveClientLogsWhenSignedOut(value) {
1529
+ if (typeof window !== "undefined") {
1530
+ localStorage.setItem("doNotRemoveClientLogsWhenSignedOut", value);
1531
+ }
1532
+ };
1533
+ return LogsStorage;
1534
+ }();
1535
+ LogsStorage._inMemoryStorage = [];
1536
+ LogsStorage._logsStorageId = "clietnLogs_j203fj2D0n-d1";
1537
+
1538
+ /**
1539
+ * Stringify given object by use of JSON.stringify but handles circular structures and "response", "request" properties
1540
+ * to avoid stringing redundant data when printing errors containing request/response objects.
1541
+ *
1542
+ * @param object - object to be stringed
1543
+ * @param indent - custom indentation
1544
+ * @return {string} - stringed object
1545
+ */
1546
+ function safeStringify(object, indent) {
1547
+ if (indent === void 0) {
1548
+ indent = 2;
1549
+ }
1550
+ var cache = [];
1551
+ if (typeof object === "string" || typeof object === "function" || typeof object === "number" || typeof object === "undefined" || typeof object === "boolean") {
1552
+ return String(object);
1553
+ }
1554
+ var retVal = JSON.stringify(object, function (key, value) {
1555
+ if (key.toLowerCase().includes("request")) {
1556
+ return JSON.stringify({
1557
+ body: value == null ? void 0 : value.body,
1558
+ query: value == null ? void 0 : value.query,
1559
+ headers: value == null ? void 0 : value.headers
1560
+ });
1561
+ }
1562
+ if (key.toLowerCase().includes("response")) {
1563
+ return JSON.stringify({
1564
+ statusText: value == null ? void 0 : value.statusText,
1565
+ status: value == null ? void 0 : value.status,
1566
+ data: value == null ? void 0 : value.data,
1567
+ headers: value == null ? void 0 : value.headers
1568
+ });
1569
+ }
1570
+ return typeof value === "object" && value !== null ? cache.includes(value) ? "duplicated reference" // Duplicated references were found, discarding this key
1571
+ : cache.push(value) && value // Store value in our collection
1572
+ : value;
1573
+ }, indent);
1574
+ cache = null;
1575
+ return retVal;
1576
+ }
1577
+
1578
+ var Logger = /*#__PURE__*/function () {
1579
+ function Logger() {}
1580
+ /**
1581
+ * Logs to client logs storage.
1582
+ *
1583
+ * WARNING! this method should ce used carefully for critical logging as we have the restriction for storing logs
1584
+ * on client side as we store them inside the local storage. Please see details inside storage.js
1585
+ * @param logString {string} log string
1586
+ * @param source {string} source of the log entry
1587
+ */
1588
+ Logger.log = function log(logString, source) {
1589
+ var timestamp = new Date().toISOString();
1590
+ LogsStorage.saveLog(timestamp + "|" + source + ":" + logString);
1591
+ };
1592
+ Logger.logError = function logError(e, settingFunction, additionalMessage, onlyToConsole) {
1593
+ var _e$errorDescription, _e$howToFix;
1594
+ if (additionalMessage === void 0) {
1595
+ additionalMessage = "";
1596
+ }
1597
+ if (onlyToConsole === void 0) {
1598
+ onlyToConsole = false;
1599
+ }
1600
+ var message = "\nFunction call " + (settingFunction != null ? settingFunction : "") + " failed. Error message: " + (e == null ? void 0 : e.message) + ". " + additionalMessage + " ";
1601
+ message += "" + ((_e$errorDescription = e == null ? void 0 : e.errorDescription) != null ? _e$errorDescription : "") + ((_e$howToFix = e == null ? void 0 : e.howToFix) != null ? _e$howToFix : "") + ((e == null ? void 0 : e.httpStatus) === 403 ? "Authentication has expired or was lost. " : "");
1602
+ if (e != null && e.response) {
1603
+ try {
1604
+ var responseData = safeStringify({
1605
+ response: e.response
1606
+ });
1607
+ responseData && (message += "\n" + responseData + ". ");
1608
+ } catch (e) {}
1609
+ }
1610
+ var finalErrorText = message + ". " + safeStringify(e);
1611
+ // eslint-disable-next-line no-console
1612
+ console.error(finalErrorText);
1613
+ if (!onlyToConsole) {
1614
+ this.log(finalErrorText, "logError");
1615
+ }
1616
+ };
1617
+ return Logger;
1618
+ }();
1619
+
1620
+ function _catch$4(body, recover) {
1621
+ try {
1622
+ var result = body();
1623
+ } catch (e) {
1624
+ return recover(e);
1625
+ }
1626
+ if (result && result.then) {
1627
+ return result.then(void 0, recover);
1628
+ }
1629
+ return result;
1630
+ }
1631
+ function useCallHandlingErrors() {
1632
+ var _useState = React.useState(),
1633
+ setState = _useState[1];
1634
+ return React.useCallback(function (functionToBeCalled, event) {
1635
+ try {
1636
+ var _temp = _catch$4(function () {
1637
+ return Promise.resolve(functionToBeCalled(event)).then(function () {});
1638
+ }, function (error) {
1639
+ Logger.logError(error, (functionToBeCalled == null ? void 0 : functionToBeCalled.name) || "errorBoundaryTrigger", "Caught by ErrorBoundary");
1640
+ // Triggering ErrorBoundary
1641
+ setState(function () {
1642
+ throw error;
1643
+ });
1644
+ });
1645
+ return Promise.resolve(_temp && _temp.then ? _temp.then(function () {}) : void 0);
1646
+ } catch (e) {
1647
+ return Promise.reject(e);
1648
+ }
1649
+ }, []);
1650
+ }
1651
+
1652
+ /**
1653
+ * Adds reference to standard state variable. It is helpful to be able to use state variable value inside
1654
+ * event handlers and other callbacks without the need to call setState(prev => { value = prev; return prev; }).
1655
+ *
1656
+ * @param initialValue {any} to be passed to useState
1657
+ * @return {[React.Ref, function]} reference to state variable and its setter
1658
+ */
1659
+ function useReferredState(initialValue) {
1660
+ var _React$useState = React__default["default"].useState(initialValue),
1661
+ state = _React$useState[0],
1662
+ setState = _React$useState[1];
1663
+ var reference = React__default["default"].useRef(state);
1664
+ var setReferredState = function setReferredState(value) {
1665
+ if (value && {}.toString.call(value) === "[object Function]") {
1666
+ value = value(reference.current);
1667
+ }
1668
+ reference.current = value;
1669
+ setState(value);
1670
+ };
1671
+ return [reference, setReferredState];
1672
+ }
1673
+
1482
1674
  /**
1483
1675
  * This function improves the passed error object (its message) by adding the passed function name
1484
1676
  * and additional message to it.
@@ -2101,144 +2293,6 @@ var Coin = /*#__PURE__*/function () {
2101
2293
  return Coin;
2102
2294
  }();
2103
2295
 
2104
- var LogsStorage = /*#__PURE__*/function () {
2105
- function LogsStorage() {}
2106
- LogsStorage.saveLog = function saveLog(log) {
2107
- this._inMemoryStorage.push(log);
2108
- };
2109
- LogsStorage.getInMemoryLogs = function getInMemoryLogs() {
2110
- return this._inMemoryStorage;
2111
- };
2112
- LogsStorage.getAllLogs = function getAllLogs() {
2113
- var storedLogs = "";
2114
- if (typeof window !== "undefined") {
2115
- storedLogs = localStorage.getItem(this._logsStorageId);
2116
- }
2117
- return storedLogs + "\n" + this._inMemoryStorage.join("\n");
2118
- }
2119
-
2120
- /**
2121
- * @param logger {Logger}
2122
- */;
2123
- LogsStorage.saveToTheDisk = function saveToTheDisk(logger) {
2124
- try {
2125
- var MAX_LOCAL_STORAGE_VOLUME_BYTES = 5 * 1024 * 1024;
2126
- var MAX_LOGS_STORAGE_BYTES = MAX_LOCAL_STORAGE_VOLUME_BYTES * 0.65;
2127
- if (typeof window !== "undefined") {
2128
- var existingLogs = localStorage.getItem(this._logsStorageId);
2129
- var logsString = existingLogs + "\n" + this._inMemoryStorage.join("\n");
2130
- var lettersCountToRemove = logsString.length - Math.round(MAX_LOGS_STORAGE_BYTES / 2);
2131
- if (lettersCountToRemove > 0) {
2132
- localStorage.setItem(this._logsStorageId, logsString.slice(lettersCountToRemove, logsString.length));
2133
- } else {
2134
- localStorage.setItem(this._logsStorageId, logsString);
2135
- }
2136
- this._inMemoryStorage = [];
2137
- }
2138
- } catch (e) {
2139
- logger == null || logger.logError(e, "saveToTheDisk", "Failed to save logs to disk");
2140
- }
2141
- };
2142
- LogsStorage.removeAllClientLogs = function removeAllClientLogs() {
2143
- if (typeof window !== "undefined") {
2144
- if (localStorage.getItem("doNotRemoveClientLogsWhenSignedOut") !== "true") {
2145
- localStorage.removeItem(this._logsStorageId);
2146
- }
2147
- }
2148
- this._inMemoryStorage = [];
2149
- };
2150
- LogsStorage.setDoNotRemoveClientLogsWhenSignedOut = function setDoNotRemoveClientLogsWhenSignedOut(value) {
2151
- if (typeof window !== "undefined") {
2152
- localStorage.setItem("doNotRemoveClientLogsWhenSignedOut", value);
2153
- }
2154
- };
2155
- return LogsStorage;
2156
- }();
2157
- LogsStorage._inMemoryStorage = [];
2158
- LogsStorage._logsStorageId = "clietnLogs_j203fj2D0n-d1";
2159
-
2160
- /**
2161
- * Stringify given object by use of JSON.stringify but handles circular structures and "response", "request" properties
2162
- * to avoid stringing redundant data when printing errors containing request/response objects.
2163
- *
2164
- * @param object - object to be stringed
2165
- * @param indent - custom indentation
2166
- * @return {string} - stringed object
2167
- */
2168
- function safeStringify(object, indent) {
2169
- if (indent === void 0) {
2170
- indent = 2;
2171
- }
2172
- var cache = [];
2173
- if (typeof object === "string" || typeof object === "function" || typeof object === "number" || typeof object === "undefined" || typeof object === "boolean") {
2174
- return String(object);
2175
- }
2176
- var retVal = JSON.stringify(object, function (key, value) {
2177
- if (key.toLowerCase().includes("request")) {
2178
- return JSON.stringify({
2179
- body: value == null ? void 0 : value.body,
2180
- query: value == null ? void 0 : value.query,
2181
- headers: value == null ? void 0 : value.headers
2182
- });
2183
- }
2184
- if (key.toLowerCase().includes("response")) {
2185
- return JSON.stringify({
2186
- statusText: value == null ? void 0 : value.statusText,
2187
- status: value == null ? void 0 : value.status,
2188
- data: value == null ? void 0 : value.data,
2189
- headers: value == null ? void 0 : value.headers
2190
- });
2191
- }
2192
- return typeof value === "object" && value !== null ? cache.includes(value) ? "duplicated reference" // Duplicated references were found, discarding this key
2193
- : cache.push(value) && value // Store value in our collection
2194
- : value;
2195
- }, indent);
2196
- cache = null;
2197
- return retVal;
2198
- }
2199
-
2200
- var Logger = /*#__PURE__*/function () {
2201
- function Logger() {}
2202
- /**
2203
- * Logs to client logs storage.
2204
- *
2205
- * WARNING! this method should ce used carefully for critical logging as we have the restriction for storing logs
2206
- * on client side as we store them inside the local storage. Please see details inside storage.js
2207
- * @param logString {string} log string
2208
- * @param source {string} source of the log entry
2209
- */
2210
- Logger.log = function log(logString, source) {
2211
- var timestamp = new Date().toISOString();
2212
- LogsStorage.saveLog(timestamp + "|" + source + ":" + logString);
2213
- };
2214
- Logger.logError = function logError(e, settingFunction, additionalMessage, onlyToConsole) {
2215
- var _e$errorDescription, _e$howToFix;
2216
- if (additionalMessage === void 0) {
2217
- additionalMessage = "";
2218
- }
2219
- if (onlyToConsole === void 0) {
2220
- onlyToConsole = false;
2221
- }
2222
- var message = "\nFunction call " + (settingFunction != null ? settingFunction : "") + " failed. Error message: " + (e == null ? void 0 : e.message) + ". " + additionalMessage + " ";
2223
- message += "" + ((_e$errorDescription = e == null ? void 0 : e.errorDescription) != null ? _e$errorDescription : "") + ((_e$howToFix = e == null ? void 0 : e.howToFix) != null ? _e$howToFix : "") + ((e == null ? void 0 : e.httpStatus) === 403 ? "Authentication has expired or was lost. " : "");
2224
- if (e != null && e.response) {
2225
- try {
2226
- var responseData = safeStringify({
2227
- response: e.response
2228
- });
2229
- responseData && (message += "\n" + responseData + ". ");
2230
- } catch (e) {}
2231
- }
2232
- var finalErrorText = message + ". " + safeStringify(e);
2233
- // eslint-disable-next-line no-console
2234
- console.error(finalErrorText);
2235
- if (!onlyToConsole) {
2236
- this.log(finalErrorText, "logError");
2237
- }
2238
- };
2239
- return Logger;
2240
- }();
2241
-
2242
2296
  /**
2243
2297
  * TODO: [tests, critical] Ued by payments logic
2244
2298
  *
@@ -2627,7 +2681,7 @@ var ExistingSwapWithFiatData = /*#__PURE__*/function (_ExistingSwap) {
2627
2681
  return ExistingSwapWithFiatData;
2628
2682
  }(ExistingSwap);
2629
2683
 
2630
- var PublicSwapCreationInfo =
2684
+ var BaseSwapCreationInfo =
2631
2685
  /**
2632
2686
  * @param fromCoin {Coin}
2633
2687
  * @param toCoin {Coin}
@@ -2641,7 +2695,7 @@ var PublicSwapCreationInfo =
2641
2695
  * @param fiatMax {number}
2642
2696
  * @param durationMinutesRange {string}
2643
2697
  */
2644
- function PublicSwapCreationInfo(fromCoin, toCoin, fromAmountCoins, toAmountCoins, rate, rawSwapData, min, fiatMin, max, fiatMax, durationMinutesRange) {
2698
+ function BaseSwapCreationInfo(fromCoin, toCoin, fromAmountCoins, toAmountCoins, rate, rawSwapData, min, fiatMin, max, fiatMax, durationMinutesRange) {
2645
2699
  this.fromCoin = fromCoin;
2646
2700
  this.toCoin = toCoin;
2647
2701
  this.fromAmountCoins = fromAmountCoins;
@@ -3915,7 +3969,7 @@ var PublicSwapService = /*#__PURE__*/function () {
3915
3969
  * fiatMax: (number|null)
3916
3970
  * }|{
3917
3971
  * result: true,
3918
- * swapCreationInfo: PublicSwapCreationInfo
3972
+ * swapCreationInfo: BaseSwapCreationInfo
3919
3973
  * }>}
3920
3974
  */
3921
3975
  ;
@@ -3968,7 +4022,7 @@ var PublicSwapService = /*#__PURE__*/function () {
3968
4022
  var toAmountCoins = AmountUtils.trim(fromAmountBigNumber.times(details.rate), fromCoin.digits);
3969
4023
  var result = {
3970
4024
  result: true,
3971
- swapCreationInfo: new PublicSwapCreationInfo(fromCoin, toCoin, fromAmountCoins, toAmountCoins, details.rate, details.rawSwapData, min, fiatMin, max, fiatMax, details.durationMinutesRange)
4025
+ swapCreationInfo: new BaseSwapCreationInfo(fromCoin, toCoin, fromAmountCoins, toAmountCoins, details.rate, details.rawSwapData, min, fiatMin, max, fiatMax, details.durationMinutesRange)
3972
4026
  };
3973
4027
  Logger.log("Result: " + safeStringify({
3974
4028
  result: result.result,
@@ -3993,7 +4047,7 @@ var PublicSwapService = /*#__PURE__*/function () {
3993
4047
  * @param fromCoin {Coin}
3994
4048
  * @param toCoin {Coin}
3995
4049
  * @param fromAmount {string}
3996
- * @param swapCreationInfo {PublicSwapCreationInfo}
4050
+ * @param swapCreationInfo {BaseSwapCreationInfo}
3997
4051
  * @param toAddress {string}
3998
4052
  * @param refundAddress {string}
3999
4053
  * @return {Promise<{
@@ -4022,7 +4076,7 @@ var PublicSwapService = /*#__PURE__*/function () {
4022
4076
  var loggerSource = "createPublicSwap";
4023
4077
  return Promise.resolve(_catch(function () {
4024
4078
  var _swapCreationInfo$fro, _swapCreationInfo$toC;
4025
- if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof fromAmount !== "string" || typeof toAddress !== "string" || typeof refundAddress !== "string" || !(swapCreationInfo instanceof PublicSwapCreationInfo)) {
4079
+ if (!(fromCoin instanceof Coin) || !(toCoin instanceof Coin) || typeof fromAmount !== "string" || typeof toAddress !== "string" || typeof refundAddress !== "string" || !(swapCreationInfo instanceof BaseSwapCreationInfo)) {
4026
4080
  throw new Error("Wrong input: " + fromCoin.ticker + " " + toCoin.ticker + " " + fromAmount + " " + swapCreationInfo);
4027
4081
  }
4028
4082
  Logger.log("Start: " + fromAmount + " " + fromCoin.ticker + " -> " + toCoin.ticker + ". Details: " + safeStringify(_extends({}, swapCreationInfo, {
@@ -4290,6 +4344,7 @@ PublicSwapService._fiatDecimalsCount = FiatCurrenciesService.getCurrencyDecimalC
4290
4344
 
4291
4345
  exports.AmountUtils = AmountUtils;
4292
4346
  exports.AssetIcon = AssetIcon;
4347
+ exports.BaseSwapCreationInfo = BaseSwapCreationInfo;
4293
4348
  exports.Blockchain = Blockchain;
4294
4349
  exports.Button = Button;
4295
4350
  exports.Cache = Cache;
@@ -4302,7 +4357,6 @@ exports.LoadingDots = LoadingDots;
4302
4357
  exports.Logger = Logger;
4303
4358
  exports.LogsStorage = LogsStorage;
4304
4359
  exports.Protocol = Protocol;
4305
- exports.PublicSwapCreationInfo = PublicSwapCreationInfo;
4306
4360
  exports.PublicSwapService = PublicSwapService;
4307
4361
  exports.SupportChat = SupportChat;
4308
4362
  exports.SwapProvider = SwapProvider;
@@ -4310,4 +4364,6 @@ exports.SwapUtils = SwapUtils;
4310
4364
  exports.SwapspaceSwapProvider = SwapspaceSwapProvider;
4311
4365
  exports.improveAndRethrow = improveAndRethrow;
4312
4366
  exports.safeStringify = safeStringify;
4367
+ exports.useCallHandlingErrors = useCallHandlingErrors;
4368
+ exports.useReferredState = useReferredState;
4313
4369
  //# sourceMappingURL=index.cjs.map