diff --git a/frontend/src/pages/AlertDetails/AlertHeader/ActionButtons/ActionButtons.tsx b/frontend/src/pages/AlertDetails/AlertHeader/ActionButtons/ActionButtons.tsx index 186a34676b..2f37c4fc9d 100644 --- a/frontend/src/pages/AlertDetails/AlertHeader/ActionButtons/ActionButtons.tsx +++ b/frontend/src/pages/AlertDetails/AlertHeader/ActionButtons/ActionButtons.tsx @@ -15,7 +15,7 @@ import { } from 'pages/AlertDetails/hooks'; import CopyToClipboard from 'periscope/components/CopyToClipboard'; import { useAlertRule } from 'providers/Alert'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { CSSProperties } from 'styled-components'; import { AlertDef } from 'types/api/alerts/def'; @@ -32,7 +32,7 @@ function AlertActionButtons({ ruleId: string; alertDetails: AlertHeaderProps['alertDetails']; }): JSX.Element { - const { isAlertRuleDisabled } = useAlertRule(); + const { alertRuleState, setAlertRuleState } = useAlertRule(); const { handleAlertStateToggle } = useAlertRuleStatusToggle({ ruleId }); const { handleAlertDuplicate } = useAlertRuleDuplicate({ @@ -79,13 +79,32 @@ function AlertActionButtons({ ); const isDarkMode = useIsDarkMode(); + // state for immediate UI feedback rather than waiting for onSuccess of handleAlertStateTiggle to updating the alertRuleState + const [isAlertRuleDisabled, setIsAlertRuleDisabled] = useState< + undefined | boolean + >(undefined); + + useEffect(() => { + if (alertRuleState === undefined) { + setAlertRuleState(alertDetails.state); + setIsAlertRuleDisabled(alertDetails.state === 'disabled'); + } + }, [setAlertRuleState, alertRuleState, alertDetails.state]); + + // on unmount remove the alert state + // eslint-disable-next-line react-hooks/exhaustive-deps + useEffect(() => (): void => setAlertRuleState(undefined), []); + return (