From 1e7e7c736505009f8b67d6991daf3b98aa94e9e5 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 6 Feb 2025 14:57:28 -0800 Subject: [PATCH] refactor DU monitors page --- .../Components/MonitorTable/index.jsx | 91 ++++++++ .../Monitors/Hooks/useSubscribeToMonitors.jsx | 58 +++++ .../DistributedUptime/Monitors/index.jsx | 221 +++++------------- 3 files changed, 202 insertions(+), 168 deletions(-) create mode 100644 Client/src/Pages/DistributedUptime/Monitors/Components/MonitorTable/index.jsx create mode 100644 Client/src/Pages/DistributedUptime/Monitors/Hooks/useSubscribeToMonitors.jsx diff --git a/Client/src/Pages/DistributedUptime/Monitors/Components/MonitorTable/index.jsx b/Client/src/Pages/DistributedUptime/Monitors/Components/MonitorTable/index.jsx new file mode 100644 index 000000000..ad0afe1a8 --- /dev/null +++ b/Client/src/Pages/DistributedUptime/Monitors/Components/MonitorTable/index.jsx @@ -0,0 +1,91 @@ +import DataTable from "../../../../../Components/Table"; +import BarChart from "../../../../../Components/Charts/BarChart"; +import { Box } from "@mui/material"; +import { StatusLabel } from "../../../../../Components/Label"; + +//REFACTOR +import Host from "../../../../Uptime/Monitors/Components/Host"; +import ActionsMenu from "../../../../Uptime/Monitors/Components/ActionsMenu"; +import { useTheme } from "@mui/material/styles"; +import { useNavigate } from "react-router-dom"; +import { useMonitorUtils } from "../../../../../Hooks/useMonitorUtils"; +const TYPE_MAP = { + distributed_http: "Distributed HTTP", +}; + +const MonitorTable = ({ isLoading, monitors }) => { + const theme = useTheme(); + const navigate = useNavigate(); + const { determineState } = useMonitorUtils(); + + const headers = [ + { + id: "name", + content: Host, + render: (row) => ( + + ), + }, + { + id: "status", + content: Status, + render: (row) => { + const status = determineState(row?.monitor); + return ( + + ); + }, + }, + { + id: "responseTime", + content: "Response Time", + render: (row) => , + }, + { + id: "type", + content: "Type", + render: (row) => {TYPE_MAP[row.monitor.type]}, + }, + { + id: "actions", + content: "Actions", + render: (row) => ( + + ), + }, + ]; + + return ( + { + navigate(`/distributed-uptime/${row.id}`); + }, + }} + /> + ); +}; + +export default MonitorTable; diff --git a/Client/src/Pages/DistributedUptime/Monitors/Hooks/useSubscribeToMonitors.jsx b/Client/src/Pages/DistributedUptime/Monitors/Hooks/useSubscribeToMonitors.jsx new file mode 100644 index 000000000..1a1db1453 --- /dev/null +++ b/Client/src/Pages/DistributedUptime/Monitors/Hooks/useSubscribeToMonitors.jsx @@ -0,0 +1,58 @@ +import { useEffect, useState } from "react"; +import { networkService } from "../../../../main"; +import { useSelector } from "react-redux"; +import { useTheme } from "@emotion/react"; +import { useMonitorUtils } from "../../../../Hooks/useMonitorUtils"; +import { createToast } from "../../../../Utils/toastUtils"; + +const useSubscribeToMonitors = () => { + // Redux + const { authToken, user } = useSelector((state) => state.auth); + + // Local state + const [isLoading, setIsLoading] = useState(true); + const [networkError, setNetworkError] = useState(false); + const [monitors, setMonitors] = useState(undefined); + const [monitorsSummary, setMonitorsSummary] = useState(undefined); + const [filteredMonitors, setFilteredMonitors] = useState(undefined); + + const theme = useTheme(); + const { getMonitorWithPercentage } = useMonitorUtils(); + + useEffect(() => { + try { + const cleanup = networkService.subscribeToDistributedUptimeMonitors({ + authToken: authToken, + teamId: user.teamId, + limit: 25, + types: ["distributed_http"], + page: 0, + rowsPerPage: 10, + filter: null, + field: null, + order: null, + onUpdate: (data) => { + const res = data.monitors; + const { monitors, filteredMonitors, summary } = res; + const mappedMonitors = filteredMonitors.map((monitor) => + getMonitorWithPercentage(monitor, theme) + ); + setMonitors(monitors); + setMonitorsSummary(summary); + setFilteredMonitors(mappedMonitors); + }, + }); + + return cleanup; + } catch (error) { + createToast({ + body: error.message, + }); + setNetworkError(true); + } finally { + setIsLoading(false); + } + }, [authToken, user, getMonitorWithPercentage, theme]); + return [isLoading, networkError, monitors, monitorsSummary, filteredMonitors]; +}; +export { useSubscribeToMonitors }; diff --git a/Client/src/Pages/DistributedUptime/Monitors/index.jsx b/Client/src/Pages/DistributedUptime/Monitors/index.jsx index 0d1288aa9..34b1a0f21 100644 --- a/Client/src/Pages/DistributedUptime/Monitors/index.jsx +++ b/Client/src/Pages/DistributedUptime/Monitors/index.jsx @@ -1,185 +1,70 @@ // Components -import { Stack, Box, Button } from "@mui/material"; -import DataTable from "../../../Components/Table"; +import { Stack, Typography } from "@mui/material"; import Breadcrumbs from "../../../Components/Breadcrumbs"; -import Host from "../../Uptime/Monitors/Components/Host"; -import BarChart from "../../../Components/Charts/BarChart"; -import ActionsMenu from "../../Uptime/Monitors/Components/ActionsMenu"; -import { StatusLabel } from "../../../Components/Label"; -// Utils -import { networkService } from "../../../main"; -import { useTheme } from "@mui/material/styles"; -import { useNavigate } from "react-router-dom"; -import useUtils from "../../Uptime/Monitors/Hooks/useUtils"; -import { useEffect, useState } from "react"; -import { useSelector } from "react-redux"; +import CreateMonitorHeader from "../../../Components/MonitorCreateHeader"; +import MonitorTable from "./Components/MonitorTable"; +import Fallback from "../../../Components/Fallback"; +import GenericFallback from "../../../Components/GenericFallback"; +// Utils +import { useTheme } from "@mui/material/styles"; +import { useSelector } from "react-redux"; +import { useIsAdmin } from "../../../Hooks/useIsAdmin"; +import { useSubscribeToMonitors } from "./Hooks/useSubscribeToMonitors"; // Constants const BREADCRUMBS = [{ name: `Distributed Uptime`, path: "/distributed-uptime" }]; -const TYPE_MAP = { - distributed_http: "Distributed HTTP", -}; const DistributedUptimeMonitors = () => { // Redux state const { authToken, user } = useSelector((state) => state.auth); // Local state - const [monitors, setMonitors] = useState([]); - const [filteredMonitors, setFilteredMonitors] = useState([]); - const [monitorsSummary, setMonitorsSummary] = useState({}); // Utils - - const { determineState } = useUtils(); const theme = useTheme(); - const navigate = useNavigate(); - const headers = [ - { - id: "name", - content: Host, - render: (row) => ( - - ), - }, - { - id: "status", - content: Status, - render: (row) => { - const status = determineState(row?.monitor); - return ( - - ); - }, - }, - { - id: "responseTime", - content: "Response Time", - render: (row) => , - }, - { - id: "type", - content: "Type", - render: (row) => {TYPE_MAP[row.monitor.type]}, - }, - { - id: "actions", - content: "Actions", - render: (row) => ( - - ), - }, - ]; + const isAdmin = useIsAdmin(); + const [isLoading, networkError, monitors, monitorsSummary, filteredMonitors] = + useSubscribeToMonitors(); - const getMonitorWithPercentage = (monitor, theme) => { - let uptimePercentage = ""; - let percentageColor = ""; - - if (monitor.uptimePercentage !== undefined) { - uptimePercentage = - monitor.uptimePercentage === 0 - ? "0" - : (monitor.uptimePercentage * 100).toFixed(2); - - percentageColor = - monitor.uptimePercentage < 0.25 - ? theme.palette.error.main - : monitor.uptimePercentage < 0.5 - ? theme.palette.warning.main - : monitor.uptimePercentage < 0.75 - ? theme.palette.success.main - : theme.palette.success.main; - } - - return { - id: monitor._id, - name: monitor.name, - url: monitor.url, - title: monitor.name, - percentage: uptimePercentage, - percentageColor, - monitor: monitor, - }; - }; - - useEffect(() => { - const cleanup = networkService.subscribeToDistributedUptimeMonitors({ - authToken: authToken, - teamId: user.teamId, - limit: 25, - types: ["distributed_http"], - page: 0, - rowsPerPage: 10, - filter: null, - field: null, - order: null, - onUpdate: (data) => { - const res = data.monitors; - const { monitors, filteredMonitors, summary } = res; - const mappedMonitors = filteredMonitors.map((monitor) => - getMonitorWithPercentage(monitor, theme) - ); - setMonitors(monitors); - setFilteredMonitors(mappedMonitors); - setMonitorsSummary(summary); - }, - }); - - return cleanup; - }, [user.teamId, authToken, theme]); - - return ( - - - - - - {monitors.length > 0 && ( - { - navigate(`/distributed-uptime/${row.id}`); - }, - }} - /> - )} + Network error + + Please check your connection + + ); + } + + if (typeof monitorsSummary !== "undefined" && monitorsSummary.totalMonitors === 0) { + return ( + + ); + } + return ( + + + + ); };