1
- import { useProductHubData } from 'features/productHub/hooks/useProductHubData'
2
- import type { ConfigResponseType , PreloadAppDataContext } from 'helpers/config'
1
+ import type { PreloadAppDataContext } from 'helpers/config'
3
2
import { configCacheTime , getLocalAppConfig , saveConfigToLocalStorage } from 'helpers/config'
4
3
import { LendingProtocol } from 'lendingProtocols'
5
4
import type { PropsWithChildren } from 'react'
6
5
import React , { useContext , useEffect , useState } from 'react'
7
6
8
- const configFetcher = ( ) =>
9
- fetch ( `/api/config` , {
10
- method : 'GET' ,
11
- headers : {
12
- 'Content-Type' : 'application/json' ,
13
- } ,
14
- } )
15
-
16
7
export const preloadAppDataContext = React . createContext < PreloadAppDataContext | undefined > (
17
8
undefined ,
18
9
)
@@ -28,49 +19,47 @@ export function usePreloadAppDataContext(): PreloadAppDataContext {
28
19
}
29
20
30
21
export function PreloadAppDataContextProvider ( { children } : PropsWithChildren < { } > ) {
31
- const [ context , setContext ] = useState < PreloadAppDataContext | undefined > ( undefined )
22
+ const [ context , setContext ] = useState < PreloadAppDataContext > ( )
32
23
const { AjnaSafetySwitch, MorphoSafetySwitch } = getLocalAppConfig ( 'features' )
33
24
34
- const [ config , setConfig ] = useState < ConfigResponseType | undefined > ( undefined )
35
- const { data : productHub } = useProductHubData ( {
36
- protocols : [
37
- ...( AjnaSafetySwitch ? [ ] : [ LendingProtocol . Ajna ] ) ,
38
- LendingProtocol . AaveV2 ,
39
- LendingProtocol . AaveV3 ,
40
- LendingProtocol . Maker ,
41
- ...( MorphoSafetySwitch ? [ ] : [ LendingProtocol . MorphoBlue ] ) ,
42
- LendingProtocol . SparkV3 ,
43
- ] . filter ( ( p ) => p ) as LendingProtocol [ ] ,
44
- } )
45
-
46
25
useEffect ( ( ) => {
47
26
const setup = async ( ) => {
48
27
const fetchConfig = async ( ) => {
49
28
try {
50
- const response = await configFetcher ( )
51
- const configResponse = ( await response . json ( ) ) as ConfigResponseType
52
- if ( ! configResponse || configResponse . error ) {
53
- throw new Error ( `Error fetching preload app data: ${ configResponse . error } ` )
54
- }
55
- setConfig ( configResponse )
56
- saveConfigToLocalStorage ( configResponse )
29
+ const response = await fetch (
30
+ `/api/prerequisites?protocols=${ [
31
+ ...( AjnaSafetySwitch ? [ ] : [ LendingProtocol . Ajna ] ) ,
32
+ LendingProtocol . AaveV2 ,
33
+ LendingProtocol . AaveV3 ,
34
+ LendingProtocol . Maker ,
35
+ ...( MorphoSafetySwitch ? [ ] : [ LendingProtocol . MorphoBlue ] ) ,
36
+ LendingProtocol . SparkV3 ,
37
+ ] } `,
38
+ {
39
+ method : 'GET' ,
40
+ headers : {
41
+ 'Content-Type' : 'application/json' ,
42
+ } ,
43
+ } ,
44
+ )
45
+ const data = ( await response . json ( ) ) as PreloadAppDataContext
46
+
47
+ if ( ! data ) throw new Error ( 'Error fetching preload app data' )
48
+
49
+ setContext ( data )
50
+ saveConfigToLocalStorage ( data . config )
57
51
} catch ( error ) {
58
52
console . error ( `Error fetching preload app data context: ${ error } ` )
59
53
}
60
54
}
61
55
await fetchConfig ( )
56
+
62
57
setInterval ( fetchConfig , 1000 * configCacheTime . frontend )
63
58
}
64
59
setup ( ) . catch ( ( error ) => {
65
60
console . error ( `Error setting up preload app data context: ${ error } ` )
66
61
} )
67
62
} , [ ] )
68
63
69
- useEffect ( ( ) => {
70
- if ( config && productHub ) {
71
- setContext ( { config, productHub } )
72
- }
73
- } , [ config , productHub ] )
74
-
75
64
return < preloadAppDataContext . Provider value = { context } > { children } </ preloadAppDataContext . Provider >
76
65
}
0 commit comments