simplify UptimeDataTable
This commit is contained in:
1 parent
6788b5672e
commit
ef28f8c38b
2 files changed
+41
-89
No files matched your search
@@ -7,12 +7,9 @@ import Host from "../Host";
|
|||||||
import { StatusLabel } from "../../../../../Components/Label";
|
import { StatusLabel } from "../../../../../Components/Label";
|
||||||
import BarChart from "../../../../../Components/Charts/BarChart";
|
import BarChart from "../../../../../Components/Charts/BarChart";
|
||||||
import ActionsMenu from "../ActionsMenu";
|
import ActionsMenu from "../ActionsMenu";
|
||||||
import { useState } from "react";
|
|
||||||
import SearchComponent from "../SearchComponent";
|
|
||||||
import CircularCount from "../../../../../Components/CircularCount";
|
|
||||||
import LoadingSpinner from "../LoadingSpinner";
|
import LoadingSpinner from "../LoadingSpinner";
|
||||||
import UptimeDataTableSkeleton from "./skeleton";
|
import UptimeDataTableSkeleton from "./skeleton";
|
||||||
import { Heading } from "../../../../../Components/Heading";
|
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
@@ -20,35 +17,6 @@ import useUtils from "../../Hooks/useUtils";
|
|||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
const MonitorDataTable = ({ shouldRender, isSearching, headers, filteredMonitors }) => {
|
|
||||||
const theme = useTheme();
|
|
||||||
const navigate = useNavigate();
|
|
||||||
|
|
||||||
if (!shouldRender) return null;
|
|
||||||
return (
|
|
||||||
<Box position="relative">
|
|
||||||
<LoadingSpinner shouldRender={isSearching} />
|
|
||||||
<DataTable
|
|
||||||
headers={headers}
|
|
||||||
data={filteredMonitors}
|
|
||||||
config={{
|
|
||||||
rowSX: {
|
|
||||||
cursor: "pointer",
|
|
||||||
"&:hover td": {
|
|
||||||
backgroundColor: theme.palette.tertiary.main,
|
|
||||||
transition: "background-color .3s ease",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
onRowClick: (row) => {
|
|
||||||
navigate(`/uptime/${row.id}`);
|
|
||||||
},
|
|
||||||
emptyView: "No monitors found",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UptimeDataTable displays a table of uptime monitors with sorting, searching, and action capabilities
|
* UptimeDataTable displays a table of uptime monitors with sorting, searching, and action capabilities
|
||||||
* @param {Object} props - Component props
|
* @param {Object} props - Component props
|
||||||
@@ -78,34 +46,29 @@ const MonitorDataTable = ({ shouldRender, isSearching, headers, filteredMonitors
|
|||||||
* @param {Function} props.triggerUpdate - Callback to trigger a data refresh
|
* @param {Function} props.triggerUpdate - Callback to trigger a data refresh
|
||||||
* @returns {JSX.Element} Rendered component
|
* @returns {JSX.Element} Rendered component
|
||||||
*/
|
*/
|
||||||
const UptimeDataTable = (props) => {
|
const UptimeDataTable = ({
|
||||||
|
isAdmin,
|
||||||
|
isSearching,
|
||||||
|
setIsLoading,
|
||||||
|
filteredMonitors,
|
||||||
|
sort,
|
||||||
|
setSort,
|
||||||
|
triggerUpdate,
|
||||||
|
monitorsAreLoading,
|
||||||
|
}) => {
|
||||||
// Utils
|
// Utils
|
||||||
|
const navigate = useNavigate();
|
||||||
const {
|
|
||||||
isAdmin,
|
|
||||||
setIsLoading,
|
|
||||||
monitors,
|
|
||||||
filteredMonitors,
|
|
||||||
monitorCount,
|
|
||||||
sort,
|
|
||||||
setSort,
|
|
||||||
setSearch,
|
|
||||||
triggerUpdate,
|
|
||||||
monitorsAreLoading,
|
|
||||||
} = props;
|
|
||||||
const { determineState } = useUtils();
|
const { determineState } = useUtils();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
|
||||||
|
|
||||||
// Local state
|
// Local state
|
||||||
const [isSearching, setIsSearching] = useState(false);
|
|
||||||
// Handlers
|
// Handlers
|
||||||
const handleSort = (field) => {
|
const handleSort = (field) => {
|
||||||
let order = "";
|
let order = "";
|
||||||
if (sort.field !== field) {
|
if (sort?.field !== field) {
|
||||||
order = "desc";
|
order = "desc";
|
||||||
} else {
|
} else {
|
||||||
order = sort.order === "asc" ? "desc" : "asc";
|
order = sort?.order === "asc" ? "desc" : "asc";
|
||||||
}
|
}
|
||||||
setSort({ field, order });
|
setSort({ field, order });
|
||||||
};
|
};
|
||||||
@@ -124,10 +87,10 @@ const UptimeDataTable = (props) => {
|
|||||||
<Stack
|
<Stack
|
||||||
justifyContent="center"
|
justifyContent="center"
|
||||||
style={{
|
style={{
|
||||||
visibility: sort.field === "name" ? "visible" : "hidden",
|
visibility: sort?.field === "name" ? "visible" : "hidden",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{sort.order === "asc" ? (
|
{sort?.order === "asc" ? (
|
||||||
<ArrowUpwardRoundedIcon />
|
<ArrowUpwardRoundedIcon />
|
||||||
) : (
|
) : (
|
||||||
<ArrowDownwardRoundedIcon />
|
<ArrowDownwardRoundedIcon />
|
||||||
@@ -160,10 +123,10 @@ const UptimeDataTable = (props) => {
|
|||||||
<Stack
|
<Stack
|
||||||
justifyContent="center"
|
justifyContent="center"
|
||||||
style={{
|
style={{
|
||||||
visibility: sort.field === "status" ? "visible" : "hidden",
|
visibility: sort?.field === "status" ? "visible" : "hidden",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{sort.order === "asc" ? (
|
{sort?.order === "asc" ? (
|
||||||
<ArrowUpwardRoundedIcon fontSize="18px" />
|
<ArrowUpwardRoundedIcon fontSize="18px" />
|
||||||
) : (
|
) : (
|
||||||
<ArrowDownwardRoundedIcon fontSize="18px" />
|
<ArrowDownwardRoundedIcon fontSize="18px" />
|
||||||
@@ -210,41 +173,37 @@ const UptimeDataTable = (props) => {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
return (
|
|
||||||
<Box
|
|
||||||
flex={1}
|
|
||||||
py={theme.spacing(8)}
|
|
||||||
>
|
|
||||||
<Stack
|
|
||||||
direction="row"
|
|
||||||
alignItems="center"
|
|
||||||
mb={theme.spacing(8)}
|
|
||||||
gap={theme.spacing(2)}
|
|
||||||
>
|
|
||||||
<Heading component="h2">Uptime monitors</Heading>
|
|
||||||
<CircularCount count={monitorCount} />
|
|
||||||
<SearchComponent
|
|
||||||
monitorsAreLoading={monitorsAreLoading}
|
|
||||||
monitors={monitors}
|
|
||||||
onSearchChange={setSearch}
|
|
||||||
setIsSearching={setIsSearching}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
|
|
||||||
<MonitorDataTable
|
if (monitorsAreLoading) {
|
||||||
shouldRender={!monitorsAreLoading}
|
return <UptimeDataTableSkeleton />;
|
||||||
isSearching={isSearching}
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box position="relative">
|
||||||
|
<LoadingSpinner shouldRender={isSearching} />
|
||||||
|
<DataTable
|
||||||
headers={headers}
|
headers={headers}
|
||||||
filteredMonitors={filteredMonitors}
|
data={filteredMonitors}
|
||||||
|
config={{
|
||||||
|
rowSX: {
|
||||||
|
cursor: "pointer",
|
||||||
|
"&:hover td": {
|
||||||
|
backgroundColor: theme.palette.tertiary.main,
|
||||||
|
transition: "background-color .3s ease",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
onRowClick: (row) => {
|
||||||
|
navigate(`/uptime/${row.id}`);
|
||||||
|
},
|
||||||
|
emptyView: "No monitors found",
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<UptimeDataTableSkeleton shouldRender={monitorsAreLoading} />
|
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
UptimeDataTable.propTypes = {
|
UptimeDataTable.propTypes = {
|
||||||
isSearching: PropTypes.bool,
|
isSearching: PropTypes.bool,
|
||||||
setIsSearching: PropTypes.func,
|
|
||||||
setSort: PropTypes.func,
|
setSort: PropTypes.func,
|
||||||
setSearch: PropTypes.func,
|
setSearch: PropTypes.func,
|
||||||
setIsLoading: PropTypes.func,
|
setIsLoading: PropTypes.func,
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { Skeleton } from "@mui/material";
|
import { Skeleton } from "@mui/material";
|
||||||
import PropTypes from "prop-types";
|
|
||||||
|
|
||||||
const UptimeDataTableSkeleton = ({ shouldRender }) => {
|
|
||||||
if (!shouldRender) return null;
|
|
||||||
|
|
||||||
|
const UptimeDataTableSkeleton = () => {
|
||||||
return (
|
return (
|
||||||
<Skeleton
|
<Skeleton
|
||||||
variant="rounded"
|
variant="rounded"
|
||||||
@@ -14,8 +11,4 @@ const UptimeDataTableSkeleton = ({ shouldRender }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
UptimeDataTableSkeleton.propTypes = {
|
|
||||||
shouldRender: PropTypes.bool.isRequired,
|
|
||||||
};
|
|
||||||
|
|
||||||
export default UptimeDataTableSkeleton;
|
export default UptimeDataTableSkeleton;
|
||||||
Reference in new issue
Block a user