Skip to content

Commit cab8e84

Browse files
Merge pull request #18 from Agent-BossFighters/migration_testnet_mainnet
Check network and minor improvement
2 parents dc4e01b + b28a511 commit cab8e84

10 files changed

Lines changed: 7740 additions & 7547 deletions

File tree

dist/index.es.js

Lines changed: 7071 additions & 7040 deletions
Large diffs are not rendered by default.

dist/index.umd.js

Lines changed: 480 additions & 485 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "player-map",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"type": "module",
55
"main": "dist/index.js",
66
"module": "dist/index.js",

src/PlayerMapGraph.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ interface PlayerMapGraphProps {
88
walletAddress?: string;
99
}
1010

11+
// Définir les types pour les props des composants
12+
interface GraphVisualizationProps {
13+
endpoint: string;
14+
onNodeSelect: (node: any) => void;
15+
onLoadingChange: (isLoading: boolean) => void;
16+
walletAddress?: string;
17+
}
18+
19+
interface LoadingAnimationProps {}
20+
1121
const PlayerMapGraph: React.FC<PlayerMapGraphProps> = ({ walletAddress }) => {
12-
const [selectedNode, setSelectedNode] = useState(null);
22+
const [selectedNode, setSelectedNode] = useState<any>(null);
1323
const [selectedEndpoint, setSelectedEndpoint] = useState("base"); // TODO: change to mainnet
1424
const [isLoading, setIsLoading] = useState(false);
1525

src/RegistrationForm.tsx

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
import { ipfsToHttpUrl, isIpfsUrl, uploadToPinata } from "./utils/pinata";
1010
import PlayerCreationProgress from "./PlayerCreationProgress";
1111
import { usePlayerCreationService } from "./services/playerCreationService";
12+
import { useNetworkCheck } from "./shared/hooks/useNetworkCheck";
13+
import { NetworkSwitchMessage } from "./shared/components/NetworkSwitchMessage";
1214

1315
interface RegistrationFormProps {
1416
isOpen: boolean;
@@ -70,6 +72,11 @@ const RegistrationForm: React.FC<RegistrationFormProps> = ({
7072
publicClient
7173
);
7274

75+
const { isCorrectNetwork, currentChainId, targetChainId } = useNetworkCheck({
76+
walletConnected,
77+
publicClient: wagmiConfig?.publicClient
78+
});
79+
7380
useEffect(() => {
7481
const checkExistingAtom = async () => {
7582
if (!walletAddress || !walletConnected) return;
@@ -178,6 +185,7 @@ const RegistrationForm: React.FC<RegistrationFormProps> = ({
178185
// Close the form after 3 seconds
179186
setTimeout(() => {
180187
onClose();
188+
window.location.reload();
181189
}, 3000);
182190
} catch (error) {
183191
console.error("Error creating player:", error);
@@ -271,24 +279,31 @@ const RegistrationForm: React.FC<RegistrationFormProps> = ({
271279
Create Your Player
272280
</h2>
273281

274-
<PlayerCreationProgress
275-
step={step}
276-
isCreatingAtom={isCreatingAtom}
277-
isCreatingTriples={isCreatingTriples}
278-
creationSuccess={creationSuccess}
279-
atomId={atomId}
280-
tripleCreated={tripleCreated}
281-
walletAddress={walletAddress}
282-
hasExistingAtom={hasExistingAtom}
283-
formData={formData}
284-
handleInputChange={handleInputChange}
285-
handleSelectChange={handleSelectChange}
286-
handleFileUpload={handleFileUpload}
287-
handleSubmit={handleSubmit}
288-
isLoading={isLoading}
289-
isUploading={isUploading}
290-
fileInputRef={fileInputRef}
291-
/>
282+
{!isCorrectNetwork ? (
283+
<NetworkSwitchMessage
284+
currentChainId={currentChainId}
285+
targetChainId={targetChainId}
286+
/>
287+
) : (
288+
<PlayerCreationProgress
289+
step={step}
290+
isCreatingAtom={isCreatingAtom}
291+
isCreatingTriples={isCreatingTriples}
292+
creationSuccess={creationSuccess}
293+
atomId={atomId}
294+
tripleCreated={tripleCreated}
295+
walletAddress={walletAddress}
296+
hasExistingAtom={hasExistingAtom}
297+
formData={formData}
298+
handleInputChange={handleInputChange}
299+
handleSelectChange={handleSelectChange}
300+
handleFileUpload={handleFileUpload}
301+
handleSubmit={handleSubmit}
302+
isLoading={isLoading}
303+
isUploading={isUploading}
304+
fileInputRef={fileInputRef}
305+
/>
306+
)}
292307
</div>
293308
</div>
294309
);

src/components/vote/ClaimVoting.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { ConnectWalletModal, CreatePlayerModal } from "../modals";
1111
import { useTripleByCreator } from "../../hooks/useTripleByCreator";
1212
import { PLAYER_TRIPLE_TYPES } from "../../utils/constants";
1313
import RegistrationForm from "../../RegistrationForm";
14+
import { useNetworkCheck } from '../../shared/hooks/useNetworkCheck';
15+
import { NetworkSwitchMessage } from '../../shared/components/NetworkSwitchMessage';
1416

1517
interface ClaimVotingProps {
1618
walletConnected?: any;
@@ -139,6 +141,11 @@ export const ClaimVoting: React.FC<ClaimVotingProps> = ({
139141
}
140142
};
141143

144+
const { isCorrectNetwork, currentChainId, targetChainId } = useNetworkCheck({
145+
walletConnected,
146+
publicClient
147+
});
148+
142149
// Si l'utilisateur n'a pas connecté son wallet, afficher juste la modale de connexion
143150
if (!isWalletReady) {
144151
return (
@@ -240,6 +247,13 @@ export const ClaimVoting: React.FC<ClaimVotingProps> = ({
240247
numberOfTransactions={numberOfTransactions}
241248
/>
242249
<TransactionStatusDisplay transactionStatus={transactionStatus} />
250+
251+
{!isCorrectNetwork && (
252+
<NetworkSwitchMessage
253+
currentChainId={currentChainId}
254+
targetChainId={targetChainId}
255+
/>
256+
)}
243257
</div>
244258
);
245259
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
3+
interface NetworkSwitchMessageProps {
4+
currentChainId: number | null;
5+
targetChainId: number;
6+
}
7+
8+
export const NetworkSwitchMessage: React.FC<NetworkSwitchMessageProps> = ({
9+
currentChainId,
10+
targetChainId
11+
}) => {
12+
return (
13+
<div style={{
14+
padding: '15px',
15+
backgroundColor: '#2e2e40',
16+
borderRadius: '8px',
17+
margin: '10px 0',
18+
textAlign: 'center'
19+
}}>
20+
<p style={{ color: '#ff4444', marginBottom: '10px' }}>
21+
You are not on the correct network
22+
</p>
23+
<p style={{ color: '#aaa', fontSize: '0.9em', marginBottom: '10px' }}>
24+
Current network: {currentChainId || 'Not connected'}<br />
25+
Required network: Base (Chain ID: {targetChainId})
26+
</p>
27+
<p style={{ color: '#fff', fontSize: '0.9em' }}>
28+
Please switch to Base network in your wallet to continue
29+
</p>
30+
</div>
31+
);
32+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { useEffect, useState } from 'react';
2+
import { ATOM_CONTRACT_CHAIN_ID } from '../../abi';
3+
4+
interface UseNetworkCheckProps {
5+
walletConnected?: any;
6+
publicClient?: any;
7+
}
8+
9+
interface NetworkCheckResult {
10+
isCorrectNetwork: boolean;
11+
currentChainId: number | null;
12+
targetChainId: number;
13+
}
14+
15+
interface WalletError {
16+
code: number;
17+
message: string;
18+
}
19+
20+
// Configuration pour Base
21+
const BASE_CONFIG = {
22+
chainId: Number(ATOM_CONTRACT_CHAIN_ID),
23+
chainName: 'Base',
24+
nativeCurrency: {
25+
name: 'ETH',
26+
symbol: 'ETH',
27+
decimals: 18,
28+
},
29+
rpcUrls: ['https://mainnet.base.org'],
30+
blockExplorerUrls: ['https://basescan.org'],
31+
};
32+
33+
export const useNetworkCheck = ({ walletConnected, publicClient }: UseNetworkCheckProps): NetworkCheckResult => {
34+
const [currentChainId, setCurrentChainId] = useState<number | null>(null);
35+
const targetChainId = Number(ATOM_CONTRACT_CHAIN_ID);
36+
37+
useEffect(() => {
38+
const checkNetwork = async () => {
39+
if (!walletConnected || !publicClient) return;
40+
41+
try {
42+
const chainId = await publicClient.getChainId();
43+
setCurrentChainId(Number(chainId));
44+
} catch (error) {
45+
console.error('Error checking network:', error);
46+
setCurrentChainId(null);
47+
}
48+
};
49+
50+
checkNetwork();
51+
}, [walletConnected, publicClient]);
52+
53+
const switchNetwork = async () => {
54+
if (!walletConnected) return;
55+
56+
try {
57+
console.log('Attempting switchChain with chainId:', targetChainId);
58+
await walletConnected.switchChain({ chainId: targetChainId });
59+
} catch (error) {
60+
console.error('Error switching network:', error);
61+
const walletError = error as WalletError;
62+
63+
if (walletError.code === 4902) {
64+
try {
65+
console.log('Network not found, attempting to add it');
66+
await walletConnected.addChain(BASE_CONFIG);
67+
68+
console.log('Network added, attempting switch again');
69+
await walletConnected.switchChain({ chainId: targetChainId });
70+
} catch (addError) {
71+
console.error('Error adding Base network:', addError);
72+
throw new Error('Unable to add Base network to your wallet. Please add it manually.');
73+
}
74+
} else {
75+
throw new Error('Unable to switch network. Please check your wallet.');
76+
}
77+
}
78+
};
79+
80+
return {
81+
isCorrectNetwork: currentChainId === targetChainId,
82+
currentChainId,
83+
targetChainId
84+
};
85+
};

src/types/playermap_graph.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare module 'playermap_graph' {
2+
interface GraphVisualizationProps {
3+
endpoint: string;
4+
onNodeSelect: (node: any) => void;
5+
onLoadingChange: (isLoading: boolean) => void;
6+
walletAddress?: string;
7+
}
8+
9+
export const GraphVisualization: React.FC<GraphVisualizationProps>;
10+
export const LoadingAnimation: React.FC;
11+
}

0 commit comments

Comments
 (0)