2020-06-18 07:31:30 -05:00
|
|
|
import { DataQueryError, LoadingState, PanelData } from '@grafana/data';
|
2020-04-22 12:21:48 -05:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
import { PanelModel } from '../../state';
|
|
|
|
import { Unsubscribable } from 'rxjs';
|
2020-05-25 07:05:43 -05:00
|
|
|
import { GetDataOptions } from '../../state/PanelQueryRunner';
|
2020-04-22 12:21:48 -05:00
|
|
|
|
2020-06-18 07:31:30 -05:00
|
|
|
interface UsePanelLatestData {
|
|
|
|
data?: PanelData;
|
|
|
|
error?: DataQueryError;
|
|
|
|
isLoading: boolean;
|
|
|
|
hasSeries: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribes and returns latest panel data from PanelQueryRunner
|
|
|
|
*/
|
|
|
|
export const usePanelLatestData = (panel: PanelModel, options: GetDataOptions): UsePanelLatestData => {
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
const querySubscription = useRef<Unsubscribable>();
|
2020-06-18 07:31:30 -05:00
|
|
|
const [latestData, setLatestData] = useState<PanelData>();
|
2020-04-22 12:21:48 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
querySubscription.current = panel
|
|
|
|
.getQueryRunner()
|
2020-05-25 07:05:43 -05:00
|
|
|
.getData(options)
|
2020-04-22 12:21:48 -05:00
|
|
|
.subscribe({
|
|
|
|
next: data => setLatestData(data),
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (querySubscription.current) {
|
|
|
|
querySubscription.current.unsubscribe();
|
|
|
|
}
|
|
|
|
};
|
2020-06-18 07:31:30 -05:00
|
|
|
/**
|
|
|
|
* Adding separate options to dependencies array to avoid additional hook for comparing previous options with current.
|
|
|
|
* Otherwise, passing different references to the same object may cause troubles.
|
|
|
|
*/
|
|
|
|
}, [panel, options.withFieldConfig, options.withTransforms]);
|
2020-04-22 12:21:48 -05:00
|
|
|
|
2020-06-18 07:31:30 -05:00
|
|
|
return {
|
|
|
|
data: latestData,
|
|
|
|
error: latestData && latestData.error,
|
|
|
|
isLoading: latestData ? latestData.state === LoadingState.Loading : true,
|
|
|
|
hasSeries: latestData ? !!latestData.series : false,
|
|
|
|
};
|
2020-04-22 12:21:48 -05:00
|
|
|
};
|