123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import { FC, useState, useEffect } from 'react';
- import { useTranslation } from 'react-i18next';
- import { makeStyles, Theme } from '@material-ui/core';
- import KeyboardArrowDown from '@material-ui/icons/KeyboardArrowDown';
- import { DataGrid } from '@mui/x-data-grid';
- import { useNavigationHook } from '@/hooks';
- import { ALL_ROUTER_TYPES } from '@/router/Types';
- import MiniTopo from './MiniTopology';
- import { getByteString, formatByteSize } from '@/utils';
- import DataCard from './DataCard';
- import { NodeListViewProps, Node } from './Types';
- const getStyles = makeStyles((theme: Theme) => ({
- root: {
- margin: '14px 40px',
- display: 'grid',
- gridTemplateColumns: 'auto 400px',
- gridTemplateRows: '40px 400px auto',
- gridTemplateAreas: `"a a"
- "b ."
- "b d"`,
- },
- cardContainer: {
- display: 'grid',
- gap: '16px',
- gridTemplateColumns: 'repeat(4, minmax(300px, 1fr))',
- },
- contentContainer: {
- borderRadius: '8px',
- boxShadow: '3px 3px 10px rgba(0, 0, 0, 0.05)',
- display: 'grid',
- marginTop: '14px',
- },
- childView: {
- height: '100%',
- width: '100%',
- transition: 'all .25s',
- position: 'absolute',
- // zIndex: 1000,
- backgroundColor: 'white',
- },
- childCloseBtn: {
- border: 0,
- backgroundColor: 'white',
- gridArea: 'a',
- cursor: 'pointer',
- width: '100%',
- },
- gridContainer: {
- gridArea: 'b',
- display: 'flex',
- },
- dataCard: {
- gridArea: 'd',
- },
- }));
- const NodeListView: FC<NodeListViewProps> = props => {
- useNavigationHook(ALL_ROUTER_TYPES.SYSTEM);
- const { t } = useTranslation('systemView');
- const { t: commonTrans } = useTranslation();
- const capacityTrans: { [key in string]: string } = commonTrans('capacity');
- const classes = getStyles();
- const [selectedChildNode, setSelectedChildNode] = useState<
- Node | undefined
- >();
- const [rows, setRows] = useState<any[]>([]);
- const { selectedCord, childNodes, setCord } = props;
- const columns: any[] = [
- {
- field: 'name',
- headerName: t('thName'),
- flex: 1,
- },
- {
- field: 'ip',
- headerName: t('thIP'),
- flex: 1,
- },
- {
- field: 'cpuCore',
- headerName: t('thCPUCount'),
- flex: 1,
- },
- {
- field: 'cpuUsage',
- headerName: t('thCPUUsage'),
- flex: 1,
- },
- {
- field: 'diskUsage',
- headerName: t('thDiskUsage'),
- flex: 1,
- },
- {
- field: 'memUsage',
- headerName: t('thMemUsage'),
- flex: 1,
- },
- ];
- useEffect(() => {
- if (selectedCord) {
- const connectedIds = selectedCord.connected.map(
- node => node.connected_identifier
- );
- const newRows: any[] = [];
- childNodes.forEach(node => {
- if (connectedIds.includes(node.identifier)) {
- const memUsage = formatByteSize(
- node?.infos?.hardware_infos.memory_usage,
- capacityTrans
- );
- const dataRow = {
- id: node?.identifier,
- ip: node?.infos?.hardware_infos.ip,
- cpuCore: node?.infos?.hardware_infos.cpu_core_count,
- cpuUsage: Number(
- node?.infos?.hardware_infos.cpu_core_usage
- ).toFixed(2),
- diskUsage: getByteString(
- node?.infos?.hardware_infos.disk_usage,
- node?.infos?.hardware_infos.disk,
- capacityTrans
- ),
- // memUsage: getByteString(
- // node?.infos?.hardware_infos.memory_usage,
- // node?.infos?.hardware_infos.memory,
- // capacityTrans
- // ),
- memUsage: `${memUsage.value}${memUsage.unit}`,
- name: node?.infos?.name,
- };
- newRows.push(dataRow);
- }
- });
- setRows(newRows);
- }
- }, [selectedCord, childNodes, capacityTrans]);
- // select first node
- useEffect(() => {
- const timeoutID = window.setTimeout(() => {
- const el = document.querySelectorAll<HTMLElement>('.MuiDataGrid-row')[0];
- if (el instanceof HTMLElement) {
- el.click();
- }
- }, 300);
- return () => window.clearTimeout(timeoutID);
- }, [childNodes]);
- return (
- <div className={classes.root}>
- <button className={classes.childCloseBtn} onClick={() => setCord(null)}>
- <KeyboardArrowDown />
- </button>
- <div className={classes.gridContainer}>
- <DataGrid
- rows={rows}
- columns={columns}
- hideFooterPagination
- hideFooterSelectedRowCount
- onRowClick={rowData => {
- const selectedNode = childNodes.find(
- node => rowData.row.id === node.identifier
- );
- setSelectedChildNode(selectedNode);
- }}
- />
- </div>
- <MiniTopo
- selectedCord={selectedCord}
- setCord={setCord}
- selectedChildNode={selectedChildNode}
- />
- <DataCard className={classes.dataCard} node={selectedChildNode} />
- </div>
- );
- };
- export default NodeListView;
|