From 948e5e932b824037799a10b35c4349af814bb3f7 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 12 Jun 2025 12:44:35 +0800 Subject: [PATCH] refactor hooks to common util --- .../checkHooks.js} | 15 +- client/src/Hooks/monitorHooks.js | 379 ++++++++++++++++++ .../src/Hooks/useFetchMonitorsWithChecks.js | 72 ---- .../src/Hooks/useFetchMonitorsWithSummary.js | 37 -- client/src/Hooks/useFetchUptimeMonitorById.js | 35 -- .../src/Hooks/useFetchUptimeMonitorDetails.js | 35 -- client/src/Hooks/useMonitorControls.js | 28 -- client/src/Hooks/useMonitorUtils.js | 22 +- .../Hooks/useHardwareMonitorsFetch.jsx | 38 -- .../Monitors/Hooks/useMonitorFetch.jsx | 46 --- .../Details/Hooks/useMonitorFetch.jsx | 39 -- .../Monitors/Hooks/useMonitorsFetch.jsx | 44 -- .../Uptime/Details/Hooks/useMonitorFetch.jsx | 33 -- .../Monitors/Hooks/useMonitorsFetch.jsx | 77 ---- .../Pages/Uptime/Monitors/Hooks/useUtils.jsx | 110 ----- 15 files changed, 405 insertions(+), 605 deletions(-) rename client/src/{Pages/Uptime/Details/Hooks/useChecksFetch.jsx => Hooks/checkHooks.js} (81%) create mode 100644 client/src/Hooks/monitorHooks.js delete mode 100644 client/src/Hooks/useFetchMonitorsWithChecks.js delete mode 100644 client/src/Hooks/useFetchMonitorsWithSummary.js delete mode 100644 client/src/Hooks/useFetchUptimeMonitorById.js delete mode 100644 client/src/Hooks/useFetchUptimeMonitorDetails.js delete mode 100644 client/src/Hooks/useMonitorControls.js delete mode 100644 client/src/Pages/Infrastructure/Details/Hooks/useHardwareMonitorsFetch.jsx delete mode 100644 client/src/Pages/Infrastructure/Monitors/Hooks/useMonitorFetch.jsx delete mode 100644 client/src/Pages/PageSpeed/Details/Hooks/useMonitorFetch.jsx delete mode 100644 client/src/Pages/PageSpeed/Monitors/Hooks/useMonitorsFetch.jsx delete mode 100644 client/src/Pages/Uptime/Details/Hooks/useMonitorFetch.jsx delete mode 100644 client/src/Pages/Uptime/Monitors/Hooks/useMonitorsFetch.jsx delete mode 100644 client/src/Pages/Uptime/Monitors/Hooks/useUtils.jsx diff --git a/client/src/Pages/Uptime/Details/Hooks/useChecksFetch.jsx b/client/src/Hooks/checkHooks.js similarity index 81% rename from client/src/Pages/Uptime/Details/Hooks/useChecksFetch.jsx rename to client/src/Hooks/checkHooks.js index 586cc8359..cc942b294 100644 --- a/client/src/Pages/Uptime/Details/Hooks/useChecksFetch.jsx +++ b/client/src/Hooks/checkHooks.js @@ -1,14 +1,9 @@ import { useState } from "react"; import { useEffect } from "react"; -import { networkService } from "../../../../main"; -import { createToast } from "../../../../Utils/toastUtils"; -export const useChecksFetch = ({ - monitorId, - monitorType, - dateRange, - page, - rowsPerPage, -}) => { +import { networkService } from "../main"; +import { createToast } from "../Utils/toastUtils"; + +const useFetchChecks = ({ monitorId, monitorType, dateRange, page, rowsPerPage }) => { const [checks, setChecks] = useState(undefined); const [checksCount, setChecksCount] = useState(undefined); const [isLoading, setIsLoading] = useState(false); @@ -47,4 +42,4 @@ export const useChecksFetch = ({ return [checks, checksCount, isLoading, networkError]; }; -export default useChecksFetch; +export { useFetchChecks }; diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js new file mode 100644 index 000000000..a39fd3c5e --- /dev/null +++ b/client/src/Hooks/monitorHooks.js @@ -0,0 +1,379 @@ +import { useEffect, useState } from "react"; +import { networkService } from "../main"; +import { createToast } from "../Utils/toastUtils"; +import { useTheme } from "@emotion/react"; +import { useMonitorUtils } from "./useMonitorUtils"; +import { useNavigate } from "react-router-dom"; + +const useFetchMonitorsWithSummary = ({ teamId, types, monitorUpdateTrigger }) => { + const [isLoading, setIsLoading] = useState(false); + const [monitors, setMonitors] = useState(undefined); + const [monitorsSummary, setMonitorsSummary] = useState(undefined); + const [networkError, setNetworkError] = useState(false); + + useEffect(() => { + const fetchMonitors = async () => { + try { + setIsLoading(true); + const res = await networkService.getMonitorsWithSummaryByTeamId({ + teamId, + types, + }); + const { monitors, summary } = res?.data?.data ?? {}; + setMonitors(monitors); + setMonitorsSummary(summary); + } catch (error) { + console.error(error); + setNetworkError(true); + createToast({ + body: error.message, + }); + } finally { + setIsLoading(false); + } + }; + fetchMonitors(); + }, [teamId, types, monitorUpdateTrigger]); + return [monitors, monitorsSummary, isLoading, networkError]; +}; + +const useFetchMonitorsWithChecks = ({ + teamId, + types, + limit, + page, + rowsPerPage, + filter, + field, + order, + monitorUpdateTrigger, +}) => { + const [isLoading, setIsLoading] = useState(false); + const [count, setCount] = useState(undefined); + const [monitors, setMonitors] = useState(undefined); + const [networkError, setNetworkError] = useState(false); + + const theme = useTheme(); + const { getMonitorWithPercentage } = useMonitorUtils(); + useEffect(() => { + const fetchMonitors = async () => { + try { + setIsLoading(true); + const res = await networkService.getMonitorsWithChecksByTeamId({ + teamId, + limit, + types, + page, + rowsPerPage, + filter, + field, + order, + }); + const { count, monitors } = res?.data?.data ?? {}; + const mappedMonitors = monitors.map((monitor) => + getMonitorWithPercentage(monitor, theme) + ); + setMonitors(mappedMonitors); + setCount(count?.monitorsCount ?? 0); + } catch (error) { + console.error(error); + setNetworkError(true); + createToast({ + body: error.message, + }); + } finally { + setIsLoading(false); + } + }; + fetchMonitors(); + }, [ + field, + filter, + getMonitorWithPercentage, + limit, + order, + page, + rowsPerPage, + teamId, + theme, + types, + monitorUpdateTrigger, + ]); + return [monitors, count, isLoading, networkError]; +}; + +const useFetchMonitorsByTeamId = ({ + teamId, + types, + limit, + page, + rowsPerPage, + filter, + field, + order, + updateTrigger, +}) => { + const [isLoading, setIsLoading] = useState(false); + const [monitors, setMonitors] = useState(undefined); + const [summary, setSummary] = useState(undefined); + const [networkError, setNetworkError] = useState(false); + + useEffect(() => { + const fetchMonitors = async () => { + try { + setIsLoading(true); + const res = await networkService.getMonitorsByTeamId({ + teamId: teamId, + limit, + types, + page, + rowsPerPage, + filter, + field, + order, + }); + if (res?.data?.data?.filteredMonitors) { + setMonitors(res.data.data.filteredMonitors); + setSummary(res.data.data.summary); + } + } catch (error) { + setNetworkError(true); + createToast({ + body: error.message, + }); + } finally { + setIsLoading(false); + } + }; + fetchMonitors(); + }, [teamId, types, limit, page, rowsPerPage, filter, field, order, updateTrigger]); + return [monitors, summary, isLoading, networkError]; +}; + +const useFetchStatsByMonitorId = ({ + monitorId, + sortOrder, + limit, + dateRange, + numToDisplay, + normalize, +}) => { + const [monitor, setMonitor] = useState(undefined); + const [audits, setAudits] = useState(undefined); + const [isLoading, setIsLoading] = useState(true); + const [networkError, setNetworkError] = useState(false); + useEffect(() => { + const fetchMonitor = async () => { + try { + setIsLoading(true); + const res = await networkService.getStatsByMonitorId({ + monitorId: monitorId, + sortOrder, + limit, + dateRange, + numToDisplay, + normalize, + }); + setMonitor(res?.data?.data ?? undefined); + setAudits(res?.data?.data?.checks?.[0]?.audits ?? undefined); + } catch (error) { + setNetworkError(true); + createToast({ body: error.message }); + } finally { + setIsLoading(false); + } + }; + fetchMonitor(); + }, [monitorId, dateRange, numToDisplay, normalize, sortOrder, limit]); + return [monitor, audits, isLoading, networkError]; +}; + +const useFetchMonitorById = ({ monitorId, setMonitor, updateTrigger }) => { + const [isLoading, setIsLoading] = useState(true); + useEffect(() => { + const fetchMonitor = async () => { + try { + setIsLoading(true); + const res = await networkService.getMonitorById({ monitorId: monitorId }); + setMonitor(res.data.data); + } catch (error) { + createToast({ body: error.message }); + } finally { + setIsLoading(false); + } + }; + fetchMonitor(); + }, [monitorId, setMonitor, updateTrigger]); + return [isLoading]; +}; + +const useFetchHardwareMonitorById = ({ monitorId, dateRange }) => { + const [isLoading, setIsLoading] = useState(true); + const [networkError, setNetworkError] = useState(false); + const [monitor, setMonitor] = useState(undefined); + + useEffect(() => { + const fetchMonitor = async () => { + try { + if (!monitorId) { + return { monitor: undefined, isLoading: false, networkError: undefined }; + } + const response = await networkService.getHardwareDetailsByMonitorId({ + monitorId: monitorId, + dateRange: dateRange, + }); + setMonitor(response.data.data); + } catch (error) { + setNetworkError(true); + } finally { + setIsLoading(false); + } + }; + fetchMonitor(); + }, [monitorId, dateRange]); + return [monitor, isLoading, networkError]; +}; + +const useFetchUptimeMonitorById = ({ monitorId, dateRange, trigger }) => { + const [networkError, setNetworkError] = useState(false); + const [isLoading, setIsLoading] = useState(true); + const [monitor, setMonitor] = useState(undefined); + const [monitorStats, setMonitorStats] = useState(undefined); + useEffect(() => { + const fetchMonitors = async () => { + try { + const res = await networkService.getUptimeDetailsById({ + monitorId: monitorId, + dateRange: dateRange, + normalize: true, + }); + const { monitorData, monitorStats } = res?.data?.data ?? {}; + setMonitor(monitorData); + setMonitorStats(monitorStats); + } catch (error) { + setNetworkError(true); + createToast({ body: error.message }); + } finally { + setIsLoading(false); + } + }; + fetchMonitors(); + }, [dateRange, monitorId, trigger]); + return [monitor, monitorStats, isLoading, networkError]; +}; + +const useCreateMonitor = () => { + const [isLoading, setIsLoading] = useState(false); + const navigate = useNavigate(); + const createMonitor = async ({ monitor, redirect }) => { + try { + setIsLoading(true); + await networkService.createMonitor({ monitor }); + createToast({ body: "Monitor created successfully!" }); + if (redirect) { + navigate(redirect); + } + } catch (error) { + createToast({ body: "Failed to create monitor." }); + } finally { + setIsLoading(false); + } + }; + return [createMonitor, isLoading]; +}; + +const useDeleteMonitor = () => { + const [isLoading, setIsLoading] = useState(false); + const navigate = useNavigate(); + const deleteMonitor = async ({ monitor, redirect }) => { + try { + setIsLoading(true); + await networkService.deleteMonitorById({ monitorId: monitor._id }); + createToast({ body: "Monitor deleted successfully!" }); + if (redirect) { + navigate(redirect); + } + } catch (error) { + createToast({ body: "Failed to delete monitor." }); + } finally { + setIsLoading(false); + } + }; + return [deleteMonitor, isLoading]; +}; + +const useUpdateMonitor = () => { + const [isLoading, setIsLoading] = useState(false); + const navigate = useNavigate(); + const updateMonitor = async ({ monitor, redirect }) => { + try { + setIsLoading(true); + const updatedFields = { + name: monitor.name, + description: monitor.description, + interval: monitor.interval, + notifications: monitor.notifications, + matchMethod: monitor.matchMethod, + expectedValue: monitor.expectedValue, + ignoreTlsErrors: monitor.ignoreTlsErrors, + jsonPath: monitor.jsonPath, + ...(monitor.type === "port" && { port: monitor.port }), + ...(monitor.type === "hardware" && { + thresholds: monitor.thresholds, + secret: monitor.secret, + }), + }; + await networkService.updateMonitor({ + monitorId: monitor._id, + updatedFields, + }); + + createToast({ body: "Monitor updated successfully!" }); + if (redirect) { + navigate(redirect); + } + } catch (error) { + createToast({ body: "Failed to update monitor." }); + } finally { + setIsLoading(false); + } + }; + return [updateMonitor, isLoading]; +}; + +const usePauseMonitor = () => { + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(undefined); + const pauseMonitor = async ({ monitorId, triggerUpdate }) => { + try { + setIsLoading(true); + const res = await networkService.pauseMonitorById({ monitorId }); + createToast({ + body: res.data.data.isActive + ? "Monitor resumed successfully" + : "Monitor paused successfully", + }); + triggerUpdate(); + } catch (error) { + setError(error); + } finally { + setIsLoading(false); + } + }; + + return [pauseMonitor, isLoading, error]; +}; + +export { + useFetchMonitorsWithSummary, + useFetchMonitorsWithChecks, + useFetchMonitorsByTeamId, + useFetchStatsByMonitorId, + useFetchMonitorById, + useFetchUptimeMonitorById, + useFetchHardwareMonitorById, + useCreateMonitor, + useDeleteMonitor, + useUpdateMonitor, + usePauseMonitor, +}; diff --git a/client/src/Hooks/useFetchMonitorsWithChecks.js b/client/src/Hooks/useFetchMonitorsWithChecks.js deleted file mode 100644 index a6f0437b9..000000000 --- a/client/src/Hooks/useFetchMonitorsWithChecks.js +++ /dev/null @@ -1,72 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../main"; -import { createToast } from "../Utils/toastUtils"; -import { useTheme } from "@emotion/react"; -import { useMonitorUtils } from "./useMonitorUtils"; - -export const useFetchMonitorsWithChecks = ({ - teamId, - types, - limit, - page, - rowsPerPage, - filter, - field, - order, - monitorUpdateTrigger, -}) => { - const [isLoading, setIsLoading] = useState(false); - const [count, setCount] = useState(undefined); - const [monitors, setMonitors] = useState(undefined); - const [networkError, setNetworkError] = useState(false); - - const theme = useTheme(); - const { getMonitorWithPercentage } = useMonitorUtils(); - useEffect(() => { - const fetchMonitors = async () => { - try { - setIsLoading(true); - const res = await networkService.getMonitorsWithChecksByTeamId({ - teamId, - limit, - types, - page, - rowsPerPage, - filter, - field, - order, - }); - const { count, monitors } = res?.data?.data ?? {}; - const mappedMonitors = monitors.map((monitor) => - getMonitorWithPercentage(monitor, theme) - ); - setMonitors(mappedMonitors); - setCount(count?.monitorsCount ?? 0); - } catch (error) { - console.error(error); - setNetworkError(true); - createToast({ - body: error.message, - }); - } finally { - setIsLoading(false); - } - }; - fetchMonitors(); - }, [ - field, - filter, - getMonitorWithPercentage, - limit, - order, - page, - rowsPerPage, - teamId, - theme, - types, - monitorUpdateTrigger, - ]); - return [monitors, count, isLoading, networkError]; -}; - -export default useFetchMonitorsWithChecks; diff --git a/client/src/Hooks/useFetchMonitorsWithSummary.js b/client/src/Hooks/useFetchMonitorsWithSummary.js deleted file mode 100644 index cd21271d4..000000000 --- a/client/src/Hooks/useFetchMonitorsWithSummary.js +++ /dev/null @@ -1,37 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../main"; -import { createToast } from "../Utils/toastUtils"; - -export const useFetchMonitorsWithSummary = ({ teamId, types, monitorUpdateTrigger }) => { - const [isLoading, setIsLoading] = useState(false); - const [monitors, setMonitors] = useState(undefined); - const [monitorsSummary, setMonitorsSummary] = useState(undefined); - const [networkError, setNetworkError] = useState(false); - - useEffect(() => { - const fetchMonitors = async () => { - try { - setIsLoading(true); - const res = await networkService.getMonitorsWithSummaryByTeamId({ - teamId, - types, - }); - const { monitors, summary } = res?.data?.data ?? {}; - setMonitors(monitors); - setMonitorsSummary(summary); - } catch (error) { - console.error(error); - setNetworkError(true); - createToast({ - body: error.message, - }); - } finally { - setIsLoading(false); - } - }; - fetchMonitors(); - }, [teamId, types, monitorUpdateTrigger]); - return [monitors, monitorsSummary, isLoading, networkError]; -}; - -export default useFetchMonitorsWithSummary; diff --git a/client/src/Hooks/useFetchUptimeMonitorById.js b/client/src/Hooks/useFetchUptimeMonitorById.js deleted file mode 100644 index cb344569c..000000000 --- a/client/src/Hooks/useFetchUptimeMonitorById.js +++ /dev/null @@ -1,35 +0,0 @@ -import { useState, useEffect } from "react"; -import { useDispatch } from "react-redux"; -import { getUptimeMonitorById } from "../Features/UptimeMonitors/uptimeMonitorsSlice"; -import { useNavigate } from "react-router"; - -const useFetchUptimeMonitorById = (monitorId, updateTrigger) => { - const [isLoading, setIsLoading] = useState(true); - const [error, setError] = useState(null); - const [monitor, setMonitor] = useState(null); - const navigate = useNavigate(); - const dispatch = useDispatch(); - useEffect(() => { - const fetchMonitor = async () => { - try { - setIsLoading(true); - const action = await dispatch(getUptimeMonitorById({ monitorId })); - - if (getUptimeMonitorById.fulfilled.match(action)) { - const monitor = action.payload.data; - setMonitor(monitor); - } else if (getUptimeMonitorById.rejected.match(action)) { - throw new Error(action.error.message); - } - } catch (error) { - navigate("/not-found", { replace: true }); - } finally { - setIsLoading(false); - } - }; - fetchMonitor(); - }, [monitorId, dispatch, navigate, updateTrigger]); - return [monitor, isLoading, error]; -}; - -export { useFetchUptimeMonitorById }; diff --git a/client/src/Hooks/useFetchUptimeMonitorDetails.js b/client/src/Hooks/useFetchUptimeMonitorDetails.js deleted file mode 100644 index 3c0ac7ee6..000000000 --- a/client/src/Hooks/useFetchUptimeMonitorDetails.js +++ /dev/null @@ -1,35 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../main"; -import { useNavigate } from "react-router-dom"; -import { createToast } from "../Utils/toastUtils"; - -export const useFetchUptimeMonitorDetails = ({ monitorId, dateRange, trigger }) => { - const [networkError, setNetworkError] = useState(false); - const [isLoading, setIsLoading] = useState(true); - const [monitor, setMonitor] = useState(undefined); - const [monitorStats, setMonitorStats] = useState(undefined); - const navigate = useNavigate(); - useEffect(() => { - const fetchMonitors = async () => { - try { - const res = await networkService.getUptimeDetailsById({ - monitorId: monitorId, - dateRange: dateRange, - normalize: true, - }); - const { monitorData, monitorStats } = res?.data?.data ?? {}; - setMonitor(monitorData); - setMonitorStats(monitorStats); - } catch (error) { - setNetworkError(true); - createToast({ body: error.message }); - } finally { - setIsLoading(false); - } - }; - fetchMonitors(); - }, [dateRange, monitorId, navigate, trigger]); - return [monitor, monitorStats, isLoading, networkError]; -}; - -export default useFetchUptimeMonitorDetails; diff --git a/client/src/Hooks/useMonitorControls.js b/client/src/Hooks/useMonitorControls.js deleted file mode 100644 index b47603b58..000000000 --- a/client/src/Hooks/useMonitorControls.js +++ /dev/null @@ -1,28 +0,0 @@ -import { useState } from "react"; -import { networkService } from "../main"; -import { createToast } from "../Utils/toastUtils"; - -const usePauseMonitor = ({ monitorId, triggerUpdate }) => { - const [isLoading, setIsLoading] = useState(false); - const [error, setError] = useState(undefined); - const pauseMonitor = async () => { - try { - setIsLoading(true); - const res = await networkService.pauseMonitorById({ monitorId }); - createToast({ - body: res.data.data.isActive - ? "Monitor resumed successfully" - : "Monitor paused successfully", - }); - triggerUpdate(); - } catch (error) { - setError(error); - } finally { - setIsLoading(false); - } - }; - - return [pauseMonitor, isLoading, error]; -}; - -export { usePauseMonitor }; diff --git a/client/src/Hooks/useMonitorUtils.js b/client/src/Hooks/useMonitorUtils.js index 424112a81..417c6eadd 100644 --- a/client/src/Hooks/useMonitorUtils.js +++ b/client/src/Hooks/useMonitorUtils.js @@ -46,7 +46,27 @@ const useMonitorUtils = () => { pending: theme.palette.warning.lowContrast, }; - return { getMonitorWithPercentage, determineState, statusColor }; + const statusToTheme = { + up: "success", + down: "error", + paused: "warning", + pending: "secondary", + "cannot resolve": "tertiary", + }; + + const pagespeedStatusMsg = { + up: "Live (collecting data)", + down: "Inactive", + paused: "Paused", + }; + + return { + getMonitorWithPercentage, + determineState, + statusColor, + statusToTheme, + pagespeedStatusMsg, + }; }; export { useMonitorUtils }; diff --git a/client/src/Pages/Infrastructure/Details/Hooks/useHardwareMonitorsFetch.jsx b/client/src/Pages/Infrastructure/Details/Hooks/useHardwareMonitorsFetch.jsx deleted file mode 100644 index c0b61ef62..000000000 --- a/client/src/Pages/Infrastructure/Details/Hooks/useHardwareMonitorsFetch.jsx +++ /dev/null @@ -1,38 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../../../../main"; - -const useHardwareMonitorsFetch = ({ monitorId, dateRange }) => { - // Abort early if creating monitor - - const [isLoading, setIsLoading] = useState(true); - const [networkError, setNetworkError] = useState(false); - const [monitor, setMonitor] = useState(undefined); - - useEffect(() => { - const fetchData = async () => { - try { - if (!monitorId) { - return { monitor: undefined, isLoading: false, networkError: undefined }; - } - const response = await networkService.getHardwareDetailsByMonitorId({ - monitorId: monitorId, - dateRange: dateRange, - }); - setMonitor(response.data.data); - } catch (error) { - setNetworkError(true); - } finally { - setIsLoading(false); - } - }; - fetchData(); - }, [monitorId, dateRange]); - - return { - isLoading, - networkError, - monitor, - }; -}; - -export { useHardwareMonitorsFetch }; diff --git a/client/src/Pages/Infrastructure/Monitors/Hooks/useMonitorFetch.jsx b/client/src/Pages/Infrastructure/Monitors/Hooks/useMonitorFetch.jsx deleted file mode 100644 index 883205c77..000000000 --- a/client/src/Pages/Infrastructure/Monitors/Hooks/useMonitorFetch.jsx +++ /dev/null @@ -1,46 +0,0 @@ -import { useState, useEffect } from "react"; -import { useSelector } from "react-redux"; -import { networkService } from "../../../../main"; -import { createToast } from "../../../../Utils/toastUtils"; - -const useMonitorFetch = ({ page, field, filter, rowsPerPage, updateTrigger }) => { - // Redux state - const { user } = useSelector((state) => state.auth); - - // Local state - const [isLoading, setIsLoading] = useState(true); - const [networkError, setNetworkError] = useState(false); - const [monitors, setMonitors] = useState(undefined); - const [summary, setSummary] = useState(undefined); - - useEffect(() => { - const fetchMonitors = async () => { - try { - const response = await networkService.getMonitorsByTeamId({ - teamId: user.teamId, - limit: 1, - field: field, - filter: filter, - types: ["hardware"], - page: page, - rowsPerPage: rowsPerPage, - }); - setMonitors(response?.data?.data?.filteredMonitors ?? []); - setSummary(response?.data?.data?.summary ?? {}); - } catch (error) { - setNetworkError(true); - createToast({ - body: error.message, - }); - } finally { - setIsLoading(false); - } - }; - - fetchMonitors(); - }, [page, field, filter, rowsPerPage, user.teamId, updateTrigger]); - - return { monitors, summary, isLoading, networkError }; -}; - -export { useMonitorFetch }; diff --git a/client/src/Pages/PageSpeed/Details/Hooks/useMonitorFetch.jsx b/client/src/Pages/PageSpeed/Details/Hooks/useMonitorFetch.jsx deleted file mode 100644 index 18ffd8782..000000000 --- a/client/src/Pages/PageSpeed/Details/Hooks/useMonitorFetch.jsx +++ /dev/null @@ -1,39 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../../../../main"; -import { createToast } from "../../../../Utils/toastUtils"; -import { useNavigate } from "react-router-dom"; -const useMonitorFetch = ({ monitorId }) => { - const navigate = useNavigate(); - - const [monitor, setMonitor] = useState(undefined); - const [audits, setAudits] = useState(undefined); - const [isLoading, setIsLoading] = useState(true); - const [networkError, setNetworkError] = useState(false); - useEffect(() => { - const fetchMonitor = async () => { - try { - const res = await networkService.getStatsByMonitorId({ - monitorId: monitorId, - sortOrder: "desc", - limit: 50, - dateRange: "day", - numToDisplay: null, - normalize: null, - }); - setMonitor(res?.data?.data ?? undefined); - setAudits(res?.data?.data?.checks?.[0]?.audits ?? undefined); - } catch (error) { - setNetworkError(true); - createToast({ body: error.message }); - } finally { - setIsLoading(false); - } - }; - - fetchMonitor(); - }, [monitorId, navigate]); - - return { monitor, audits, isLoading }; -}; - -export { useMonitorFetch }; diff --git a/client/src/Pages/PageSpeed/Monitors/Hooks/useMonitorsFetch.jsx b/client/src/Pages/PageSpeed/Monitors/Hooks/useMonitorsFetch.jsx deleted file mode 100644 index d2f0f4842..000000000 --- a/client/src/Pages/PageSpeed/Monitors/Hooks/useMonitorsFetch.jsx +++ /dev/null @@ -1,44 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../../../../main"; -import { createToast } from "../../../../Utils/toastUtils"; - -const useMonitorsFetch = ({ teamId }) => { - const [isLoading, setIsLoading] = useState(true); - const [monitors, setMonitors] = useState([]); - const [summary, setSummary] = useState({}); - const [networkError, setNetworkError] = useState(false); - - useEffect(() => { - const fetchMonitors = async () => { - try { - setIsLoading(true); - const res = await networkService.getMonitorsByTeamId({ - teamId: teamId, - limit: 10, - types: ["pagespeed"], - page: null, - rowsPerPage: null, - filter: null, - field: null, - order: null, - }); - if (res?.data?.data?.filteredMonitors) { - setMonitors(res.data.data.filteredMonitors); - setSummary(res.data.data.summary); - } - } catch (error) { - setNetworkError(true); - createToast({ - body: error.message, - }); - } finally { - setIsLoading(false); - } - }; - - fetchMonitors(); - }, [teamId]); - return { isLoading, monitors, summary, networkError }; -}; - -export default useMonitorsFetch; diff --git a/client/src/Pages/Uptime/Details/Hooks/useMonitorFetch.jsx b/client/src/Pages/Uptime/Details/Hooks/useMonitorFetch.jsx deleted file mode 100644 index 9491e7cc3..000000000 --- a/client/src/Pages/Uptime/Details/Hooks/useMonitorFetch.jsx +++ /dev/null @@ -1,33 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../../../../main"; -import { useNavigate } from "react-router-dom"; -import { createToast } from "../../../../Utils/toastUtils"; - -export const useMonitorFetch = ({ monitorId, dateRange }) => { - const [networkError, setNetworkError] = useState(false); - const [isLoading, setIsLoading] = useState(true); - const [monitor, setMonitor] = useState(undefined); - const navigate = useNavigate(); - - useEffect(() => { - const fetchMonitors = async () => { - try { - const res = await networkService.getUptimeDetailsById({ - monitorId: monitorId, - dateRange: dateRange, - normalize: true, - }); - setMonitor(res?.data?.data ?? {}); - } catch (error) { - setNetworkError(true); - createToast({ body: error.message }); - } finally { - setIsLoading(false); - } - }; - fetchMonitors(); - }, [dateRange, monitorId, navigate]); - return [monitor, isLoading, networkError]; -}; - -export default useMonitorFetch; diff --git a/client/src/Pages/Uptime/Monitors/Hooks/useMonitorsFetch.jsx b/client/src/Pages/Uptime/Monitors/Hooks/useMonitorsFetch.jsx deleted file mode 100644 index 1f1d15212..000000000 --- a/client/src/Pages/Uptime/Monitors/Hooks/useMonitorsFetch.jsx +++ /dev/null @@ -1,77 +0,0 @@ -import { useEffect, useState } from "react"; -import { networkService } from "../../../../main"; -import { createToast } from "../../../../Utils/toastUtils"; -import { useTheme } from "@emotion/react"; -import { useMonitorUtils } from "../../../../Hooks/useMonitorUtils"; - -export const useMonitorFetch = ({ - teamId, - limit, - page, - rowsPerPage, - filter, - field, - order, - triggerUpdate, -}) => { - const [monitorsAreLoading, setMonitorsAreLoading] = useState(false); - const [monitors, setMonitors] = useState(undefined); - const [filteredMonitors, setFilteredMonitors] = useState(undefined); - const [monitorsSummary, setMonitorsSummary] = useState(undefined); - const [networkError, setNetworkError] = useState(false); - - const theme = useTheme(); - const { getMonitorWithPercentage } = useMonitorUtils(); - useEffect(() => { - const fetchMonitors = async () => { - try { - setMonitorsAreLoading(true); - const res = await networkService.getMonitorsByTeamId({ - teamId, - limit, - types: ["http", "ping", "docker", "port"], - page, - rowsPerPage, - filter, - field, - order, - }); - const { monitors, filteredMonitors, summary } = res.data.data; - const mappedMonitors = filteredMonitors.map((monitor) => - getMonitorWithPercentage(monitor, theme) - ); - setMonitors(monitors); - setFilteredMonitors(mappedMonitors); - setMonitorsSummary(summary); - } catch (error) { - setNetworkError(true); - createToast({ - body: error.message, - }); - } finally { - setMonitorsAreLoading(false); - } - }; - fetchMonitors(); - }, [ - teamId, - limit, - field, - filter, - order, - page, - rowsPerPage, - theme, - triggerUpdate, - getMonitorWithPercentage, - ]); - return { - monitors, - filteredMonitors, - monitorsSummary, - monitorsAreLoading, - networkError, - }; -}; - -export default useMonitorFetch; diff --git a/client/src/Pages/Uptime/Monitors/Hooks/useUtils.jsx b/client/src/Pages/Uptime/Monitors/Hooks/useUtils.jsx deleted file mode 100644 index ff3288cdf..000000000 --- a/client/src/Pages/Uptime/Monitors/Hooks/useUtils.jsx +++ /dev/null @@ -1,110 +0,0 @@ -import { useTheme } from "@mui/material"; - -const useUtils = () => { - const determineState = (monitor) => { - if (typeof monitor === "undefined") return "pending"; - if (monitor.isActive === false) return "paused"; - if (monitor?.status === undefined) return "pending"; - return monitor?.status == true ? "up" : "down"; - }; - - /* TODO Refactor: from here on shouldn't live in a custom hook, but on theme, or constants */ - const theme = useTheme(); - - const statusColor = { - up: theme.palette.success.lowContrast, - down: theme.palette.error.lowContrast, - paused: theme.palette.warning.lowContrast, - pending: theme.palette.warning.lowContrast, - }; - - const statusMsg = { - up: "Your site is up.", - down: "Your site is down.", - paused: "Pending...", - }; - - const pagespeedStatusMsg = { - up: "Live (collecting data)", - down: "Inactive", - paused: "Paused", - }; - - /* - TODO - This is used on - 1) Details > Gradient card */ - /* These are rediections. We should do something that maps up to success, down to error, and get the theme by that - See Client\src\Components\Label\index.jsx - */ - - const statusToTheme = { - up: "success", - down: "error", - paused: "warning", - pending: "secondary", - "cannot resolve": "tertiary", - }; - - const getStatusStyles = (status) => { - const themeColor = statusToTheme[status]; - - return { - backgroundColor: theme.palette[themeColor].lowContrast, - background: `linear-gradient(340deg, ${theme.palette[themeColor].main} -60%, ${theme.palette[themeColor].lowContrast} 35%)`, - borderColor: theme.palette[themeColor].lowContrast, - "& h2": { - color: theme.palette[themeColor].contrastText, - textTransform: "uppercase", - }, - "& p": { - color: theme.palette[themeColor].contrastText, - }, - }; - }; - - const statusStyles = { - up: { - backgroundColor: theme.palette.success.lowContrast, - background: `linear-gradient(340deg, ${theme.palette.tertiary.main} -60%, ${theme.palette.success.lowContrast} 35%)`, // CAIO_REVIEW - borderColor: theme.palette.success.contrastText, - // "& h2": { color: theme.palette.success.contrastText }, // CAIO_REVIEW - }, - down: { - backgroundColor: theme.palette.error.lowContrast, - background: `linear-gradient(340deg, ${theme.palette.tertiary.main} -60%, ${theme.palette.error.lowContrast} 35%)`, // CAIO_REVIEW - borderColor: theme.palette.error.contrastText, - "& h2": { color: theme.palette.error.contrastText }, // CAIO_REVIEW - "& .MuiTypography-root": { color: theme.palette.error.contrastText }, // CAIO_REVIEW - }, - paused: { - backgroundColor: theme.palette.warning.lowContrast, - background: `linear-gradient(340deg, ${theme.palette.tertiary.main} -60%, ${theme.palette.warning.lowContrast} 35%)`, // CAIO_REVIEW - borderColor: theme.palette.warning.contrastText, - "& h2": { color: theme.palette.warning.contrastText }, // CAIO_REVIEW - "& .MuiTypography-root": { color: theme.palette.warning.contrastText }, // CAIO_REVIEW - }, - pending: { - backgroundColor: theme.palette.warning.lowContrast, - background: `linear-gradient(340deg, ${theme.palette.tertiary.main} -60%, ${theme.palette.warning.lowContrast} 35%)`, // CAIO_REVIEW - borderColor: theme.palette.warning.contrastText, - "& h2": { color: theme.palette.warning.contrastText }, // CAIO_REVIEW - }, - }; - - /* These are rediections. We should do something that maps up to success, down to error, and get the theme by that - - */ - - return { - determineState, - statusColor, - statusMsg, - pagespeedStatusMsg, - statusStyles, - statusToTheme, - getStatusStyles, - }; -}; - -export default useUtils;