NodeListView.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { FC, useState, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { makeStyles, Theme } from '@material-ui/core';
  4. import KeyboardArrowDown from '@material-ui/icons/KeyboardArrowDown';
  5. import { DataGrid } from '@mui/x-data-grid';
  6. import { useNavigationHook } from '../../hooks/Navigation';
  7. import { ALL_ROUTER_TYPES } from '../../router/Types';
  8. import MiniTopo from './MiniTopology';
  9. import { getByteString } from '../../utils/Format';
  10. import DataCard from './DataCard';
  11. import { NodeListViewProps, Node } from './Types';
  12. const getStyles = makeStyles((theme: Theme) => ({
  13. root: {
  14. fontFamily: 'Roboto',
  15. margin: '14px 40px',
  16. display: 'grid',
  17. gridTemplateColumns: 'auto 400px',
  18. gridTemplateRows: '40px 400px auto',
  19. gridTemplateAreas:
  20. `"a a"
  21. "b ."
  22. "b d"`,
  23. height: 'calc(100% - 28px)',
  24. },
  25. cardContainer: {
  26. display: 'grid',
  27. gap: '16px',
  28. gridTemplateColumns: 'repeat(4, minmax(300px, 1fr))',
  29. },
  30. contentContainer: {
  31. borderRadius: '8px',
  32. boxShadow: '3px 3px 10px rgba(0, 0, 0, 0.05)',
  33. display: 'grid',
  34. marginTop: '14px',
  35. },
  36. childView: {
  37. height: '100%',
  38. width: '100%',
  39. transition: 'all .25s',
  40. position: 'absolute',
  41. zIndex: 1000,
  42. backgroundColor: 'white',
  43. },
  44. childCloseBtn: {
  45. border: 0,
  46. backgroundColor: 'white',
  47. gridArea: 'a',
  48. cursor: 'pointer',
  49. width: '100%',
  50. },
  51. gridContainer: {
  52. gridArea: 'b',
  53. display: 'flex',
  54. },
  55. dataCard: {
  56. gridArea: 'd',
  57. }
  58. }));
  59. const NodeListView: FC<NodeListViewProps> = (props) => {
  60. useNavigationHook(ALL_ROUTER_TYPES.SYSTEM);
  61. const { t } = useTranslation('systemView');
  62. const { t: commonTrans } = useTranslation();
  63. const capacityTrans: { [key in string]: string } = commonTrans('capacity');
  64. const classes = getStyles();
  65. const [selectedChildNode, setSelectedChildNode] = useState<Node | undefined>();
  66. const [rows, setRows] = useState<any[]>([]);
  67. const { selectedCord, childNodes, setCord } = props;
  68. let columns: any[] = [
  69. {
  70. field: 'name',
  71. headerName: t('thName'),
  72. flex: 1,
  73. },
  74. {
  75. field: 'ip',
  76. headerName: t('thIP'),
  77. flex: 1,
  78. },
  79. {
  80. field: 'cpuCore',
  81. headerName: t('thCPUCount'),
  82. flex: 1,
  83. },
  84. {
  85. field: 'cpuUsage',
  86. headerName: t('thCPUUsage'),
  87. flex: 1,
  88. },
  89. {
  90. field: 'diskUsage',
  91. headerName: t('thDiskUsage'),
  92. flex: 1,
  93. },
  94. {
  95. field: 'memUsage',
  96. headerName: t('thMemUsage'),
  97. flex: 1,
  98. },
  99. ];
  100. useEffect(() => {
  101. if (selectedCord) {
  102. const connectedIds = selectedCord.connected.map(node => node.connected_identifier);
  103. const newRows: any[] = [];
  104. childNodes.forEach(node => {
  105. if (connectedIds.includes(node.identifier)) {
  106. const dataRow = {
  107. id: node?.identifier,
  108. ip: node?.infos?.hardware_infos.ip,
  109. cpuCore: node?.infos?.hardware_infos.cpu_core_count,
  110. cpuUsage: node?.infos?.hardware_infos.cpu_core_usage,
  111. diskUsage: getByteString(node?.infos?.hardware_infos.disk_usage, node?.infos?.hardware_infos.disk, capacityTrans),
  112. memUsage: getByteString(node?.infos?.hardware_infos.memory_usage, node?.infos?.hardware_infos.memory, capacityTrans),
  113. name: node?.infos?.name,
  114. }
  115. newRows.push(dataRow);
  116. }
  117. })
  118. setRows(newRows);
  119. }
  120. }, [selectedCord, childNodes, capacityTrans]);
  121. return (
  122. <div className={classes.root}>
  123. <button className={classes.childCloseBtn} onClick={() => setCord(null)}>
  124. <KeyboardArrowDown />
  125. </button>
  126. <div className={classes.gridContainer}>
  127. <DataGrid
  128. rows={rows}
  129. columns={columns}
  130. hideFooterPagination
  131. hideFooterSelectedRowCount
  132. onRowClick={(rowData) => {
  133. const selectedNode = childNodes.find(node => rowData.row.id === node.identifier);
  134. setSelectedChildNode(selectedNode);
  135. }}
  136. />
  137. </div>
  138. <MiniTopo selectedCord={selectedCord} setCord={setCord} selectedChildNode={selectedChildNode} />
  139. <DataCard className={classes.dataCard} node={selectedChildNode} />
  140. </div>
  141. );
  142. };
  143. export default NodeListView;