66
77import { apiFetch , ExtendedError } from './api' ;
88import type { ApiQueryGet } from './query' ;
9- import type { PlotParams , QueryParams } from '@/url2' ;
9+ import type { PlotParams , TimeRange , VariableKey , VariableParams } from '@/url2' ;
1010import { useLiveModeStore } from '@/store2/liveModeStore' ;
1111import { useMemo } from 'react' ;
12- import { getLoadPlotUrlParams } from '@/store2/plotDataStore/loadPlotData' ;
1312import { useQuery , type UseQueryResult } from '@tanstack/react-query' ;
13+ import { useStatsHouse } from '@/store2' ;
14+ import { getLoadPlotUrlParamsLight } from '@/store2/plotDataStore/loadPlotData2' ;
1415
1516export const ApiBadgesEndpoint = '/api/badges' ;
1617
@@ -35,35 +36,36 @@ export async function apiBadgesFetch(params: ApiQueryGet, keyRequest?: unknown)
3536
3637export function useApiBadges < T = ApiBadges > (
3738 plot : PlotParams ,
38- params : QueryParams ,
39+ timeRange : TimeRange ,
40+ timeShifts : number [ ] ,
41+ variables : Partial < Record < VariableKey , VariableParams > > ,
3942 select ?: ( response ?: ApiBadges ) => T ,
40- enabled : boolean = true
43+ enabled : boolean = false ,
44+ priority : number = 2
4145) : UseQueryResult < T , ExtendedError > {
42- const interval = useLiveModeStore ( ( { interval, status } ) => ( status ? interval : undefined ) ) ;
43-
44- const priority = useMemo ( ( ) => {
45- if ( plot ?. id === params . tabNum ) {
46- return 1 ;
47- }
48- return enabled ? 2 : 3 ;
49- } , [ enabled , params . tabNum , plot ?. id ] ) ;
46+ const interval = useLiveModeStore ( ( { interval, status } ) => ( status ? interval * 2 : undefined ) ) ;
5047
5148 const keyParams = useMemo ( ( ) => {
5249 if ( ! plot ?. id ) {
5350 return null ;
5451 }
55- return getLoadPlotUrlParams ( plot ?. id , params ) ;
56- } , [ params , plot ?. id ] ) ;
52+ return getLoadPlotUrlParamsLight ( plot , timeRange , timeShifts , variables , interval ) ;
53+ } , [ interval , plot , timeRange , timeShifts , variables ] ) ;
54+
55+ const plotHeals = useStatsHouse ( ( s ) => {
56+ const status = s . plotHeals [ plot . id ] ;
57+ return ! ( ! ! status && ! status . status && status . lastTimestamp + status . timeout * 1000 > Date . now ( ) ) ;
58+ } ) ;
5759
5860 const fetchParams = useMemo ( ( ) => {
5961 if ( ! plot ?. id ) {
6062 return null ;
6163 }
62- return getLoadPlotUrlParams ( plot ?. id , params , interval , false , priority ) ;
63- } , [ interval , params , plot ?. id , priority ] ) ;
64+ return getLoadPlotUrlParamsLight ( plot , timeRange , timeShifts , variables , interval , false , priority ) ;
65+ } , [ interval , plot , priority , timeRange , timeShifts , variables ] ) ;
6466
6567 return useQuery ( {
66- enabled,
68+ enabled : plotHeals && enabled ,
6769 select,
6870 queryKey : [ ApiBadgesEndpoint , keyParams ] ,
6971 queryFn : async ( { signal } ) => {
@@ -78,5 +80,6 @@ export function useApiBadges<T = ApiBadges>(
7880 return response ;
7981 } ,
8082 placeholderData : ( previousData ) => previousData ,
83+ refetchInterval : interval ? interval * 1000 : undefined , //live mode badge x2 interval
8184 } ) ;
8285}
0 commit comments