grafana/public/app/features/trails/hideEmptyPreviews.ts
Darren Janeczek 96772e1a32
datatrails: improve handling of error states and disable states (#81669)
* fix: Cascader: allow disabled state

* fix: datatrails metrics selection scene stability
  - clear panels and filter data when datasource changes
  - detect metric names loading / error state and disable components accordingly
  - put all scene variable dependencies together
  - reset metric names without clearing panels when time range changes
2024-02-05 09:45:47 -05:00

44 lines
1.2 KiB
TypeScript

import { FieldType, LoadingState } from '@grafana/data';
import { SceneCSSGridItem, sceneGraph } from '@grafana/scenes';
import { MetricSelectScene } from './MetricSelectScene';
export function hideEmptyPreviews(metric: string) {
return (gridItem: SceneCSSGridItem) => {
const data = sceneGraph.getData(gridItem);
if (!data) {
return;
}
data.subscribeToState((state) => {
if (state.data?.state === LoadingState.Loading || state.data?.state === LoadingState.Error) {
return;
}
const scene = sceneGraph.getAncestor(gridItem, MetricSelectScene);
if (!state.data?.series.length) {
scene.updateMetricPanel(metric, true, true);
return;
}
let hasValue = false;
for (const frame of state.data.series) {
for (const field of frame.fields) {
if (field.type !== FieldType.number) {
continue;
}
hasValue = field.values.some((v) => v != null && !isNaN(v) && v !== 0);
if (hasValue) {
break;
}
}
if (hasValue) {
break;
}
}
scene.updateMetricPanel(metric, true, !hasValue);
});
};
}