Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Issue 34: Listener added to wallet provider in order to detect account changed #61

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/components/common/Header/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import Link from 'next/link'
import { StyledHeader, StyledButton, StyledNavbar } from './styles'
import { ellipseAddress } from '@/helpers/utils'
import { useHeader } from '@/hooks/pages/useHeader'
import { useRef } from 'react'
import { useEffect, useRef } from 'react'
import { useOnClickOutside } from 'usehooks-ts'
import { useConnect } from '@/hooks/common/useConnect'

export const Header = () => {
const {
Expand All @@ -23,6 +24,8 @@ export const Header = () => {
setDisplayWalletPicker,
accountBalance,
} = useHeader()

const { connect } = useConnect()
const divRef = useRef<HTMLDivElement | null>(null)
useOnClickOutside(divRef, () => {
if (displayWalletPicker) setDisplayWalletPicker(false)
Expand All @@ -32,6 +35,23 @@ export const Header = () => {
setDisplayWalletPicker(!displayWalletPicker)
}

const provider = () => {
window.ethereum.on('accountsChanged', function () {
connect()
})

return window.ethereum
}

useEffect(() => {
provider()
return () => {
window.ethereum.removeListener('accountsChanged', () => {
connect()
})
}
}, [])

return (
<StyledHeader>
<StyledNavbar
Expand Down Expand Up @@ -124,7 +144,7 @@ export const Header = () => {
color: 'orange',
icon: 'circle',
name: 'Metamask',
provider: () => window.ethereum,
provider: () => provider(),
},
],
},
Expand Down
1 change: 1 addition & 0 deletions src/hooks/common/useAccountEntity/useAccountDomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function useAccountDomains({
onSuccess,
onError,
triggerOnMount,
triggerDeps: [appState.account],
})

const entities = appState.accountDomains
Expand Down
1 change: 1 addition & 0 deletions src/hooks/common/useAccountEntity/useAccountFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function useAccountFunctions({
onSuccess,
onError,
triggerOnMount,
triggerDeps: [appState.account],
})

const entities = appState.accountFunctions
Expand Down
1 change: 1 addition & 0 deletions src/hooks/common/useAccountEntity/useAccountInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function useAccountInstances({
onSuccess,
onError,
triggerOnMount: true,
triggerDeps: [appState.account],
})

const entities = appState.accountInstances
Expand Down
1 change: 1 addition & 0 deletions src/hooks/common/useAccountEntity/useAccountSSHKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function useAccountSSHKeys({
onSuccess,
onError,
triggerOnMount,
triggerDeps: [appState.account],
})

const entities = appState.accountSSHKeys
Expand Down
1 change: 1 addition & 0 deletions src/hooks/common/useAccountEntity/useAccountVolumes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function useAccountVolumes({
onSuccess,
onError,
triggerOnMount,
triggerDeps: [appState.account],
})

const entities = appState.accountVolumes
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/common/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
export type UseRequestProps<T> = UseRequestStateProps<T> & {
doRequest: () => Promise<T>
triggerOnMount?: boolean
triggerDeps?: any[]
}

export type UseRequestReturn<T> = RequestState<T> & {
Expand All @@ -17,6 +18,7 @@ export type UseRequestReturn<T> = RequestState<T> & {
export function useRequest<T>({
doRequest,
triggerOnMount,
triggerDeps = [],
...rest
}: UseRequestProps<T>): UseRequestReturn<T> {
const [reqState, { onLoad, onSuccess, onError }] = useRequestState(rest)
Expand All @@ -37,7 +39,7 @@ export function useRequest<T>({
if (!triggerOnMount) return
request()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}, triggerDeps)

return { ...reqState, request }
}