@rabbitio/ui-kit 1.0.0-beta.36 → 1.0.0-beta.38

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.36",
3
+ "version": "1.0.0-beta.38",
4
4
  "description": "Rabbit.io react.js components kit",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -1,3 +1,6 @@
1
+ import { Logger } from "./utils/logging/logger.js";
2
+ import { safeStringify } from "./utils/safeStringify.js";
3
+
1
4
  /**
2
5
  * This function improves the passed error object (its message) by adding the passed function name
3
6
  * and additional message to it.
@@ -25,3 +28,15 @@ function improvedErrorMessage(e, settingFunction, additionalMessage) {
25
28
 
26
29
  return message;
27
30
  }
31
+
32
+ export function logErrorOrOutputToConsole(e) {
33
+ try {
34
+ // TODO: [dev] remove this after few weeks of testing output in real life
35
+ // eslint-disable-next-line no-console
36
+ console.log("BEFORE SAFE", e);
37
+ Logger.log("logErrorOrOutputToConsole", safeStringify(e));
38
+ } catch (e) {
39
+ // eslint-disable-next-line no-console
40
+ console.log("logErrorOrOutputToConsole", e);
41
+ }
42
+ }
@@ -0,0 +1,14 @@
1
+ export const handleClickOutside = (exceptionsRefs, callback) => {
2
+ function handleClick(event) {
3
+ const isExceptionClicked = exceptionsRefs.find(
4
+ (ref) => ref?.current && ref.current.contains(event.target)
5
+ );
6
+ if (!isExceptionClicked) {
7
+ callback();
8
+ }
9
+ }
10
+
11
+ document.addEventListener("click", handleClick);
12
+
13
+ return () => document.removeEventListener("click", handleClick);
14
+ };
@@ -0,0 +1,87 @@
1
+ const PARAMETER_VALUES_SEPARATOR = "|*|"; // Sting that with high probability will not be in the user's data
2
+
3
+ /**
4
+ * Adds specified parameter with values to the URL query string
5
+ *
6
+ * @param parameterName - String - name of the parameter
7
+ * @param values - Array of String values
8
+ * @param updateURLCallback - callback that will be called with the updated query string. Can be used to save it to URL
9
+ */
10
+ export function saveQueryParameterAndValues(
11
+ parameterName,
12
+ values,
13
+ updateURLCallback = (newQueryString) => {}
14
+ ) {
15
+ let parametersAndValues = parseSearchString();
16
+ parametersAndValues = parametersAndValues.filter(
17
+ (parameterAndValues) => parameterAndValues[0] !== parameterName
18
+ );
19
+ const parameterValuesForURL = encodeURIComponent(
20
+ values.join(PARAMETER_VALUES_SEPARATOR)
21
+ );
22
+ parametersAndValues.push([parameterName, parameterValuesForURL]);
23
+ const newQueryString = `?${parametersAndValues
24
+ .map((parameterAndValues) => parameterAndValues.join("="))
25
+ .join("&")}`;
26
+ updateURLCallback(newQueryString);
27
+
28
+ return newQueryString;
29
+ }
30
+
31
+ /**
32
+ * Removes specified parameter with values from the URL query string
33
+ *
34
+ * @param parameterName - String - name of the parameter
35
+ * @param updateURLCallback - callback that will be called with the updated query string. Can be used to save it to URL
36
+ */
37
+ // TODO: [tests, moderate] units required the same as or other functions in this module
38
+ export function removeQueryParameterAndValues(
39
+ parameterName,
40
+ updateURLCallback = (newQueryString) => {}
41
+ ) {
42
+ let parametersAndValues = parseSearchString();
43
+ parametersAndValues = parametersAndValues.filter(
44
+ (parameterAndValues) => parameterAndValues[0] !== parameterName
45
+ );
46
+ const newQueryString = `?${parametersAndValues
47
+ .map((parameterAndValues) => parameterAndValues.join("="))
48
+ .join("&")}`;
49
+ updateURLCallback(newQueryString);
50
+
51
+ return newQueryString;
52
+ }
53
+
54
+ /**
55
+ * Retrieves parameter values from the URL query string.
56
+ *
57
+ * If there are several parameters with the same name in the URL then all their values are returned
58
+ *
59
+ * @param name {string} - parameter name
60
+ * @return {string[]} [] - if the parameter is not present in URL. [""] - if parameter present but has empty value
61
+ */
62
+ export function getQueryParameterValues(name) {
63
+ return parseSearchString()
64
+ .filter((parameterAndValue) => parameterAndValue[0] === name)
65
+ .reduce((allValues, parameterAndValue) => {
66
+ const values = decodeURIComponent(parameterAndValue[1] || "").split(
67
+ PARAMETER_VALUES_SEPARATOR
68
+ );
69
+ return [...allValues, ...values];
70
+ }, []);
71
+ }
72
+
73
+ function parseSearchString() {
74
+ const trimmed = (window.location.search?.slice(1) || "").trim();
75
+
76
+ return (
77
+ (trimmed &&
78
+ trimmed
79
+ .split("&")
80
+ .map((parameterAndValue) => parameterAndValue.split("="))) ||
81
+ []
82
+ );
83
+ }
84
+
85
+ export function getQueryParameterSingleValue(name) {
86
+ return (getQueryParameterValues(name) || [])[0];
87
+ }
package/src/index.js CHANGED
@@ -6,9 +6,16 @@ export { AssetIcon } from "./components/atoms/AssetIcon/AssetIcon.jsx";
6
6
 
7
7
  export { useCallHandlingErrors } from "./components/hooks/useCallHandlingErrors.js";
8
8
  export { useReferredState } from "./components/hooks/useReferredState.js";
9
+ export { handleClickOutside } from "./components/utils/uiUtils.js";
10
+
11
+ export { saveQueryParameterAndValues } from "./components/utils/urlQueryUtils.js";
12
+ export { removeQueryParameterAndValues } from "./components/utils/urlQueryUtils.js";
13
+ export { getQueryParameterSingleValue } from "./components/utils/urlQueryUtils.js";
14
+ export { getQueryParameterValues } from "./components/utils/urlQueryUtils.js";
9
15
 
10
16
  // Common code lib (to be extracted later to dedicated lib)
11
17
  export { improveAndRethrow } from "./common/errorUtils.js";
18
+ export { logErrorOrOutputToConsole } from "./common/errorUtils.js";
12
19
  export { FiatCurrenciesService } from "./common/fiatCurrenciesService.js";
13
20
  export { AmountUtils } from "./common/amountUtils.js";
14
21
 
@@ -22,7 +22,7 @@ export class SwapspaceSwapProvider extends SwapProvider {
22
22
  ) {
23
23
  super();
24
24
  this._supportedCoins = [];
25
- this._URL = `${apiKeysProxyUrl}/swapspace`;
25
+ this._URL = `${apiKeysProxyUrl}`;
26
26
  this._maxRateDigits = 20;
27
27
  this.useRestrictedCoinsSet = useRestrictedCoinsSet;
28
28
  this._customCoinBuilder = customCoinBuilder;