Use react table instead of data grid for performance

This commit is contained in:
Alex Holliday committed 2024-07-06 21:59:41 -07:00
1 parent 30c9c1d0a3
commit 6a7a919789
1 file changed
+101 -81
+101 -81
View File
@@ -3,117 +3,137 @@ import PropTypes from "prop-types";
import { DataGrid } from "@mui/x-data-grid"; import { DataGrid } from "@mui/x-data-grid";
import OpenIt from "../../assets/Images/Icon-open-in-tab-gray.png"; import OpenIt from "../../assets/Images/Icon-open-in-tab-gray.png";
import ResponseTimeChart from "../Charts/ResponseTimeChart"; import ResponseTimeChart from "../Charts/ResponseTimeChart";
import { useState, useEffect } from "react"; import {
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper,
} from "@mui/material";
/**
* Host component.
* This subcomponent receives a params object and displays the host details.
*
* @component
* @param {Object} params - An object containing the following properties:
* @param {string} params.url - The URL of the host.
* @param {string} params.title - The name of the host.
* @param {string} params.percentageColor - The color of the percentage text.
* @param {number} params.precentage - The percentage to display.
* @returns {React.ElementType} Returns a div element with the host details.
*/
const Host = ({ params }) => { const Host = ({ params }) => {
return ( return (
<div className="host-row"> <div className="host-row">
<a href={params.value.url} target="_blank"> <a href={params.url} target="_blank">
<img className="host-open-icon" src={OpenIt} alt="OpenIt" /> <img className="host-open-icon" src={OpenIt} alt="OpenIt" />
</a> </a>
<div className="host-name">{params.value.title}</div> <div className="host-name">{params.title}</div>
<div <div
className="host-percentage" className="host-percentage"
style={{ color: params.value.percentageColor }} style={{ color: params.percentageColor }}
> >
{params.value.precentage}% {params.precentage}%
</div> </div>
</div> </div>
); );
}; };
/**
* Status component.
* This subcomponent receives a params object and displays the status details of a monitor.
*
* @component
* @param {Object} params - An object containing the following properties:
* @param {string} params.backgroundColor - The background color of the status box.
* @param {string} params.statusDotColor - The color of the status dot.
* @param {string} params.status - The status text to display.
* @returns {React.ElementType} Returns a div element with the host status.
*/
const Status = ({ params }) => { const Status = ({ params }) => {
return ( return (
<div className="host-status"> <div className="host-status">
<div <div
className="host-status-details" className="host-status-details"
style={{ backgroundColor: params.value.backgroundColor }} style={{ backgroundColor: params.backgroundColor }}
> >
<div <div
className="host-status-dot" className="host-status-dot"
style={{ backgroundColor: params.value.statusDotColor }} style={{ backgroundColor: params.statusDotColor }}
/> />
<span className="host-status-text">{params.value.status}</span> <span className="host-status-text">{params.status}</span>
</div> </div>
</div> </div>
); );
}; };
const cols = [ /**
{ * MonitorTable component.
field: "host", * Takes an array of monitor objects and displays them in a table.
headerName: "Host", * Each row in the table represents a monitor and includes the host, status, response time, and action.
renderCell: (params) => { *
return <Host params={params} />; * @component
}, * @param {Object[]} monitors - An array of monitor objects. Each object should have the following properties:
flex: 1, * @param {string} monitors[].url - The URL of the monitor.
}, * @param {string} monitors[].name - The name of the monitor.
{ * @param {boolean} monitors[].status - The status of the monitor. True if the monitor is up, false otherwise.
field: "status", * @param {Object[]} monitors[].checks - An array of check objects for the response time chart.
headerName: "Status", * @returns {React.Component} Returns a table with the monitor data.
renderCell: (params) => { */
return <Status params={params} />;
},
},
{
field: "response",
headerName: "Response",
renderCell: (params) => {
return <ResponseTimeChart checks={params.value.checks} />;
},
flex: 1,
},
{
field: "actions",
headerName: "Actions",
},
];
const MonitorTable = ({ monitors = [] }) => { const MonitorTable = ({ monitors = [] }) => {
const [rows, setRows] = useState([]); const mappedRows = monitors.map((monitor) => {
useEffect(() => { const params = {
const mappedRows = monitors.map((monitor) => { url: monitor.url,
return { title: monitor.name,
id: monitor._id, precentage: 100,
host: { percentageColor:
title: monitor.name, monitor.status === true
url: monitor.url, ? "var(--env-var-color-17)"
precentage: 100, : "var(--env-var-color-19)",
percentageColor: status: monitor.status === true ? "up" : "down",
monitor.status === true backgroundColor:
? "var(--env-var-color-17)" monitor.status === true
: "var(--env-var-color-19)", ? "var(--env-var-color-20)"
}, : "var(--env-var-color-21)",
status: { statusDotColor:
status: monitor.status === true ? "Up" : "Down", monitor.status === true
backgroundColor: ? "var(--env-var-color-17)"
monitor.status === true : "var(--env-var-color-19)",
? "var(--env-var-color-20)" };
: "var(--env-var-color-21)",
statusDotColor: return (
monitor.status === true <TableRow key={monitor._id}>
? "var(--env-var-color-17)" <TableCell>
: "var(--env-var-color-19)", <Host params={params} />
}, </TableCell>
response: { <TableCell>
checks: monitor.checks, <Status params={params} />
}, </TableCell>
}; <TableCell>
}); <ResponseTimeChart checks={monitor.checks} />
setRows(mappedRows); </TableCell>
}, [monitors]); <TableCell>TODO Add Actions</TableCell>
</TableRow>
);
});
return ( return (
<DataGrid <TableContainer component={Paper}>
autoHeight <Table>
columns={cols} <TableHead>
rows={rows} <TableRow>
initialState={{ <TableCell>Host</TableCell>
pagination: { <TableCell>Status</TableCell>
paginationModel: { page: 0, pageSize: 10 }, <TableCell>Response Time</TableCell>
}, <TableCell>Action</TableCell>
}} </TableRow>
pageSizeOptions={[5, 10]} </TableHead>
/> <TableBody>{mappedRows}</TableBody>
</Table>
</TableContainer>
); );
}; };