11"use client" ;
22
3- import { createContext , useContext , useState , useEffect , ReactNode } from "react" ;
3+ import {
4+ createContext ,
5+ useContext ,
6+ useState ,
7+ useEffect ,
8+ ReactNode ,
9+ } from "react" ;
410import { useRouter } from "next/navigation" ;
511import { MagicService } from "@/lib/embedded-wallet/get-magic" ;
612import { useConsole , LogType , LogMethod } from "./ConsoleContext" ;
713
14+ export enum Network {
15+ POLYGON = "polygon" ,
16+ ETHEREUM = "ethereum" ,
17+ OPTIMISM = "optimism" ,
18+ HEDERA = "hedera" ,
19+ SOLANA = "solana" ,
20+ }
21+
822interface WalletContextType {
923 publicAddress : string | null ;
10- selectedNetwork : string ;
24+ selectedNetwork : Network ;
1125 isAuthenticated : boolean ;
1226 isLoading : boolean ;
1327 userInfo : any | null ;
14- handleNetworkChange : ( network : string ) => void ;
28+ handleNetworkChange : ( network : Network ) => void ;
1529 handleLogout : ( ) => Promise < void > ;
1630 fetchAllNetworkAddresses : ( ) => Promise < void > ;
1731}
1832
1933const WalletContext = createContext < WalletContextType | undefined > ( undefined ) ;
2034
2135export function WalletProvider ( { children } : { children : ReactNode } ) {
22- const [ selectedNetwork , setSelectedNetwork ] = useState < string > ( "ethereum" ) ;
36+ const [ selectedNetwork , setSelectedNetwork ] = useState < Network > (
37+ Network . ETHEREUM
38+ ) ;
2339 const [ isAuthenticated , setIsAuthenticated ] = useState < boolean > ( false ) ;
2440 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
2541 const [ userInfo , setUserInfo ] = useState < any | null > ( null ) ;
26-
2742 const { logToConsole } = useConsole ( ) ;
2843 const router = useRouter ( ) ;
2944
3045 // Helper function to get public address from userInfo based on selected network
3146 const getPublicAddress = ( ) : string | null => {
3247 if ( ! userInfo ?. wallets ) return null ;
33-
48+
3449 // EVM networks (polygon, ethereum, optimism) all use the ethereum wallet
35- if ( [ 'polygon' , 'ethereum' , 'optimism' ] . includes ( selectedNetwork ) ) {
50+ if (
51+ [ Network . POLYGON , Network . ETHEREUM , Network . OPTIMISM ] . includes (
52+ selectedNetwork
53+ )
54+ ) {
3655 return userInfo . wallets . ethereum ?. publicAddress || null ;
3756 }
38-
57+
3958 // Other networks use their own wallet
4059 return userInfo . wallets [ selectedNetwork ] ?. publicAddress || null ;
4160 } ;
4261
4362 const fetchAllNetworkAddresses = async ( ) => {
4463 try {
4564 const fetchedUserInfo = await MagicService . magic . user . getInfo ( ) ;
46- logToConsole ( LogType . INFO , LogMethod . MAGIC_USER_GET_INFO , 'User info fetched' , fetchedUserInfo ) ;
47-
65+ logToConsole (
66+ LogType . INFO ,
67+ LogMethod . MAGIC_USER_GET_INFO ,
68+ "User info fetched" ,
69+ fetchedUserInfo
70+ ) ;
71+
4872 // Store user info for other components to use
4973 setUserInfo ( fetchedUserInfo ) ;
50- logToConsole ( LogType . SUCCESS , LogMethod . MAGIC_USER_GET_INFO , 'User info and wallets fetched successfully' , fetchedUserInfo ) ;
74+ logToConsole (
75+ LogType . SUCCESS ,
76+ LogMethod . MAGIC_USER_GET_INFO ,
77+ "User info and wallets fetched successfully" ,
78+ fetchedUserInfo
79+ ) ;
5180 } catch ( error ) {
52- logToConsole ( LogType . ERROR , LogMethod . MAGIC_USER_GET_INFO , 'Error fetching user info' , error ) ;
81+ logToConsole (
82+ LogType . ERROR ,
83+ LogMethod . MAGIC_USER_GET_INFO ,
84+ "Error fetching user info" ,
85+ error
86+ ) ;
5387 }
5488 } ;
5589
5690 // Check if user is already authenticated on component mount
5791 useEffect ( ( ) => {
58- const savedNetwork = localStorage . getItem ( 'magic_selectedNetwork' ) ;
59- if ( savedNetwork && [ 'polygon' , 'ethereum' , 'optimism' , 'hedera' , 'solana' ] . includes ( savedNetwork ) ) {
92+ const savedNetwork = localStorage . getItem (
93+ "magic_selectedNetwork"
94+ ) as Network | null ;
95+ if (
96+ savedNetwork &&
97+ [
98+ Network . POLYGON ,
99+ Network . ETHEREUM ,
100+ Network . OPTIMISM ,
101+ Network . HEDERA ,
102+ Network . SOLANA ,
103+ ] . includes ( savedNetwork )
104+ ) {
60105 setSelectedNetwork ( savedNetwork ) ;
61106 }
62107 const checkAuthStatus = async ( ) => {
63108 try {
64109 setIsLoading ( true ) ;
65- logToConsole ( LogType . INFO , LogMethod . MAGIC_USER_IS_LOGGED_IN , 'Checking authentication status...' ) ;
110+ logToConsole (
111+ LogType . INFO ,
112+ LogMethod . MAGIC_USER_IS_LOGGED_IN ,
113+ "Checking authentication status..."
114+ ) ;
66115 const isLoggedIn = await MagicService . magic . user . isLoggedIn ( ) ;
67116 if ( isLoggedIn ) {
68117 setIsAuthenticated ( true ) ;
69118 await fetchAllNetworkAddresses ( ) ;
70119 } else {
71120 setIsAuthenticated ( false ) ;
72- logToConsole ( LogType . INFO , LogMethod . MAGIC_USER_IS_LOGGED_IN , 'User is not authenticated, redirecting to embedded-wallet page' ) ;
73- router . push ( '/embedded-wallet' ) ;
121+ logToConsole (
122+ LogType . INFO ,
123+ LogMethod . MAGIC_USER_IS_LOGGED_IN ,
124+ "User is not authenticated, redirecting to embedded-wallet page"
125+ ) ;
126+ router . push ( "/embedded-wallet" ) ;
74127 }
75128 } catch ( error : unknown ) {
76129 const err = error as Error ;
77130 setIsAuthenticated ( false ) ;
78- logToConsole ( LogType . ERROR , LogMethod . MAGIC_USER_IS_LOGGED_IN , 'Error checking auth status' , err . message ) ;
131+ logToConsole (
132+ LogType . ERROR ,
133+ LogMethod . MAGIC_USER_IS_LOGGED_IN ,
134+ "Error checking auth status" ,
135+ err . message
136+ ) ;
79137 console . error ( "Error checking auth status:" , error ) ;
80- router . push ( ' /embedded-wallet' ) ;
138+ router . push ( " /embedded-wallet" ) ;
81139 } finally {
82140 setIsLoading ( false ) ;
83141 }
84142 } ;
85143 checkAuthStatus ( ) ;
86144 } , [ ] ) ;
87145
88- const handleNetworkChange = ( network : string ) => {
146+ const handleNetworkChange = ( network : Network ) => {
89147 setSelectedNetwork ( network ) ;
90-
148+
91149 // Persist network selection to localStorage
92- localStorage . setItem ( ' magic_selectedNetwork' , network ) ;
93-
150+ localStorage . setItem ( " magic_selectedNetwork" , network ) ;
151+
94152 // Log the network change
95- logToConsole ( LogType . SUCCESS , LogMethod . MAGIC_USER_GET_INFO , `Network changed to: ${ network } ` ) ;
153+ logToConsole (
154+ LogType . SUCCESS ,
155+ LogMethod . MAGIC_USER_GET_INFO ,
156+ `Network changed to: ${ network } `
157+ ) ;
96158 } ;
97159
98160 const handleLogout = async ( ) => {
99161 try {
100- logToConsole ( LogType . INFO , LogMethod . MAGIC_USER_LOGOUT , 'Logging out user...' ) ;
162+ logToConsole (
163+ LogType . INFO ,
164+ LogMethod . MAGIC_USER_LOGOUT ,
165+ "Logging out user..."
166+ ) ;
101167 const res = await MagicService . magic . user . logout ( ) ;
102168 if ( res ) {
103169 setIsAuthenticated ( false ) ;
104- logToConsole ( LogType . SUCCESS , LogMethod . MAGIC_USER_LOGOUT , 'User logged out successfully' ) ;
105- router . replace ( '/embedded-wallet' ) ;
170+ logToConsole (
171+ LogType . SUCCESS ,
172+ LogMethod . MAGIC_USER_LOGOUT ,
173+ "User logged out successfully"
174+ ) ;
175+ router . replace ( "/embedded-wallet" ) ;
106176 }
107177 } catch ( error ) {
108- logToConsole ( LogType . ERROR , LogMethod . MAGIC_USER_LOGOUT , 'Logout error' , error ) ;
178+ logToConsole (
179+ LogType . ERROR ,
180+ LogMethod . MAGIC_USER_LOGOUT ,
181+ "Logout error" ,
182+ error
183+ ) ;
109184 console . error ( "Logout error:" , error ) ;
110185 }
111186 } ;
@@ -122,16 +197,14 @@ export function WalletProvider({ children }: { children: ReactNode }) {
122197 } ;
123198
124199 return (
125- < WalletContext . Provider value = { value } >
126- { children }
127- </ WalletContext . Provider >
200+ < WalletContext . Provider value = { value } > { children } </ WalletContext . Provider >
128201 ) ;
129202}
130203
131204export function useEmbeddedWallet ( ) {
132205 const context = useContext ( WalletContext ) ;
133206 if ( context === undefined ) {
134- throw new Error ( ' useEmbeddedWallet must be used within a WalletProvider' ) ;
207+ throw new Error ( " useEmbeddedWallet must be used within a WalletProvider" ) ;
135208 }
136209 return context ;
137210}
0 commit comments