error.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use client";
  2. import React from "react";
  3. import { IconButton } from "./button";
  4. import ResetIcon from "../icons/reload.svg";
  5. import Locale from "../locales";
  6. import './error.scss';
  7. import { Button} from 'antd';
  8. import { Color } from "antd/es/color-picker";
  9. interface IErrorBoundaryState {
  10. hasError: boolean;
  11. error: Error | null;
  12. info: React.ErrorInfo | null;
  13. }
  14. export class ErrorBoundary extends React.Component<any, IErrorBoundaryState> {
  15. constructor(props: any) {
  16. super(props);
  17. this.state = { hasError: false, error: null, info: null };
  18. }
  19. componentDidCatch(error: Error, info: React.ErrorInfo) {
  20. // Update state with error details
  21. this.setState({ hasError: true, error, info });
  22. }
  23. clearAndSaveData() {
  24. // 直接清除数据,不下载备份
  25. localStorage.clear();
  26. location.reload();
  27. }
  28. render() {
  29. if (this.state.hasError) {
  30. // Render error message
  31. return (
  32. <div className="error">
  33. <div className="error-icon">❌</div>
  34. <h2 className="error-title">哎呀,出了点小问题!</h2>
  35. <p className="error-desc">系统遇到了意外状况,别担心,我们的技术团队已经收到通知。您可以尝试刷新页面或清除数据来解决问题。</p>
  36. <div className="error-actions">
  37. {/* <IconButton
  38. icon={<ResetIcon />}
  39. text="Clear All Data"
  40. onClick={() => {
  41. this.clearAndSaveData();
  42. }}
  43. bordered
  44. /> */}
  45. <Button className="btn" type="primary" onClick={()=>{
  46. location.reload();
  47. }}> 刷新页面</Button>
  48. <Button className="btn btn-outline" onClick={() => {
  49. this.clearAndSaveData();
  50. }}>清除所有数据</Button>
  51. </div>
  52. {/* 注释掉错误信息 */}
  53. {/* <pre>
  54. <code>{this.state.error?.toString()}</code>
  55. <code>{this.state.info?.componentStack}</code>
  56. </pre> */}
  57. <div style={{ display: "flex", justifyContent: "center" }}>
  58. {/* <IconButton
  59. icon={<ResetIcon />}
  60. text="Clear All Data"
  61. onClick={() => {
  62. this.clearAndSaveData();
  63. }}
  64. bordered
  65. /> */}
  66. </div>
  67. </div>
  68. );
  69. }
  70. // if no error occurred, render children
  71. return this.props.children;
  72. }
  73. }