Fix infrastructure action menu: correct pause/resume text display and handle undefined monitor properties
This commit is contained in:
1 parent
310084a902
commit
1a2009b615
1 file changed
+48
-21
@@ -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 } from "react";
|
import { useRef, useState, useEffect } 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,9 +30,21 @@ 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();
|
||||||
@@ -64,19 +76,21 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
|
|
||||||
const handlePause = async () => {
|
const handlePause = async () => {
|
||||||
try {
|
try {
|
||||||
await pauseMonitor({ monitorId: monitor.id });
|
// Toggle the local state immediately for better UI responsiveness
|
||||||
createToast({
|
setLocalMonitorState(prevState => ({
|
||||||
body: t("monitorActions.pauseResumeSuccess", "Monitor {{action}} successfully", {
|
...prevState,
|
||||||
action: monitor.isActive ? t("monitorActions.paused", "paused") : t("monitorActions.resumed", "resumed")
|
status: prevState.status === "paused" ? "active" : "paused"
|
||||||
}),
|
}));
|
||||||
});
|
|
||||||
updateCallback();
|
// Pass updateCallback as triggerUpdate to the hook
|
||||||
|
await pauseMonitor({ monitorId: monitor.id, triggerUpdate: updateCallback });
|
||||||
|
// Toast is already displayed in the hook, no need to display it again
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createToast({
|
// Error handling is done in the hook - revert the local state on error
|
||||||
body: t("monitorActions.pauseResumeFailed", "Failed to {{action}} monitor", {
|
setLocalMonitorState(prevState => ({
|
||||||
action: monitor.isActive ? t("pause", "pause") : t("resume", "resume")
|
...prevState,
|
||||||
}),
|
status: prevState.status === "paused" ? "active" : "paused"
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -131,13 +145,25 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<MenuItem onClick={() => openDetails(monitor.id)}>{t("monitorActions.details", "Details")}</MenuItem>
|
<MenuItem onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
openDetails(monitor.id);
|
||||||
|
closeMenu(e);
|
||||||
|
}}>{t("monitorActions.details", "Details")}</MenuItem>
|
||||||
{isAdmin && (
|
{isAdmin && (
|
||||||
<MenuItem onClick={() => openConfigure(monitor.id)}>{t("configure", "Configure")}</MenuItem>
|
<MenuItem onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
openConfigure(monitor.id);
|
||||||
|
closeMenu(e);
|
||||||
|
}}>{t("configure", "Configure")}</MenuItem>
|
||||||
)}
|
)}
|
||||||
{isAdmin && (
|
{isAdmin && (
|
||||||
<MenuItem onClick={handlePause}>
|
<MenuItem onClick={async (e) => {
|
||||||
{monitor.isActive ? t("pause", "Pause") : t("resume", "Resume")}
|
e.stopPropagation();
|
||||||
|
await handlePause();
|
||||||
|
closeMenu(e);
|
||||||
|
}}>
|
||||||
|
{localMonitorState?.status === "paused" ? t("resume", "Resume") : t("pause", "Pause")}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
)}
|
)}
|
||||||
{isAdmin && (
|
{isAdmin && (
|
||||||
@@ -166,13 +192,14 @@ const InfrastructureMenu = ({ monitor, isAdmin, updateCallback }) => {
|
|||||||
|
|
||||||
InfrastructureMenu.propTypes = {
|
InfrastructureMenu.propTypes = {
|
||||||
monitor: PropTypes.shape({
|
monitor: PropTypes.shape({
|
||||||
id: PropTypes.string,
|
id: PropTypes.string.isRequired,
|
||||||
url: PropTypes.string,
|
url: PropTypes.string,
|
||||||
type: PropTypes.string,
|
type: PropTypes.string, // Made optional
|
||||||
isActive: PropTypes.bool,
|
isActive: PropTypes.bool,
|
||||||
|
status: PropTypes.string,
|
||||||
}).isRequired,
|
}).isRequired,
|
||||||
isAdmin: PropTypes.bool,
|
isAdmin: PropTypes.bool.isRequired,
|
||||||
updateCallback: PropTypes.func,
|
updateCallback: PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export { InfrastructureMenu };
|
export { InfrastructureMenu };
|
||||||
Reference in new issue
Block a user