@@ -4,6 +4,16 @@ import type { Chain } from "../types/chains";
4
4
import { fetchChains } from "../utils/chains" ;
5
5
import { useServerConfig } from "./ServerConfigContext" ;
6
6
7
+ // Cache configuration
8
+ const CACHE_KEY = "sourcify_chains_cache" ;
9
+ const CACHE_DURATION = 30 * 60 * 1000 ; // 30 minutes in milliseconds
10
+
11
+ interface CachedChains {
12
+ chains : Chain [ ] ;
13
+ timestamp : number ;
14
+ serverUrl : string ;
15
+ }
16
+
7
17
interface ChainsContextType {
8
18
chains : Chain [ ] ;
9
19
loading : boolean ;
@@ -19,12 +29,61 @@ export function ChainsProvider({ children }: { children: ReactNode }) {
19
29
const [ loading , setLoading ] = useState ( true ) ;
20
30
const [ error , setError ] = useState < string | null > ( null ) ;
21
31
22
- const loadChains = async ( ) => {
32
+ const getCachedChains = ( ) : CachedChains | null => {
33
+ try {
34
+ const cached = localStorage . getItem ( CACHE_KEY ) ;
35
+ if ( ! cached ) return null ;
36
+
37
+ const parsedCache : CachedChains = JSON . parse ( cached ) ;
38
+ const now = Date . now ( ) ;
39
+
40
+ // Check if cache is still valid and for the same server
41
+ if ( now - parsedCache . timestamp < CACHE_DURATION && parsedCache . serverUrl === serverUrl ) {
42
+ return parsedCache ;
43
+ }
44
+
45
+ // Cache is expired or for different server, remove it
46
+ localStorage . removeItem ( CACHE_KEY ) ;
47
+ return null ;
48
+ } catch ( err ) {
49
+ console . error ( "Error reading chains cache:" , err ) ;
50
+ localStorage . removeItem ( CACHE_KEY ) ;
51
+ return null ;
52
+ }
53
+ } ;
54
+
55
+ const setCachedChains = ( chains : Chain [ ] ) => {
56
+ try {
57
+ const cacheData : CachedChains = {
58
+ chains,
59
+ timestamp : Date . now ( ) ,
60
+ serverUrl,
61
+ } ;
62
+ localStorage . setItem ( CACHE_KEY , JSON . stringify ( cacheData ) ) ;
63
+ } catch ( err ) {
64
+ console . error ( "Error caching chains:" , err ) ;
65
+ }
66
+ } ;
67
+
68
+ const loadChains = async ( useCache = true ) => {
23
69
try {
24
70
setLoading ( true ) ;
25
71
setError ( null ) ;
72
+
73
+ // Try to load from cache first
74
+ if ( useCache ) {
75
+ const cachedChains = getCachedChains ( ) ;
76
+ if ( cachedChains ) {
77
+ setChains ( cachedChains . chains ) ;
78
+ setLoading ( false ) ;
79
+ return ;
80
+ }
81
+ }
82
+
83
+ // Fetch from API if no valid cache
26
84
const fetchedChains = await fetchChains ( serverUrl ) ;
27
85
setChains ( fetchedChains ) ;
86
+ setCachedChains ( fetchedChains ) ;
28
87
} catch ( err ) {
29
88
setError ( err instanceof Error ? err . message : "Failed to fetch chains" ) ;
30
89
console . error ( "Error loading chains:" , err ) ;
@@ -34,10 +93,11 @@ export function ChainsProvider({ children }: { children: ReactNode }) {
34
93
} ;
35
94
36
95
const refetch = async ( ) => {
37
- await loadChains ( ) ;
96
+ await loadChains ( false ) ; // Force refresh, skip cache
38
97
} ;
39
98
40
99
useEffect ( ( ) => {
100
+ // Load chains immediately (will use cache if available)
41
101
loadChains ( ) ;
42
102
} , [ serverUrl ] ) ;
43
103
0 commit comments