-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1c43a1
commit 4b65bdd
Showing
7 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
packages/react-components/src/lib/components/NetworkDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { type NetworkConfig } from '../context/NetworkContext.js'; | ||
import { useAgoricNetwork } from '../hooks/network.js'; | ||
import { agoricChain, prettyTestChainName } from '../config/index.js'; | ||
import { ChangeChainCombobox } from '@interchain-ui/react'; | ||
import { useState } from 'react'; | ||
|
||
type Props = { | ||
networkConfigs: NetworkConfig[]; | ||
label?: ChangeChainCombobox['label']; | ||
size?: ChangeChainCombobox['size']; | ||
appearance?: ChangeChainCombobox['appearance']; | ||
maxHeight?: ChangeChainCombobox['maxHeight']; | ||
}; | ||
|
||
const nameForNetwork = (network: NetworkConfig) => { | ||
const name = network.testChain?.chainName ?? agoricChain?.chain_name; | ||
assert( | ||
name, | ||
'Cannot find default Agoric chain in registry, testChain must be provided.', | ||
); | ||
return name; | ||
}; | ||
|
||
const iconForNetwork = (network: NetworkConfig) => { | ||
return network.testChain === undefined | ||
? agoricChain?.images?.[0].svg | ||
: network.testChain.iconUrl; | ||
}; | ||
|
||
export const NetworkDropdown = ({ | ||
networkConfigs, | ||
label, | ||
maxHeight, | ||
size = 'md', | ||
appearance = 'bold', | ||
}: Props) => { | ||
const { networkConfig, setNetworkConfig } = useAgoricNetwork(); | ||
assert( | ||
networkConfig && setNetworkConfig, | ||
'NetworkDropdown error, NetworkContext missing', | ||
); | ||
|
||
const [selectedChain, setSelectedChain] = useState<{ | ||
label: string; | ||
value: string; | ||
iconUrl?: string; | ||
}>({ | ||
value: nameForNetwork(networkConfig), | ||
label: prettyTestChainName(nameForNetwork(networkConfig)), | ||
iconUrl: iconForNetwork(networkConfig), | ||
}); | ||
|
||
const dropdownList = networkConfigs.map(config => { | ||
return { | ||
value: nameForNetwork(config), | ||
label: prettyTestChainName(nameForNetwork(config)), | ||
iconUrl: iconForNetwork(config), | ||
}; | ||
}); | ||
|
||
return ( | ||
<ChangeChainCombobox | ||
maxHeight={maxHeight} | ||
label={label} | ||
isClearable={false} | ||
defaultSelected={selectedChain} | ||
size={size} | ||
valueItem={selectedChain} | ||
appearance={appearance} | ||
onItemSelected={item => { | ||
if (!item) return; | ||
const selectedNetworkConfig = networkConfigs.find(network => { | ||
return nameForNetwork(network) === item?.value; | ||
}); | ||
assert( | ||
selectedNetworkConfig, | ||
'Selected chain missing from networkConfigs', | ||
); | ||
setSelectedChain(item); | ||
setNetworkConfig(selectedNetworkConfig); | ||
}} | ||
options={dropdownList} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './ConnectWalletButton'; | ||
export * from './NodeSelectorModal'; | ||
export * from './AmountInput'; | ||
export * from './NetworkDropdown'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './agoric.js'; | ||
export * from './amountInput.js'; | ||
export * from './network.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { useContext } from 'react'; | ||
import { NetworkContext } from '../context'; | ||
|
||
export const useAgoricNetwork = () => useContext(NetworkContext); |