import React, { useEffect, useState } from 'react'; import { MetricType } from '@src/api'; import { FormattedMetricData } from '@src/constants/chartConfig'; import { AppContainer, Empty, SingleChart, Spin, Tooltip } from 'knowdesign'; import { arrayMoveImmutable } from 'array-move'; import DragGroup from '../DragGroup'; import { IconFont } from '@knowdesign/icons'; import { getChartConfig } from './config'; import { EventBus } from 'knowdesign/lib/utils/event-bus'; const DRAG_GROUP_GUTTER_NUM: [number, number] = [16, 16]; interface ChartListProps { busInstance: EventBus; loading: boolean; gridNum: number; data: FormattedMetricData[]; autoReload: boolean; dragCallback: (oldIndex: number, newIndex: number) => void; onExpand: (metricName: string, metricType: MetricType) => void; } const ChartList = (props: ChartListProps) => { const { loading, gridNum, data, autoReload, busInstance, dragCallback, onExpand } = props; const [global] = AppContainer.useGlobalValue(); const [chartData, setChartData] = useState(data); // 拖拽开始回调,触发图表的 onDrag 事件( 设置为 true ),禁止同步展示图表的 tooltip const dragStart = () => { busInstance.emit('onDrag', true); }; // 拖拽结束回调,更新图表顺序,并触发图表的 onDrag 事件( 设置为 false ),允许同步展示图表的 tooltip const dragEnd = ({ oldIndex, newIndex }: { oldIndex: number; newIndex: number }) => { dragCallback(oldIndex, newIndex); busInstance.emit('onDrag', false); setChartData(arrayMoveImmutable(chartData, oldIndex, newIndex)); }; // 监听盒子宽度变化,重置图表宽度 const observeDashboardWidthChange = () => { const targetNode = document.getElementsByClassName('dcd-two-columns-layout-sider-footer')[0]; targetNode && targetNode.addEventListener('click', () => busInstance.emit('chartResize')); }; useEffect(() => { setChartData(data); }, [data]); useEffect(() => { setTimeout(() => observeDashboardWidthChange()); }, []); return ( <>
{chartData && chartData.length ? (
{chartData.map((data) => { const { metricName, metricType, metricUnit, metricLines, showLegend } = data; return (
{ let content = ''; const metricDefine = global.getMetricDefine(metricType, metricName); if (metricDefine) { content = metricDefine.desc; } return content; }} > {metricName} ({metricUnit}) {(metricType === MetricType.Connect || metricType === MetricType.Connectors) && ( {metricType === MetricType.Connect ? 'Cluster' : 'Connector'} )}
{metricLines?.length > 0 && (
onExpand(metricName, metricType)}>
)} {metricLines?.length > 0 ? ( ) : ( )}
); })}
) : loading ? ( <> ) : ( )}
); }; export default ChartList;