fix: Remove unnecessary local monitor state in InfrastructureMenu component

This commit is contained in:
mohadeseh safari committed 2025-07-06 18:09:17 -04:00
1 parent 0f82f076f7
commit d842f93a19
1 file changed
+4 -24
@@ -1,6 +1,6 @@
/* TODO I basically copied and pasted this component from the actionsMenu. Check how we can make it reusable */ /* TODO I basically copied and pasted this component from the actionsMenu. Check how we can make it reusable */
import { useRef, useState, useEffect } from "react"; import { useRef, useState } from "react";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { createToast } from "../../../../../Utils/toastUtils"; import { createToast } from "../../../../../Utils/toastUtils";
@@ -30,21 +30,11 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
const anchor = useRef(null); const anchor = useRef(null);
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [isDialogOpen, setIsDialogOpen] = useState(false); const [isDialogOpen, setIsDialogOpen] = useState(false);
const [localMonitorState, setLocalMonitorState] = useState(monitor);
const theme = useTheme(); const theme = useTheme();
const [pauseMonitor] = usePauseMonitor(); const [pauseMonitor] = usePauseMonitor();
const { t } = useTranslation(); const { t } = useTranslation();
// Update local state when monitor prop changes
useEffect(() => {
// Ensure we have a valid monitor object with default values for missing properties
if (monitor) {
setLocalMonitorState({
...monitor,
status: monitor.status || "active", // Default to active if status is undefined
});
}
}, [monitor]);
const openMenu = (e) => { const openMenu = (e) => {
e.stopPropagation(); e.stopPropagation();
@@ -76,21 +66,11 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
const handlePause = async () => { const handlePause = async () => {
try { try {
// Toggle the local state immediately for better UI responsiveness
setLocalMonitorState((prevState) => ({
...prevState,
status: prevState.status === "paused" ? "active" : "paused",
}));
// Pass updateCallback as triggerUpdate to the hook // Pass updateCallback as triggerUpdate to the hook
await pauseMonitor({ monitorId: monitor.id, triggerUpdate: updateCallback }); await pauseMonitor({ monitorId: monitor.id, triggerUpdate: updateCallback });
// Toast is already displayed in the hook, no need to display it again // Toast is already displayed in the hook, no need to display it again
} catch (error) { } catch (error) {
// Error handling is done in the hook - revert the local state on error // Error handling is done in the hook
setLocalMonitorState((prevState) => ({
...prevState,
status: prevState.status === "paused" ? "active" : "paused",
}));
} }
}; };
@@ -175,7 +155,7 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
closeMenu(e); closeMenu(e);
}} }}
> >
{localMonitorState?.status === "paused" {monitor.status === "paused"
? t("resume", "Resume") ? t("resume", "Resume")
: t("pause", "Pause")} : t("pause", "Pause")}
</MenuItem> </MenuItem>