Skip to content

Commit

Permalink
Fix alerts from not disappearing on route changes (gravitational#1395)
Browse files Browse the repository at this point in the history
ryanclark authored Nov 30, 2022
1 parent 35c2092 commit 972433e
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions web/packages/teleport/src/components/BannerList/useAlerts.tsx
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ import type { ClusterAlert } from 'teleport/services/alerts';

const logger = Logger.create('ClusterAlerts');

const DISABLED_BANNERS = 'disabledAlerts';
const DISABLED_ALERTS = 'disabledAlerts';
const MS_HOUR = 60 * 60 * 1000;

export function addHours(date: number, hours: number) {
@@ -39,27 +39,27 @@ function setItem(key: string, data: string) {
window.localStorage.setItem(key, data);
}

type DismissedAlert = {
[alertName: string]: string;
type DismissedAlerts = {
[alertName: string]: number;
};

export function useAlerts(initialAlerts: ClusterAlert[] = []) {
const [alerts, setAlerts] = useState<ClusterAlert[]>(initialAlerts);
const [dismissedAlerts, setDismissedAlerts] = useState<DismissedAlert[]>([]);
const [dismissedAlerts, setDismissedAlerts] = useState<DismissedAlerts>({});
const { clusterId } = useStickyClusterId();

useEffect(() => {
const disabledAlerts = getItem(DISABLED_BANNERS);
const disabledAlerts = getItem(DISABLED_ALERTS);
if (disabledAlerts) {
// Loop through the existing ones and remove those that have passed 24h.
const data = JSON.parse(disabledAlerts);
Object.entries(data).forEach(([name, expiry]: [string, string]) => {
const data: DismissedAlerts = JSON.parse(disabledAlerts);
Object.entries(data).forEach(([name, expiry]) => {
if (new Date().getTime() > +expiry) {
delete data[name];
}
});
setDismissedAlerts(data);
setItem(DISABLED_BANNERS, JSON.stringify(data));
setItem(DISABLED_ALERTS, JSON.stringify(data));
}
}, []);

@@ -77,13 +77,14 @@ export function useAlerts(initialAlerts: ClusterAlert[] = []) {
}, [clusterId]);

function dismissAlert(name: string) {
const disabledAlerts = getItem(DISABLED_BANNERS);
let data = {};
const disabledAlerts = getItem(DISABLED_ALERTS);
let data: DismissedAlerts = {};
if (disabledAlerts) {
data = JSON.parse(disabledAlerts);
}
data[name] = addHours(new Date().getTime(), 24);
setItem(DISABLED_BANNERS, JSON.stringify(data));
setDismissedAlerts(data);
setItem(DISABLED_ALERTS, JSON.stringify(data));
}

const dismissedAlertNames = Object.keys(dismissedAlerts);
2 changes: 1 addition & 1 deletion web/packages/teleport/src/services/alerts/alerts.tsx
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ export type ClusterAlert = {
metadata: {
name: string;
labels: { [key: string]: string }; //"teleport.internal/alert-on-login": "yes",
expires: string; //2022-08-31T17:26:05.728149Z
};
expires: string; //2022-08-31T17:26:05.728149Z
spec: {
severity: number;
message: string;

0 comments on commit 972433e

Please sign in to comment.