@taicode/common-web 1.1.11 → 1.1.14

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.
@@ -1,7 +1,6 @@
1
1
  import React from 'react';
2
2
  import { SystemErrorType, UserErrorType } from '@taicode/common-base';
3
3
  import 'react-toastify/dist/ReactToastify.css';
4
- import './style.css';
5
4
  interface ToastCatcherProps {
6
5
  defaultMessage?: string;
7
6
  children: React.ReactNode;
@@ -9,5 +8,11 @@ interface ToastCatcherProps {
9
8
  }
10
9
  /** 自动捕获和处理异步错误,无法识别的错误会保持原样 */
11
10
  export declare function Toaster(props: ToastCatcherProps): import("react/jsx-runtime").JSX.Element;
11
+ export declare namespace Toaster {
12
+ var info: (content: React.ReactNode) => void;
13
+ var error: (content: React.ReactNode) => void;
14
+ var success: (content: React.ReactNode) => void;
15
+ var warning: (content: React.ReactNode) => void;
16
+ }
12
17
  export {};
13
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/toaster/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyC,MAAM,OAAO,CAAA;AAE7D,OAAO,EAA0B,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAE7F,OAAO,uCAAuC,CAAA;AAC9C,OAAO,aAAa,CAAA;AAEpB,UAAU,iBAAiB;IACzB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;IACzB,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,GAAG,eAAe,EAAE,MAAM,CAAC,CAAC,CAAA;CACnE;AAED,+BAA+B;AAC/B,wBAAgB,OAAO,CAAC,KAAK,EAAE,iBAAiB,2CAqF/C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/toaster/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyC,MAAM,OAAO,CAAA;AAE7D,OAAO,EAA0B,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAE7F,OAAO,uCAAuC,CAAA;AAE9C,UAAU,iBAAiB;IACzB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;IACzB,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,GAAG,eAAe,EAAE,MAAM,CAAC,CAAC,CAAA;CACnE;AAED,+BAA+B;AAC/B,wBAAgB,OAAO,CAAC,KAAK,EAAE,iBAAiB,2CAyF/C;yBAzFe,OAAO;wBAmGE,KAAK,CAAC,SAAS;yBAId,KAAK,CAAC,SAAS;2BAIb,KAAK,CAAC,SAAS;2BAIf,KAAK,CAAC,SAAS"}
@@ -1,36 +1,48 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useCallback, useEffect, useRef } from 'react';
2
+ import { useEffect, useRef } from 'react';
3
3
  import { ToastContainer, toast as baseToast } from 'react-toastify';
4
4
  import { SystemError, UserError } from '@taicode/common-base';
5
5
  import 'react-toastify/dist/ReactToastify.css';
6
- import './style.css';
7
6
  /** 自动捕获和处理异步错误,无法识别的错误会保持原样 */
8
7
  export function Toaster(props) {
9
8
  const addEventListened = useRef(false);
10
9
  const { messages = {}, defaultMessage } = props;
11
- const sendError = useCallback((error) => {
12
- baseToast.success(_jsx(Toast, { type: 'success', error: error }));
13
- }, []);
14
- const sendSuccess = useCallback((error) => {
15
- baseToast.error(_jsx(Toast, { type: 'error', error: error }));
16
- }, []);
10
+ const handledErrors = useRef(new WeakSet());
17
11
  useEffect(() => {
18
12
  if (addEventListened.current)
19
13
  return;
20
14
  const errorHandler = (event) => {
21
15
  const errorObject = event.error;
16
+ // 检查是否已经处理过这个错误对象
17
+ if (errorObject && handledErrors.current.has(errorObject)) {
18
+ event.preventDefault();
19
+ event.stopPropagation();
20
+ return;
21
+ }
22
22
  if (SystemError.is(errorObject) || UserError.is(errorObject)) {
23
+ console.error('Caught error:', errorObject);
24
+ // 标记此错误已处理
25
+ handledErrors.current.add(errorObject);
23
26
  event.preventDefault();
24
27
  event.stopPropagation();
25
- sendError(errorObject.type);
28
+ Toaster.error(messages[errorObject.type] || defaultMessage);
26
29
  }
27
30
  };
28
31
  const unhandledRejectionHandler = (event) => {
29
32
  const errorObject = event.reason;
33
+ // 检查是否已经处理过这个错误对象
34
+ if (errorObject && handledErrors.current.has(errorObject)) {
35
+ event.preventDefault();
36
+ event.stopPropagation();
37
+ return;
38
+ }
30
39
  if (SystemError.is(errorObject) || UserError.is(errorObject)) {
40
+ console.error('Caught error:', errorObject);
41
+ // 标记此错误已处理
42
+ handledErrors.current.add(errorObject);
31
43
  event.preventDefault();
32
44
  event.stopPropagation();
33
- sendError(errorObject.type);
45
+ Toaster.error(messages[errorObject.type] || defaultMessage);
34
46
  }
35
47
  };
36
48
  window.addEventListener('error', errorHandler, true);
@@ -40,17 +52,26 @@ export function Toaster(props) {
40
52
  window.removeEventListener('error', errorHandler, true);
41
53
  window.removeEventListener('unhandledrejection', unhandledRejectionHandler, true);
42
54
  addEventListened.current = false;
55
+ handledErrors.current = new WeakSet();
43
56
  };
44
57
  }, []);
45
58
  return (_jsxs(_Fragment, { children: [props.children, _jsx(ToastContainer, { newestOnTop: true, hideProgressBar: true, closeButton: false, position: "top-center" })] }));
46
59
  function ErrorIcon() {
47
60
  return (_jsx("svg", { xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", strokeWidth: 1.5, stroke: "currentColor", className: "size-6 text-red-500", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" }) }));
48
61
  }
49
- function SuccessIcon() {
50
- return (_jsx("svg", { xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", strokeWidth: 1.5, stroke: "currentColor", className: "size-6 text-green-500", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" }) }));
51
- }
52
- function Toast(props) {
53
- const { type, error } = props;
54
- return (_jsxs("div", { className: "flex items-center w-full p-4 space-x-4 rtl:space-x-reverse text-gray-500 bg-white divide-x rtl:divide-x-reverse divide-gray-200 rounded-lg shadow dark:text-gray-400 dark:divide-gray-700 dark:bg-gray-800", role: "alert", children: [props.type === 'error' && (_jsx(ErrorIcon, {})), props.type === 'success' && (_jsx(SuccessIcon, {})), _jsx("div", { className: "ps-4 text-sm font-normal", children: messages[error] || defaultMessage })] }));
62
+ function ErrorToast(props) {
63
+ return (_jsxs("div", { className: "flex items-center space-x-3", role: "alert", children: [_jsx(ErrorIcon, {}), _jsx("div", { className: "text-sm font-medium text-gray-900 dark:text-gray-100" })] }));
55
64
  }
56
65
  }
66
+ Toaster.info = (content) => {
67
+ baseToast.info(content);
68
+ };
69
+ Toaster.error = (content) => {
70
+ baseToast.error(content);
71
+ };
72
+ Toaster.success = (content) => {
73
+ baseToast.success(content);
74
+ };
75
+ Toaster.warning = (content) => {
76
+ baseToast.warning(content);
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taicode/common-web",
3
- "version": "1.1.11",
3
+ "version": "1.1.14",
4
4
  "author": "Alain",
5
5
  "license": "ISC",
6
6
  "description": "",
@@ -44,6 +44,10 @@
44
44
  "@testing-library/jest-dom": "^6.1.0",
45
45
  "jsdom": "^23.0.0",
46
46
  "vitest": "^1.0.0",
47
- "@vitest/ui": "^1.0.0"
47
+ "@vitest/ui": "^1.0.0",
48
+ "@needle-di/core": "^1.0.0",
49
+ "@types/react": "^18.0.0",
50
+ "mobx": "^6.0.0",
51
+ "react": "^18.0.0"
48
52
  }
49
53
  }