@blocklet/discuss-kit-ux 2.6.11 → 2.6.13
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.
|
@@ -4,14 +4,68 @@ import Box from "@mui/material/Box";
|
|
|
4
4
|
import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
|
|
5
5
|
import { useApiErrorHandler } from "./api-error-handler.mjs";
|
|
6
6
|
import { toast } from "../toast.mjs";
|
|
7
|
+
const recentErrors = /* @__PURE__ */ new Map();
|
|
8
|
+
const ERROR_COOLDOWN_MS = 3e3;
|
|
9
|
+
const MAX_TOASTS_PER_SECOND = 3;
|
|
10
|
+
let toastCount = 0;
|
|
11
|
+
let lastResetTime = Date.now();
|
|
12
|
+
const getErrorKey = (msg) => {
|
|
13
|
+
if (typeof msg === "string") return msg;
|
|
14
|
+
return `react-element-${msg.type?.toString() || "unknown"}-${msg.key || ""}`;
|
|
15
|
+
};
|
|
16
|
+
const shouldShowError = (errorKey) => {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
if (now - lastResetTime > 1e3) {
|
|
19
|
+
toastCount = 0;
|
|
20
|
+
lastResetTime = now;
|
|
21
|
+
}
|
|
22
|
+
if (toastCount >= MAX_TOASTS_PER_SECOND) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const lastShown = recentErrors.get(errorKey);
|
|
26
|
+
if (lastShown && now - lastShown < ERROR_COOLDOWN_MS) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
if (recentErrors.size > 50) {
|
|
30
|
+
const cutoff = now - ERROR_COOLDOWN_MS;
|
|
31
|
+
for (const [key, time] of recentErrors.entries()) {
|
|
32
|
+
if (time < cutoff) {
|
|
33
|
+
recentErrors.delete(key);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
const markErrorShown = (errorKey) => {
|
|
40
|
+
recentErrors.set(errorKey, Date.now());
|
|
41
|
+
toastCount++;
|
|
42
|
+
};
|
|
43
|
+
const getErrorMessage = (data, fallback) => {
|
|
44
|
+
if (data && typeof data === "object") {
|
|
45
|
+
const { message, error } = data;
|
|
46
|
+
if (typeof message === "string" && message) return message;
|
|
47
|
+
if (typeof error === "string" && error) return error;
|
|
48
|
+
}
|
|
49
|
+
return fallback;
|
|
50
|
+
};
|
|
7
51
|
export const useDefaultApiErrorHandler = (options) => {
|
|
8
52
|
const { request, onError } = options;
|
|
9
53
|
const { t } = useLocaleContext();
|
|
10
54
|
const showError = (msg, toastOptions) => {
|
|
11
|
-
|
|
55
|
+
const errorKey = getErrorKey(msg);
|
|
56
|
+
if (!shouldShowError(errorKey)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
markErrorShown(errorKey);
|
|
60
|
+
toast.error(msg, { ...toastOptions, id: errorKey });
|
|
12
61
|
};
|
|
13
62
|
const showWarning = (msg, toastOptions) => {
|
|
14
|
-
|
|
63
|
+
const errorKey = getErrorKey(msg);
|
|
64
|
+
if (!shouldShowError(errorKey)) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
markErrorShown(errorKey);
|
|
68
|
+
toast.warning(msg, { ...toastOptions, id: errorKey });
|
|
15
69
|
};
|
|
16
70
|
const errorHandler = {
|
|
17
71
|
handleError(error) {
|
|
@@ -27,21 +81,21 @@ export const useDefaultApiErrorHandler = (options) => {
|
|
|
27
81
|
const statusCode = response.status;
|
|
28
82
|
switch (statusCode) {
|
|
29
83
|
case 400: {
|
|
30
|
-
showError(t("apiError.400"));
|
|
84
|
+
showError(getErrorMessage(response.data, t("apiError.400")));
|
|
31
85
|
break;
|
|
32
86
|
}
|
|
33
87
|
case 401: {
|
|
34
|
-
showError(t("apiError.401"));
|
|
88
|
+
showError(getErrorMessage(response.data, t("apiError.401")));
|
|
35
89
|
break;
|
|
36
90
|
}
|
|
37
91
|
case 403: {
|
|
38
|
-
showError(t("apiError.403"));
|
|
92
|
+
showError(getErrorMessage(response.data, t("apiError.403")));
|
|
39
93
|
break;
|
|
40
94
|
}
|
|
41
95
|
case 409: {
|
|
42
96
|
showWarning(
|
|
43
97
|
/* @__PURE__ */ jsxs(Box, { children: [
|
|
44
|
-
/* @__PURE__ */ jsx(Box, { children: t("apiError.409") }),
|
|
98
|
+
/* @__PURE__ */ jsx(Box, { children: getErrorMessage(response.data, t("apiError.409")) }),
|
|
45
99
|
/* @__PURE__ */ jsxs(Box, { children: [
|
|
46
100
|
"(",
|
|
47
101
|
t("apiError.staleTip"),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/discuss-kit-ux",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.13",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist"
|
|
6
6
|
],
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"unstated-next": "^1.1.0",
|
|
48
48
|
"url-join": "^4.0.1",
|
|
49
49
|
"zustand": "^4.5.5",
|
|
50
|
-
"@blocklet/
|
|
51
|
-
"@blocklet/
|
|
50
|
+
"@blocklet/editor": "2.6.13",
|
|
51
|
+
"@blocklet/labels": "2.6.13"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@arcblock/did-connect-react": "^3.1.5",
|