From 778d506be7a05d7fd3312f60d2760ad4d2e81be2 Mon Sep 17 00:00:00 2001 From: Slinetrac Date: Tue, 14 Oct 2025 21:22:48 +0800 Subject: [PATCH] refactor: common components --- .../common/traffic-error-boundary.tsx | 27 -------------- .../common/with-traffic-error-boundary.tsx | 36 +++++++++++++++++++ 2 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 src/components/common/with-traffic-error-boundary.tsx diff --git a/src/components/common/traffic-error-boundary.tsx b/src/components/common/traffic-error-boundary.tsx index ce7a3f61..c665e2a9 100644 --- a/src/components/common/traffic-error-boundary.tsx +++ b/src/components/common/traffic-error-boundary.tsx @@ -313,30 +313,3 @@ export const LightweightTrafficErrorBoundary: React.FC<{ ); }; - -/** - * HOC:为任何组件添加流量错误边界 - */ -export function withTrafficErrorBoundary

( - WrappedComponent: React.ComponentType

, - options?: { - lightweight?: boolean; - onError?: (error: Error, errorInfo: ErrorInfo) => void; - }, -) { - const WithErrorBoundaryComponent = (props: P) => { - const ErrorBoundaryComponent = options?.lightweight - ? LightweightTrafficErrorBoundary - : TrafficErrorBoundary; - - return ( - - - - ); - }; - - WithErrorBoundaryComponent.displayName = `withTrafficErrorBoundary(${WrappedComponent.displayName || WrappedComponent.name})`; - - return WithErrorBoundaryComponent; -} diff --git a/src/components/common/with-traffic-error-boundary.tsx b/src/components/common/with-traffic-error-boundary.tsx new file mode 100644 index 00000000..65b6b927 --- /dev/null +++ b/src/components/common/with-traffic-error-boundary.tsx @@ -0,0 +1,36 @@ +import React from "react"; +import type { ErrorInfo } from "react"; + +import { + TrafficErrorBoundary, + LightweightTrafficErrorBoundary, +} from "./traffic-error-boundary"; + +interface WithTrafficErrorBoundaryOptions { + lightweight?: boolean; + onError?: (error: Error, errorInfo: ErrorInfo) => void; +} + +/** + * HOC:为任何组件添加流量错误边界 + */ +export function withTrafficErrorBoundary

( + WrappedComponent: React.ComponentType

, + options?: WithTrafficErrorBoundaryOptions, +) { + const WithErrorBoundaryComponent = (props: P) => { + const ErrorBoundaryComponent = options?.lightweight + ? LightweightTrafficErrorBoundary + : TrafficErrorBoundary; + + return ( + + + + ); + }; + + WithErrorBoundaryComponent.displayName = `withTrafficErrorBoundary(${WrappedComponent.displayName || WrappedComponent.name})`; + + return WithErrorBoundaryComponent; +}