From cef0dc141ba31241b375be6c4013c7d82ca7f75d Mon Sep 17 00:00:00 2001 From: DimaAmega Date: Fri, 13 Dec 2024 16:04:24 +0400 Subject: [PATCH] UI: refactor use widget wrapper --- .../src/components/DashboardContent.tsx | 8 +- .../src/components/DashboardWidgets.tsx | 6 +- .../src/contexts/WidgetWrapper.tsx | 13 ++ .../evidently-ui-lib/src/widgets/Widget.tsx | 199 +++++++++--------- .../src/widgets/WidgetRenderer.tsx | 8 +- 5 files changed, 117 insertions(+), 117 deletions(-) create mode 100644 ui/packages/evidently-ui-lib/src/contexts/WidgetWrapper.tsx diff --git a/ui/packages/evidently-ui-lib/src/components/DashboardContent.tsx b/ui/packages/evidently-ui-lib/src/components/DashboardContent.tsx index d0c2d0136d..3427c2881e 100644 --- a/ui/packages/evidently-ui-lib/src/components/DashboardContent.tsx +++ b/ui/packages/evidently-ui-lib/src/components/DashboardContent.tsx @@ -4,17 +4,13 @@ import { WidgetRenderer } from '~/widgets/WidgetRenderer' export interface DashboardContentProps { widgets: WidgetInfo[] - ItemWrapper?: ({ id, children }: { id: string; children: React.ReactNode }) => React.ReactNode } -export const DashboardContentWidgets: FunctionComponent = ({ - widgets, - ItemWrapper -}) => ( +export const DashboardContentWidgets: FunctionComponent = ({ widgets }) => ( <> {widgets.length > 0 && widgets.map((wi, idx) => ( - {WidgetRenderer(`wi_${idx}`, wi, ItemWrapper)} + {WidgetRenderer(`wi_${idx}`, wi)} ))} ) diff --git a/ui/packages/evidently-ui-lib/src/components/DashboardWidgets.tsx b/ui/packages/evidently-ui-lib/src/components/DashboardWidgets.tsx index 05d3e02a11..913943de21 100644 --- a/ui/packages/evidently-ui-lib/src/components/DashboardWidgets.tsx +++ b/ui/packages/evidently-ui-lib/src/components/DashboardWidgets.tsx @@ -3,11 +3,9 @@ import type { WidgetInfo } from '~/api' import { DashboardContentWidgets } from '~/components/DashboardContent' export const DashboardWidgets = ({ - widgets, - ItemWrapper + widgets }: { widgets: WidgetInfo[] - ItemWrapper?: ({ id, children }: { id: string; children: React.ReactNode }) => React.ReactNode }) => { if (widgets.length === 0) { return ( @@ -20,7 +18,7 @@ export const DashboardWidgets = ({ return ( <> - + ) diff --git a/ui/packages/evidently-ui-lib/src/contexts/WidgetWrapper.tsx b/ui/packages/evidently-ui-lib/src/contexts/WidgetWrapper.tsx new file mode 100644 index 0000000000..287ac192d5 --- /dev/null +++ b/ui/packages/evidently-ui-lib/src/contexts/WidgetWrapper.tsx @@ -0,0 +1,13 @@ +import React, { useContext } from 'react' + +export type WidgetWrapper = { + WidgetWrapper: ({ id, children }: { id: string; children: React.ReactNode }) => JSX.Element +} + +const EmptyWidgetWrapper: WidgetWrapper['WidgetWrapper'] = ({ children }) => <>{children} + +export const widgetWrapperContext = React.createContext({ + WidgetWrapper: EmptyWidgetWrapper +}) + +export const useWidgetWrapper = () => useContext(widgetWrapperContext) diff --git a/ui/packages/evidently-ui-lib/src/widgets/Widget.tsx b/ui/packages/evidently-ui-lib/src/widgets/Widget.tsx index db09f06c1b..5399e97c76 100644 --- a/ui/packages/evidently-ui-lib/src/widgets/Widget.tsx +++ b/ui/packages/evidently-ui-lib/src/widgets/Widget.tsx @@ -3,6 +3,7 @@ import type { WidgetInfo } from '~/api' import { Card, CardContent, Grid, Typography } from '@mui/material' +import { useWidgetWrapper } from '~/contexts/WidgetWrapper' import AlertBlock from './AlertBlock' import AlertStatBlock from './AlertStatBlock' import InsightBlock from './InsightBlock' @@ -59,127 +60,123 @@ function Sizes(size: number): sizes { } } -const Widget = ( - props: WidgetProps & { - ItemWrapper?: ({ id, children }: { id: string; children: React.ReactNode }) => React.ReactNode - } -) => { - const { size, ItemWrapper } = props +const Widget = (props: WidgetProps) => { + const { size } = props const alertsPosition = props.children.alertsPosition ?? 'row' const { id, title, details, content, alerts, alertStats, insights } = props.children const isAlertsExists = alerts === undefined ? false : alerts.length > 0 const isInsightsExists = insights === undefined ? false : insights.length > 0 - const Component = ( - - - - {alertsPosition === 'row' ? ( - - - {title ? ( - - {title} - - ) : ( -
- )} -
{content}
- {details ? {details} :
} - - {isAlertsExists ? ( - - {alerts ? ( - - {alertStats ? ( - - - + const { WidgetWrapper } = useWidgetWrapper() + + return ( + + + + + + {alertsPosition === 'row' ? ( + + + {title ? ( + + {title} + + ) : ( +
+ )} +
{content}
+ {details ? {details} :
} + + {isAlertsExists ? ( + + {alerts ? ( + + {alertStats ? ( + + + + ) : ( +
+ )} + {alerts.map((alert) => ( + // biome-ignore lint/correctness/useJsxKeyInIterable: not reordered + + + + ))} + ) : (
)} - {alerts.map((alert) => ( - // biome-ignore lint/correctness/useJsxKeyInIterable: not reordered - - - - ))} - + ) : (
)} ) : ( -
- )} - - ) : ( - - - {title ? {title} :
} -
{content}
- {details ? {details} :
} - - {isAlertsExists ? ( - - - {alerts ? ( - - {alertStats ? ( - - - + + + {title ? {title} :
} +
{content}
+ {details ? {details} :
} + + {isAlertsExists ? ( + + + {alerts ? ( + + {alertStats ? ( + + + + ) : ( +
+ )} + {alerts.map((alert) => ( + // biome-ignore lint/correctness/useJsxKeyInIterable: not reordered + + + + ))} + ) : (
)} - {alerts.map((alert) => ( - // biome-ignore lint/correctness/useJsxKeyInIterable: not reordered - - - - ))} - - ) : ( -
- )} + + + ) : ( +
+ )} + + )} + {isInsightsExists ? ( + insights?.map((insight) => ( + // biome-ignore lint/correctness/useJsxKeyInIterable: not reordered + + - + )) ) : (
)} - - )} - {isInsightsExists ? ( - insights?.map((insight) => ( - // biome-ignore lint/correctness/useJsxKeyInIterable: not reordered - - - - )) - ) : ( -
- )} - - - - ) - - return ( - - {ItemWrapper ? ItemWrapper({ id, children: Component }) : Component} + + + + ) } diff --git a/ui/packages/evidently-ui-lib/src/widgets/WidgetRenderer.tsx b/ui/packages/evidently-ui-lib/src/widgets/WidgetRenderer.tsx index c0713206cb..587ccc99d4 100644 --- a/ui/packages/evidently-ui-lib/src/widgets/WidgetRenderer.tsx +++ b/ui/packages/evidently-ui-lib/src/widgets/WidgetRenderer.tsx @@ -46,11 +46,7 @@ function sizeTransform(size: WidgetSize): 1 | 3 | 6 | 12 { return 12 } -export function WidgetRenderer( - key: string, - info: WidgetInfo, - ItemWrapper?: ({ id, children }: { id: string; children: React.ReactNode }) => React.ReactNode -) { +export function WidgetRenderer(key: string, info: WidgetInfo) { let content = if (info.type === 'counter') { content = @@ -102,7 +98,7 @@ export function WidgetRenderer( content = } return ( - + {{ ...info, content: content