mirror of
https://github.com/grafana/grafana.git
synced 2025-09-19 21:24:56 +08:00

PR #47674 attempted to sort a read-only managed async array. This change moves the sort logic to the fetch code so sort happens once on fetch, to a mutable array, rather than trying on each render for an immutable array. Signed-off-by: Joe Blubaugh <joe.blubaugh@grafana.com>
39 lines
842 B
TypeScript
39 lines
842 B
TypeScript
import { getBackendSrv } from '@grafana/runtime';
|
|
import { StateHistoryItem } from 'app/types/unified-alerting';
|
|
|
|
export function fetchAnnotations(alertId: string): Promise<StateHistoryItem[]> {
|
|
return getBackendSrv()
|
|
.get('/api/annotations', {
|
|
alertId,
|
|
})
|
|
.then((result) => {
|
|
return result?.sort(sortStateHistory);
|
|
});
|
|
}
|
|
|
|
export function sortStateHistory(a: StateHistoryItem, b: StateHistoryItem): number {
|
|
const compareDesc = (a: number, b: number): number => {
|
|
// Larger numbers first.
|
|
if (a > b) {
|
|
return -1;
|
|
}
|
|
|
|
if (b > a) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
};
|
|
|
|
const endNeq = compareDesc(a.timeEnd, b.timeEnd);
|
|
if (endNeq) {
|
|
return endNeq;
|
|
}
|
|
|
|
const timeNeq = compareDesc(a.time, b.time);
|
|
if (timeNeq) {
|
|
return timeNeq;
|
|
}
|
|
|
|
return compareDesc(a.id, b.id);
|
|
}
|