Skip to content

Commit 2dcfa7a

Browse files
committed
Cache the supported chains
1 parent 19679db commit 2dcfa7a

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

app/contexts/ChainsContext.tsx

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import type { Chain } from "../types/chains";
44
import { fetchChains } from "../utils/chains";
55
import { useServerConfig } from "./ServerConfigContext";
66

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+
717
interface ChainsContextType {
818
chains: Chain[];
919
loading: boolean;
@@ -19,12 +29,61 @@ export function ChainsProvider({ children }: { children: ReactNode }) {
1929
const [loading, setLoading] = useState(true);
2030
const [error, setError] = useState<string | null>(null);
2131

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) => {
2369
try {
2470
setLoading(true);
2571
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
2684
const fetchedChains = await fetchChains(serverUrl);
2785
setChains(fetchedChains);
86+
setCachedChains(fetchedChains);
2887
} catch (err) {
2988
setError(err instanceof Error ? err.message : "Failed to fetch chains");
3089
console.error("Error loading chains:", err);
@@ -34,10 +93,11 @@ export function ChainsProvider({ children }: { children: ReactNode }) {
3493
};
3594

3695
const refetch = async () => {
37-
await loadChains();
96+
await loadChains(false); // Force refresh, skip cache
3897
};
3998

4099
useEffect(() => {
100+
// Load chains immediately (will use cache if available)
41101
loadChains();
42102
}, [serverUrl]);
43103

0 commit comments

Comments
 (0)