11import { TrackerItem , TrackerQueryType } from "@homeremote/types" ;
2- import { useState } from "react" ;
2+ import { useEffect , useState } from "react" ;
33import { useGetCoordsQuery } from "../../../Services/dataloraApi" ;
4+ import { getErrorMessage } from "../../../Utils/getErrorMessage" ;
5+ import { useAppDispatch } from "../../../store" ;
6+ import { logInfo } from "../LogCard/logSlice" ;
47
58const UPDATE_INTERVAL_MS = 1000 * 60 * 60 ; // 1000 ms / 60 seconds / 60 minutes = 1x per hour
69
@@ -13,19 +16,37 @@ interface LocQuery {
1316}
1417
1518export const useLocQuery = ( ) : LocQuery => {
19+ const dispatch = useAppDispatch ( ) ;
20+
1621 const [ queryType , setQueryType ] = useState < TrackerQueryType > ( "24h" ) ;
1722 const {
1823 data : coords ,
1924 isLoading,
2025 isFetching,
2126 refetch,
27+ isError,
28+ error,
2229 } = useGetCoordsQuery (
2330 {
2431 type : queryType ,
2532 } ,
2633 { pollingInterval : UPDATE_INTERVAL_MS , refetchOnMountOrArgChange : true }
2734 ) ;
2835
36+ useEffect ( ( ) => {
37+ if ( isError && error ) {
38+ // This errors to often to use logError
39+ dispatch ( logInfo ( `useLocQuery failed: ${ getErrorMessage ( error ) } ` ) ) ;
40+ }
41+ } , [ dispatch , error , isError ] ) ;
42+
43+ // There often is an error on the first call, so retry after 5 seconds (also if there is no error)
44+ useEffect ( ( ) => {
45+ setTimeout ( ( ) => {
46+ refetch ( ) ;
47+ } , 5000 ) ;
48+ } , [ refetch ] ) ;
49+
2950 const update = async ( ) : Promise < void > => {
3051 refetch ( ) ;
3152 } ;
@@ -36,7 +57,7 @@ export const useLocQuery = (): LocQuery => {
3657 } ;
3758
3859 return {
39- coords : coords ?? [ ] ,
60+ coords : coords ?? [ [ ] , [ ] ] ,
4061 update,
4162 isLoading : isLoading || isFetching ,
4263 toggleQueryType,
0 commit comments