Limit persisted notifications to 25 (#49393)

This commit is contained in:
Josh Hunt 2022-05-23 12:32:31 +01:00 committed by GitHub
parent 6891bbf03c
commit 5b8bc3e44c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { config } from '@grafana/runtime';
import { AppNotification, AppNotificationSeverity, AppNotificationsState } from 'app/types/';
const MAX_STORED_NOTIFICATIONS = 25;
export const STORAGE_KEY = 'notifications';
export const NEW_NOTIFS_KEY = `${STORAGE_KEY}/lastRead`;
type StoredNotification = Omit<AppNotification, 'component'>;
@ -111,6 +112,8 @@ function serializeNotifications(notifs: Record<string, StoredNotification>) {
const reducedNotifs = Object.values(notifs)
.filter(isAtLeastWarning)
.sort((a, b) => b.timestamp - a.timestamp)
.slice(0, MAX_STORED_NOTIFICATIONS)
.reduce<Record<string, StoredNotification>>((prev, cur) => {
prev[cur.id] = {
id: cur.id,
@ -125,5 +128,11 @@ function serializeNotifications(notifs: Record<string, StoredNotification>) {
return prev;
}, {});
try {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(reducedNotifs));
} catch (err) {
console.error('Unable to persist notifications to local storage');
console.error(err);
}
}