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

Fix problem with edit custom network #304

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { ArrayOneOrMore, ChainExtended } from '@/types'
import { CHAINS_IMG_PATH } from './images'
import { RpcUrl } from '@/services/useink/chains/data/types'
import { WalletConnectionEvents } from '@/domain'

export const DEFAULT_DECIMALS = 12
export const MAX_CUSTOM_NAME_LENGTH = 10
Expand Down Expand Up @@ -46,6 +47,11 @@ export const updateChain = (chains: ChainExtended[], chain: ChainExtended) => {
chain => chain.id === OPTION_FOR_CUSTOM_NETWORK
)
chains[chainIndex] = chain

document.dispatchEvent(
new CustomEvent(WalletConnectionEvents.customChainNameChanged)
)

return chains
}

Expand Down
3 changes: 2 additions & 1 deletion src/domain/DomainEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export const enum WalletConnectionEvents {
walletConnectInit = 'walletConnectInit',
changeAccountAddress = 'changeAccountAddress',
networkChanged = 'networkChanged'
networkChanged = 'networkChanged',
customChainNameChanged = 'customChainNameChanged'
}

export const enum UserContractEvents {
Expand Down
6 changes: 1 addition & 5 deletions src/hooks/userContracts/useListUserContracts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { useCallback, useEffect, useState } from 'react'

import { useLocalDbContext } from '@/context/LocalDbContext'
import {
UserContractDetails,
WalletConnectionEvents,
UserContractEvents
} from '@/domain'
import { UserContractDetails, UserContractEvents } from '@/domain'
import { useListDeployments } from '../deployments/useListDeployments'
import { useMultiEventListener } from '@/hooks/useMultipleEventListener'
import { ChainId } from '@/services/useink/chains'
Expand Down
25 changes: 21 additions & 4 deletions src/view/HomeView/ContractsTableWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { useListUserContracts } from '@/hooks/userContracts/useListUserContracts
import { Box, Typography, Paper } from '@mui/material'
import NetworkBadge from '@/view/components/NetworkBadge'
import { useDownloadMetadata } from '@/view/components/ContractsTable/useDownloadMetadata'
import { WalletConnectionEvents } from '@/domain'
import { useMultiEventListener } from '@/hooks/useMultipleEventListener'
import { useCallback, useEffect, useState } from 'react'

export function ContractsTableWidget() {
const { accountConnected, networkConnected } = useNetworkAccountsContext()
const { userContracts, isLoading } = useListUserContracts(
accountConnected?.address,
networkConnected
)
const { logo, name: networkName } = getChain(networkConnected)
const [chain, setChain] = useState(getChain(networkConnected))

const { userContractItems: items, onDownloadSource } =
useDownloadMetadata(userContracts)

Expand All @@ -21,6 +25,19 @@ export function ContractsTableWidget() {
userContractItems = items.filter(item => item.hidden === false)
}

const updateChain = useCallback(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the function with the useCallback is not passed to a child I don't see the point, as this has a computational cost and I would prefer to give it to a useMemo in useContractItems and run the simple logic in the effects.

Such as:

const userContractItems = useMemo(() => {
    return Array.isArray(items) ? items.filter(item => item.hidden === false) : [];
  }, [items]);

  useEffect(() => {
    const updatedChain = getChain(networkConnected);
    setChain(updatedChain);
  }, [networkConnected]);

  useMultiEventListener([WalletConnectionEvents.customChainNameChanged], () => {
    const updatedChain = getChain(networkConnected);
    setChain(updatedChain);
  });

const chain = getChain(networkConnected)
setChain(chain)
}, [networkConnected])

useEffect(() => {
updateChain()
}, [updateChain])

useMultiEventListener([WalletConnectionEvents.customChainNameChanged], () => {
updateChain()
})

return (
<>
{accountConnected && userContracts && (
Expand All @@ -38,10 +55,10 @@ export function ContractsTableWidget() {
on
</Typography>
<NetworkBadge
name={networkName}
logo={logo.src}
name={chain.name}
logo={chain.logo.src}
logoSize={{ width: 20, height: 20 }}
description={logo.alt}
description={chain.logo.alt}
/>
</Box>
</Box>
Expand Down
Loading