mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Limit persisted notifications to 25 (#49393)
This commit is contained in:
parent
6891bbf03c
commit
5b8bc3e44c
@ -3,6 +3,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|||||||
import { config } from '@grafana/runtime';
|
import { config } from '@grafana/runtime';
|
||||||
import { AppNotification, AppNotificationSeverity, AppNotificationsState } from 'app/types/';
|
import { AppNotification, AppNotificationSeverity, AppNotificationsState } from 'app/types/';
|
||||||
|
|
||||||
|
const MAX_STORED_NOTIFICATIONS = 25;
|
||||||
export const STORAGE_KEY = 'notifications';
|
export const STORAGE_KEY = 'notifications';
|
||||||
export const NEW_NOTIFS_KEY = `${STORAGE_KEY}/lastRead`;
|
export const NEW_NOTIFS_KEY = `${STORAGE_KEY}/lastRead`;
|
||||||
type StoredNotification = Omit<AppNotification, 'component'>;
|
type StoredNotification = Omit<AppNotification, 'component'>;
|
||||||
@ -111,6 +112,8 @@ function serializeNotifications(notifs: Record<string, StoredNotification>) {
|
|||||||
|
|
||||||
const reducedNotifs = Object.values(notifs)
|
const reducedNotifs = Object.values(notifs)
|
||||||
.filter(isAtLeastWarning)
|
.filter(isAtLeastWarning)
|
||||||
|
.sort((a, b) => b.timestamp - a.timestamp)
|
||||||
|
.slice(0, MAX_STORED_NOTIFICATIONS)
|
||||||
.reduce<Record<string, StoredNotification>>((prev, cur) => {
|
.reduce<Record<string, StoredNotification>>((prev, cur) => {
|
||||||
prev[cur.id] = {
|
prev[cur.id] = {
|
||||||
id: cur.id,
|
id: cur.id,
|
||||||
@ -125,5 +128,11 @@ function serializeNotifications(notifs: Record<string, StoredNotification>) {
|
|||||||
|
|
||||||
return prev;
|
return prev;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
|
try {
|
||||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(reducedNotifs));
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(reducedNotifs));
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Unable to persist notifications to local storage');
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user