Skip to content

Commit 4ffe3fb

Browse files
committed
Sync the etherscan key local storage with the state
1 parent 366a5bc commit 4ffe3fb

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

app/components/verification/ImportFromEtherscan.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from "react";
22
import { fetchFromEtherscan, processEtherscanResult } from "../../utils/etherscanApi";
3-
import { hasEtherscanApiKey, getEtherscanApiKey } from "../../utils/etherscanStorage";
3+
import { useEtherscanApiKey, getEtherscanApiKey } from "../../utils/etherscanStorage";
44

55
interface ImportFromEtherscanProps {
66
chainId: string;
@@ -24,7 +24,10 @@ export default function ImportFromEtherscan({ chainId, address, onImportSuccess
2424
const [error, setError] = useState<string | null>(null);
2525
const [success, setSuccess] = useState<string | null>(null);
2626

27-
const canImport = hasEtherscanApiKey() && chainId && address;
27+
// Use the custom hook to reactively track API key changes
28+
const hasApiKey = useEtherscanApiKey();
29+
30+
const canImport = hasApiKey && chainId && address;
2831

2932
const handleImport = async () => {
3033
if (!canImport) return;
@@ -58,7 +61,7 @@ export default function ImportFromEtherscan({ chainId, address, onImportSuccess
5861

5962
const getButtonText = () => {
6063
if (isImporting) return "Importing...";
61-
if (!hasEtherscanApiKey()) return "Add API key in settings";
64+
if (!hasApiKey) return "Add API key in settings";
6265
if (!chainId || !address) return "Select chain and address";
6366
return "Import from Etherscan";
6467
};
@@ -99,7 +102,7 @@ export default function ImportFromEtherscan({ chainId, address, onImportSuccess
99102
</div>
100103
)}
101104

102-
{!hasEtherscanApiKey() && (
105+
{!hasApiKey && (
103106
<p className="text-xs text-gray-500">
104107
Add your Etherscan API key in settings to import verified contracts
105108
</p>

app/utils/etherscanStorage.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { useState, useEffect } from "react";
2+
13
const ETHERSCAN_API_KEY_STORAGE_KEY = "etherscan-api-key";
4+
const ETHERSCAN_API_KEY_CHANGED_EVENT = "etherscan-api-key-changed";
25

36
export const getEtherscanApiKey = (): string | null => {
47
if (typeof window === "undefined") return null;
@@ -8,14 +11,37 @@ export const getEtherscanApiKey = (): string | null => {
811
export const setEtherscanApiKey = (apiKey: string): void => {
912
if (typeof window === "undefined") return;
1013
localStorage.setItem(ETHERSCAN_API_KEY_STORAGE_KEY, apiKey);
14+
// Dispatch custom event to notify components
15+
window.dispatchEvent(new CustomEvent(ETHERSCAN_API_KEY_CHANGED_EVENT));
1116
};
1217

1318
export const removeEtherscanApiKey = (): void => {
1419
if (typeof window === "undefined") return;
1520
localStorage.removeItem(ETHERSCAN_API_KEY_STORAGE_KEY);
21+
// Dispatch custom event to notify components
22+
window.dispatchEvent(new CustomEvent(ETHERSCAN_API_KEY_CHANGED_EVENT));
1623
};
1724

1825
export const hasEtherscanApiKey = (): boolean => {
1926
const apiKey = getEtherscanApiKey();
2027
return apiKey !== null && apiKey.trim().length > 0;
28+
};
29+
30+
// Custom hook to listen for API key changes
31+
export const useEtherscanApiKey = () => {
32+
const [hasApiKey, setHasApiKey] = useState(hasEtherscanApiKey());
33+
34+
useEffect(() => {
35+
const handleApiKeyChange = () => {
36+
setHasApiKey(hasEtherscanApiKey());
37+
};
38+
39+
window.addEventListener(ETHERSCAN_API_KEY_CHANGED_EVENT, handleApiKeyChange);
40+
41+
return () => {
42+
window.removeEventListener(ETHERSCAN_API_KEY_CHANGED_EVENT, handleApiKeyChange);
43+
};
44+
}, []);
45+
46+
return hasApiKey;
2147
};

0 commit comments

Comments
 (0)