Skip to content

Commit

Permalink
fix: wallets disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniya Bashieva committed Jan 22, 2025
1 parent 52bccae commit 6e558be
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
35 changes: 35 additions & 0 deletions src/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { Component, ErrorInfo, ReactNode } from 'react';

interface ErrorBoundaryProps {
children: ReactNode;
}

interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
}

class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null };
}

static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error };
}

componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
console.error("Error caught by ErrorBoundary:", error, errorInfo);
}

render() {
if (this.state.hasError) {
return <h1>Something went wrong. Please try again later.</h1>;
}

return this.props.children;
}
}

export default ErrorBoundary;
10 changes: 0 additions & 10 deletions src/components/TokenGetter/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useContext, useState } from 'react'
import Button from '../common/Button'
import { Input as NumericalInput } from '../common/NumericalInput'
import { Loader } from '../common/Loader'
import { useWeb3React } from '@web3-react/core'
import { AxiosError } from 'axios'
import { TokenSelect } from './TokenSelect'
import { useHttp } from '../../utils/useHttp'
Expand All @@ -17,7 +16,6 @@ export default function Form(props: any) {
}
} = props
const { post } = useHttp()
const { deactivate } = useWeb3React()
const [amount, setAmount] = useState(0)
const [token, setToken] = useState<any>({})
const [isMaxAmountIncreased, setIsMaxAmointIncreased] = useState(false)
Expand All @@ -44,14 +42,6 @@ export default function Form(props: any) {
.catch((err: AxiosError) => {
const status = err.response?.status

if (status >= 500) {
try {
deactivate()
} catch (error) {
console.error(error)
}
}

const details =
status === 524
? 'The airdrop is taking longer than expected. Please check your tokens later.'
Expand Down
17 changes: 8 additions & 9 deletions src/components/common/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { useContext } from 'react'
import Logo from '@/assets/logo.svg'
import { useWeb3React } from '@web3-react/core'
import { isMobile } from '../../../config'
import { WalletContext } from "../../../contexts/wallets";

export const Header = () => {
const { deactivate, active } = useWeb3React()
const { connectedWallet, disconnectWallet } = useContext(WalletContext)

async function disconnect() {
try {
deactivate()
} catch (ex) {
console.log(ex)
}
disconnectWallet()
}

return <div className='w-full p-6 flex justify-between items-center relative z-10'>
<div onClick={disconnect} className={active ? 'cursor-pointer' : null}>
return (
<div className='w-full p-6 flex justify-between items-center relative z-10'>
<div onClick={disconnect} className={connectedWallet ? 'cursor-pointer' : ''}>
<Logo />
</div>
{!isMobile() && (
Expand All @@ -37,4 +35,5 @@ export const Header = () => {
</div>
)}
</div>
)
}
13 changes: 11 additions & 2 deletions src/contexts/wallets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface WalletContextType {
notification: string;
handleConnectWallet: (wallet: EIP6963ProviderDetail) => void;
setNotification: (message: string) => void;
disconnectWallet: () => void;
}

export const WalletContext = createContext<WalletContextType>({
Expand All @@ -23,7 +24,8 @@ export const WalletContext = createContext<WalletContextType>({
currentProvider: null,
notification: '',
handleConnectWallet: (wallet: EIP6963ProviderDetail) => {},
setNotification: (message: string) => {}
setNotification: (message: string) => {},
disconnectWallet: () => {}
})

export const WalletProvider = ({ children }) => {
Expand Down Expand Up @@ -85,6 +87,7 @@ export const WalletProvider = ({ children }) => {
setCurrentProvider(wallet.provider)
connectNetwork(supportedProviders.get(wallet.info.rdns))!
}

useEffect(() => {
window.addEventListener(EIP6963EventNames.Announce, onAnnounceProvider);
/*
Expand All @@ -99,8 +102,14 @@ export const WalletProvider = ({ children }) => {
window.removeEventListener(EIP6963EventNames.Announce, onAnnounceProvider)
}
}, [])

Check warning on line 104 in src/contexts/wallets.tsx

View workflow job for this annotation

GitHub Actions / deploy (20)

React Hook useEffect has a missing dependency: 'onAnnounceProvider'. Either include it or remove the dependency array

const disconnectWallet = () => {
setCurrentProvider(null)
setConnectedWallet('')
}

return (
<WalletContext.Provider value={{ connectedWallet, currentProvider, handleConnectWallet, injectedProviders, notification, setNotification }}>
<WalletContext.Provider value={{ connectedWallet, currentProvider, handleConnectWallet, injectedProviders, notification, setNotification, disconnectWallet }}>
{children}
</WalletContext.Provider>
)
Expand Down
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react'
import ReactDOM from 'react-dom/client';
import App from './App'
import ErrorBoundary from './ErrorBoundary'

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
<ErrorBoundary>
<App />
</ErrorBoundary>
</React.StrictMode>
);

0 comments on commit 6e558be

Please sign in to comment.