Skip to content

Commit

Permalink
handle account change
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirotw committed Apr 1, 2022
1 parent c8a3118 commit 0bc1579
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
40 changes: 24 additions & 16 deletions src/custom/pages/Profile/AddressSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default function AddressSelector(props: AddressSelectorProps) {
const selectedAddress = useAddress()
const { chainId, library } = useActiveWeb3React()
const [open, setOpen] = useState(false)
const [primaryEnsName, setPrimaryEnsName] = useState<string>()
const [items, setItems] = useState<string[]>([address])
const toggle = useCallback(() => setOpen((open) => !open), [])
const node = useRef<HTMLDivElement>(null)
Expand All @@ -33,18 +32,7 @@ export default function AddressSelector(props: AddressSelectorProps) {
[dispatch, toggle]
)

const lookup = useCallback(async () => {
try {
const ensName = await library?.lookupAddress(address)
setPrimaryEnsName(ensName ?? undefined)
} catch (error) {
console.log(error)
}
}, [library, address])

useEffect(() => {
lookup()

if (!chainId) {
return
}
Expand All @@ -57,17 +45,37 @@ export default function AddressSelector(props: AddressSelectorProps) {
}
setItems([...response, address])
})
}, [address, chainId, lookup])
}, [address, chainId])

useEffect(() => {
if (selectedAddress) {
return
}

if (primaryEnsName) {
dispatch(updateAddress(primaryEnsName))
dispatch(updateAddress(address))
}, [selectedAddress, address, dispatch])

useEffect(() => {
if (!selectedAddress) {
return
}
}, [selectedAddress, primaryEnsName, dispatch])

// if the user switches accounts, reset the selected address
if (isAddress(selectedAddress) && selectedAddress !== address) {
dispatch(updateAddress(address))
return
}

// the selected address is a ens name, verify that resolves to the correct address
const verify = async () => {
const resolvedAddress = await library?.resolveName(selectedAddress)
if (resolvedAddress !== address) {
dispatch(updateAddress(address))
}
}

verify()
}, [selectedAddress, address, dispatch, library])

return (
<Wrapper ref={node}>
Expand Down
2 changes: 1 addition & 1 deletion src/custom/pages/Profile/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function ensNames(
}

const data = await new GraphQLClient(subgraphUrl).request<EnsNamesQuery>(DOMAINS_BY_ADDRESS_QUERY, {
resolvedAddress: address,
resolvedAddress: address.toLocaleLowerCase(),
})

return data.domains
Expand Down
2 changes: 1 addition & 1 deletion src/custom/state/affiliate/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface AffiliateState {
isNotificationClosed?: {
[key: string]: boolean
}
address?: string
address?: string // this can be a ENS name or an address
}

export const initialState: AffiliateState = {
Expand Down

0 comments on commit 0bc1579

Please sign in to comment.