[v9.3.x] Alerting: Use optional chaining for accessing frames (#63417)

Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2023-02-19 07:25:18 -05:00
committed by GitHub
co-authored by Gilles De Mey
parent 403ad6923a
commit 7883f9b001
@@ -1,11 +1,20 @@
import { DataFrame, Labels, roundDecimals } from '@grafana/data';
/**
* ⚠️ `frame.fields` could be an empty array ⚠️
*
* TypeScript will NOT complain about it when accessing items via index signatures.
* Make sure to check for empty array or use optional chaining!
*
* see https://github.com/Microsoft/TypeScript/issues/13778
*/
const getSeriesName = (frame: DataFrame): string => {
return frame.name ?? formatLabels(frame.fields[0].labels ?? {});
return frame.name ?? formatLabels(frame.fields[0]?.labels ?? {});
};
const getSeriesValue = (frame: DataFrame) => {
const value = frame.fields[0].values.get(0);
const value = frame.fields[0]?.values.get(0);
if (Number.isFinite(value)) {
return roundDecimals(value, 5);