From 5b8bc3e44c3820cf4fff66fd112d3f041640afeb Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Mon, 23 May 2022 12:32:31 +0100 Subject: [PATCH] Limit persisted notifications to 25 (#49393) --- public/app/core/reducers/appNotification.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/public/app/core/reducers/appNotification.ts b/public/app/core/reducers/appNotification.ts index eff7f50c4a4..adea058d646 100644 --- a/public/app/core/reducers/appNotification.ts +++ b/public/app/core/reducers/appNotification.ts @@ -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; @@ -111,6 +112,8 @@ function serializeNotifications(notifs: Record) { const reducedNotifs = Object.values(notifs) .filter(isAtLeastWarning) + .sort((a, b) => b.timestamp - a.timestamp) + .slice(0, MAX_STORED_NOTIFICATIONS) .reduce>((prev, cur) => { prev[cur.id] = { id: cur.id, @@ -125,5 +128,11 @@ function serializeNotifications(notifs: Record) { return prev; }, {}); - window.localStorage.setItem(STORAGE_KEY, JSON.stringify(reducedNotifs)); + + try { + window.localStorage.setItem(STORAGE_KEY, JSON.stringify(reducedNotifs)); + } catch (err) { + console.error('Unable to persist notifications to local storage'); + console.error(err); + } }