From a1152f1ad129a6c676d936c34aa914faa9edc6cd Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Sun, 3 Nov 2024 17:32:05 +0800 Subject: [PATCH] refactor(ErrorBoundary): optimize code --- .../schema-initializer/withInitializer.tsx | 6 +++--- .../components/SchemaSettingsChildren.tsx | 13 +++++++------ .../application/schema-toolbar/hooks/index.tsx | 4 ++-- .../antd/action/Action.Drawer.tsx | 2 +- .../antd/action/Action.Modal.tsx | 2 +- .../src/schema-component/antd/action/Action.tsx | 2 +- .../antd/block-item/BlockItem.tsx | 2 +- .../antd/block-item/BlockItemError.tsx | 6 +----- .../src/schema-component/antd/page/Page.tsx | 6 +----- .../src/client/filter/FilterItemInitializers.tsx | 2 +- .../src/client/renderer/ChartRenderer.tsx | 16 +++++----------- 11 files changed, 24 insertions(+), 37 deletions(-) diff --git a/packages/core/client/src/application/schema-initializer/withInitializer.tsx b/packages/core/client/src/application/schema-initializer/withInitializer.tsx index 997a571d5e..a4f08b1dc1 100644 --- a/packages/core/client/src/application/schema-initializer/withInitializer.tsx +++ b/packages/core/client/src/application/schema-initializer/withInitializer.tsx @@ -12,13 +12,13 @@ import { ConfigProvider, Popover, theme } from 'antd'; import React, { ComponentType, useCallback, useMemo, useState } from 'react'; import { css } from '@emotion/css'; +import { ErrorBoundary } from 'react-error-boundary'; import { useNiceDropdownMaxHeight } from '../../common/useNiceDropdownHeight'; import { useFlag } from '../../flag-provider'; import { ErrorFallback, useDesignable } from '../../schema-component'; import { useSchemaInitializerStyles } from './components/style'; import { SchemaInitializerContext } from './context'; import { SchemaInitializerOptions } from './types'; -import { ErrorBoundary } from 'react-error-boundary'; const defaultWrap = (s: ISchema) => s; const useWrapDefault = (wrap = defaultWrap) => wrap; @@ -91,7 +91,7 @@ export function withInitializer(C: ComponentType) { } return ( - console.error(err)}> + (C: ComponentType) { {...popoverProps} arrow={false} overlayClassName={overlayClassName} - open={visible} + open={visible} onOpenChange={setVisible} content={wrapSSR(
{ + return (props) => ; +}); + export const SchemaSettingsChildren: FC = (props) => { const { children } = props; const { visible } = useSchemaSettings(); @@ -85,11 +90,7 @@ export const SchemaSettingsChildren: FC = (props) = // 一个不会重复的 key,保证每次渲染都是新的组件。 const key = `${fieldComponentName ? fieldComponentName + '-' : ''}${item?.name}`; return ( - } - onError={(err) => console.log(err)} - > + ); diff --git a/packages/core/client/src/application/schema-toolbar/hooks/index.tsx b/packages/core/client/src/application/schema-toolbar/hooks/index.tsx index 8d0e645ee4..019bcd4d7e 100644 --- a/packages/core/client/src/application/schema-toolbar/hooks/index.tsx +++ b/packages/core/client/src/application/schema-toolbar/hooks/index.tsx @@ -9,9 +9,9 @@ import { ISchema } from '@formily/json-schema'; import React, { useMemo } from 'react'; +import { ErrorBoundary, FallbackProps } from 'react-error-boundary'; import { ErrorFallback, useComponent, useDesignable } from '../../../schema-component'; import { SchemaToolbar, SchemaToolbarProps } from '../../../schema-settings/GeneralSchemaDesigner'; -import { ErrorBoundary, FallbackProps } from 'react-error-boundary'; const SchemaToolbarErrorFallback: React.FC = (props) => { const { designable } = useDesignable(); @@ -46,7 +46,7 @@ export const useSchemaToolbarRender = (fieldSchema: ISchema) => { return null; } return ( - console.error(err)}> + ); diff --git a/packages/core/client/src/schema-component/antd/action/Action.Drawer.tsx b/packages/core/client/src/schema-component/antd/action/Action.Drawer.tsx index 9847e5c9c2..4b2d1092b3 100644 --- a/packages/core/client/src/schema-component/antd/action/Action.Drawer.tsx +++ b/packages/core/client/src/schema-component/antd/action/Action.Drawer.tsx @@ -111,7 +111,7 @@ export const InternalActionDrawer: React.FC = observer( ); export const ActionDrawer: ComposedActionDrawer = (props) => ( - console.log(err)}> + ); diff --git a/packages/core/client/src/schema-component/antd/action/Action.Modal.tsx b/packages/core/client/src/schema-component/antd/action/Action.Modal.tsx index 5478cc0317..1bf62a93e8 100644 --- a/packages/core/client/src/schema-component/antd/action/Action.Modal.tsx +++ b/packages/core/client/src/schema-component/antd/action/Action.Modal.tsx @@ -154,7 +154,7 @@ export const InternalActionModal: React.FC> = obse ); export const ActionModal: ComposedActionDrawer = (props) => ( - console.log(err)}> + ); diff --git a/packages/core/client/src/schema-component/antd/action/Action.tsx b/packages/core/client/src/schema-component/antd/action/Action.tsx index 67f253816c..83153601c0 100644 --- a/packages/core/client/src/schema-component/antd/action/Action.tsx +++ b/packages/core/client/src/schema-component/antd/action/Action.tsx @@ -56,7 +56,7 @@ const useA = () => { }; }; -const handleError = (err) => console.log(err); +const handleError = console.log; export const Action: ComposedAction = withDynamicSchemaProps( React.memo((props: ActionProps) => { diff --git a/packages/core/client/src/schema-component/antd/block-item/BlockItem.tsx b/packages/core/client/src/schema-component/antd/block-item/BlockItem.tsx index 76aea95113..deb129be5a 100644 --- a/packages/core/client/src/schema-component/antd/block-item/BlockItem.tsx +++ b/packages/core/client/src/schema-component/antd/block-item/BlockItem.tsx @@ -44,7 +44,7 @@ export const BlockItem: React.FC = withDynamicSchemaProps( style={style} > {render()} - console.log(err)}> + {children} diff --git a/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx b/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx index 599acd93a3..b4bb2451eb 100644 --- a/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx +++ b/packages/core/client/src/schema-component/antd/block-item/BlockItemError.tsx @@ -39,13 +39,9 @@ const FallbackComponent: FC = (props) => { ); }; -const handleErrors = (error) => { - console.error(error); -}; - export const BlockItemError: FC = ({ children }) => { return ( - + {children} ); diff --git a/packages/core/client/src/schema-component/antd/page/Page.tsx b/packages/core/client/src/schema-component/antd/page/Page.tsx index ca0a3cbb33..d9c227f03b 100644 --- a/packages/core/client/src/schema-component/antd/page/Page.tsx +++ b/packages/core/client/src/schema-component/antd/page/Page.tsx @@ -59,10 +59,6 @@ export const Page = (props) => { const { wrapSSR, hashId, componentCls } = useStyles(); const { token } = useToken(); - const handleErrors = useCallback((error) => { - console.error(error); - }, []); - // 这里的样式是为了保证页面 tabs 标签下面的分割线和页面内容对齐(页面内边距可以通过主题编辑器调节) const tabBarStyle = useMemo( () => ({ @@ -176,7 +172,7 @@ export const Page = (props) => {
- + {tabUid ? ( // used to match the rout with name "admin.page.tab" diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/filter/FilterItemInitializers.tsx b/packages/plugins/@nocobase/plugin-data-visualization/src/client/filter/FilterItemInitializers.tsx index 547ccf895c..fa94c21c93 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/client/filter/FilterItemInitializers.tsx +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/filter/FilterItemInitializers.tsx @@ -104,7 +104,7 @@ export const ChartFilterFormItem = observer( {/* {exists ? ( */} - console.log(err)} FallbackComponent={ErrorFallback}> + {/* ) : ( */} diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx b/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx index 95b5744c88..6ede400d5e 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx @@ -7,18 +7,18 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ +import { Schema } from '@formily/react'; +import { uid } from '@formily/shared'; import { useAPIClient } from '@nocobase/client'; import { Empty, Result, Spin, Typography } from 'antd'; import React, { useContext, useEffect } from 'react'; import { ErrorBoundary } from 'react-error-boundary'; +import { useChart } from '../chart/group'; import { useData, useFieldTransformer, useFieldsWithAssociation } from '../hooks'; import { useChartsTranslation } from '../locale'; import { getField } from '../utils'; -import { ChartRendererContext } from './ChartRendererProvider'; -import { useChart } from '../chart/group'; -import { Schema } from '@formily/react'; import { ChartRendererDesigner } from './ChartRendererDesigner'; -import { uid } from '@formily/shared'; +import { ChartRendererContext } from './ChartRendererProvider'; const { Paragraph, Text } = Typography; const ErrorFallback = ({ error }) => { @@ -83,13 +83,7 @@ export const ChartRenderer: React.FC & { return ( - { - console.error(error); - }} - FallbackComponent={ErrorFallback} - > + {!service.loading && }