Fix health check AlertNotification #2571
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the bug
AlertNotification has "Try again" button which doesn't do anything, the potential reason could be that some of the dependencies in useEffect are not updated or badly composed registerSetInterval function with some closure complexity.
To Reproduce
Steps to reproduce the bug:
For testing purposes
As I didn't know how to mock server healthz response, I just setup [counter, setCounter] state, added condition "if counter % 2 === 0" inside performHealthCheck with setting error if true, else "return checkerFunction()...", also I added click handler which increments counter and invokes performHealthCheck with new counter value.
So with my mocking the scenario was:
`export function PureAlertNotification({ checkerFunction }: PureAlertNotificationProps) {
const [networkStatusCheckTimeFactor, setNetworkStatusCheckTimeFactor] = React.useState(0);
const [error, setError] = React.useState<null | string | boolean>(null);
const { t } = useTranslation();
const { pathname } = useLocation();
const [counter, setCounter] = React.useState(0);
const performHealthCheck = React.useCallback((counter: number) => {
if (!window.navigator.onLine) {
setError('Offline');
return;
}
}, []);
React.useEffect(() => {
const interval = setInterval(
() => performHealthCheck(counter),
(networkStatusCheckTimeFactor + 1) * NETWORK_STATUS_CHECK_TIME
);
return () => clearInterval(interval);
}, [performHealthCheck, networkStatusCheckTimeFactor, counter]);
// Make sure we do not show the alert notification if we are not on a cluster route.
React.useEffect(() => {
if (!getCluster()) {
setError(null);
}
}, [pathname]);
const showOnRoute = React.useMemo(() => {
for (const route of ROUTES_WITHOUT_ALERT) {
const routePath = getRoutePath(getRoute(route));
if (matchPath(pathname, routePath)?.isExact) {
return false;
}
}
return true;
}, [pathname]);
if (!error || !showOnRoute) {
return null;
}
const handleClick = () => {
setCounter(prev => {
const newValue = prev + 1;
performHealthCheck(newValue);
return newValue;
});
};
return (
<Alert
variant="filled"
severity="error"
sx={theme => ({
color: theme.palette.common.white,
background: theme.palette.error.main,
textAlign: 'center',
display: 'flex',
paddingTop: theme.spacing(0.5),
paddingBottom: theme.spacing(1),
paddingRight: theme.spacing(3),
justifyContent: 'center',
position: 'fixed',
zIndex: theme.zIndex.snackbar + 1,
top: '0',
alignItems: 'center',
left: '50%',
width: 'auto',
transform: 'translateX(-50%)',
})}
action={
<Button
sx={theme => ({
color: theme.palette.error.main,
borderColor: theme.palette.error.main,
background: theme.palette.common.white,
lineHeight: theme.typography.body2.lineHeight,
'&:hover': {
color: theme.palette.common.white,
borderColor: theme.palette.common.white,
background: theme.palette.error.dark,
},
})}
onClick={handleClick}
size="small"
>
{t('translation|Try Again')}
}
>
<Typography
variant="body2"
sx={theme => ({
paddingTop: theme.spacing(0.5),
fontWeight: 'bold',
fontSize: '16px',
})}
>
{t('translation|Lost connection to the cluster.')}
);
}`