11import pluralize from 'pluralize' ;
2- import { createContext , useEffect , useRef , FC } from 'react' ;
2+ import { createContext , useEffect , useRef , FC , useState } from 'react' ;
33import { useRecoilValue } from 'recoil' ;
44
55import { useFetch } from 'shared/hooks/BackendAPI/useFetch' ;
66import { useObjectState } from 'shared/useObjectState' ;
77import jp from 'jsonpath' ;
88import { jsonataWrapper } from '../helpers/jsonataWrapper' ;
99import { activeNamespaceIdState } from 'state/activeNamespaceIdAtom' ;
10+ import { resourcesConditions } from 'state/resourceConditionsAtom' ;
1011
1112export interface Resource {
1213 metadata : {
@@ -83,10 +84,29 @@ export const DataSourcesContextProvider: FC<Props> = ({
8384 // refetch intervals
8485 const intervals = useRef < ReturnType < typeof setTimeout > [ ] > ( [ ] ) ;
8586 const fallbackNamespace = useRecoilValue ( activeNamespaceIdState ) ;
87+ const stateConditions = useRecoilValue ( resourcesConditions ) ;
88+ const [ refetchSource , setRefetchSource ] = useState ( '' ) ;
89+
90+ const findUpdatedName = ( conditionsArr : string [ ] , storeArr : string [ ] ) => {
91+ return conditionsArr . find ( item => storeArr . includes ( item ) ) ;
92+ } ;
8693
8794 // clear timeouts on component unmount
8895 // eslint-disable-next-line react-hooks/exhaustive-deps
8996 useEffect ( ( ) => ( ) => intervals . current . forEach ( clearInterval ) , [ ] ) ;
97+ useEffect (
98+ ( ) => ( ) => {
99+ const updatedSourceName = findUpdatedName (
100+ Object . keys ( stateConditions ) ,
101+ Object . keys ( store ) ,
102+ ) ;
103+ if ( updatedSourceName && ! refetchSource ) {
104+ setRefetchSource ( updatedSourceName ) ;
105+ }
106+ } ,
107+ // eslint-disable-next-line react-hooks/exhaustive-deps
108+ [ stateConditions , refetchSource ] ,
109+ ) ;
90110
91111 const buildUrl = (
92112 dataSource : DataSource ,
@@ -179,6 +199,7 @@ export const DataSourcesContextProvider: FC<Props> = ({
179199 const dataSource = dataSources [ dataSourceName ] ;
180200
181201 if (
202+ refetchSource === dataSourceName ||
182203 ! dataSourcesDict . current [ dataSourceName ] ||
183204 dataSourcesDict . current [ dataSourceName ] . rootName !==
184205 resource ?. metadata ?. name ||
@@ -205,7 +226,9 @@ export const DataSourcesContextProvider: FC<Props> = ({
205226 REFETCH_INTERVAL ,
206227 ) ,
207228 ) ;
208-
229+ if ( refetchSource ) {
230+ setRefetchSource ( 'reFetched' ) ;
231+ }
209232 return firstFetch ;
210233 } else if ( store ?. [ dataSourceName ] ?. loading ) {
211234 return store ?. [ dataSourceName ] ?. firstFetch ;
0 commit comments