refactor: common components

This commit is contained in:
Slinetrac
2025-10-14 21:22:48 +08:00
parent 65cf6c387b
commit 778d506be7
2 changed files with 36 additions and 27 deletions

View File

@@ -313,30 +313,3 @@ export const LightweightTrafficErrorBoundary: React.FC<{
</TrafficErrorBoundary>
);
};
/**
* HOC为任何组件添加流量错误边界
*/
export function withTrafficErrorBoundary<P extends object>(
WrappedComponent: React.ComponentType<P>,
options?: {
lightweight?: boolean;
onError?: (error: Error, errorInfo: ErrorInfo) => void;
},
) {
const WithErrorBoundaryComponent = (props: P) => {
const ErrorBoundaryComponent = options?.lightweight
? LightweightTrafficErrorBoundary
: TrafficErrorBoundary;
return (
<ErrorBoundaryComponent onError={options?.onError}>
<WrappedComponent {...props} />
</ErrorBoundaryComponent>
);
};
WithErrorBoundaryComponent.displayName = `withTrafficErrorBoundary(${WrappedComponent.displayName || WrappedComponent.name})`;
return WithErrorBoundaryComponent;
}

View File

@@ -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<P extends object>(
WrappedComponent: React.ComponentType<P>,
options?: WithTrafficErrorBoundaryOptions,
) {
const WithErrorBoundaryComponent = (props: P) => {
const ErrorBoundaryComponent = options?.lightweight
? LightweightTrafficErrorBoundary
: TrafficErrorBoundary;
return (
<ErrorBoundaryComponent onError={options?.onError}>
<WrappedComponent {...props} />
</ErrorBoundaryComponent>
);
};
WithErrorBoundaryComponent.displayName = `withTrafficErrorBoundary(${WrappedComponent.displayName || WrappedComponent.name})`;
return WithErrorBoundaryComponent;
}