import isEmpty from 'lodash/isEmpty' ; import isString from 'lodash/isString' ; import { createNetwork , destroyNetwork as deleteNetworkRedux , deleteNodeOrNetwork as deleteNodeOrNetworkRedux , deleteNode as deleteNodeRedux , selectNetworks , updateNetwork as updateNetworkRedux , useDispatch , useSelector } from '@store' ; import { Network , NetworkId , NodeOptions } from '@types' ; import { EthersJS } from '../../EthService/network/ethersJsProvider' ; import { getNetworkById as getNetworkByIdFunc } from './helpers' ; import { NetworkUtils } from './utils' ; export interface INetworkContext { networks : Network [ ] ; addNetwork ( item ) : void ; updateNetwork ( item ) : void ; deleteNetwork ( networkId ) : void ; getNetworkById ( networkId ) : Network ; getNetworkByChainId ( chainId ) : Network | undefined ; getNetworkNodes ( networkId ) : NodeOptions [ ] ; addNodeToNetwork ( node , network ) : void ; updateNode ( node , network , nodeName ) : void ; deleteNode ( nodeName , network ) : void ; setNetworkSelectedNode ( networkId , selectedNode ) : void ; isNodeNameAvailable ( networkId , nodeName , ignoreNames ? ) : boolean ; } function useNetworks ( ) { const networks = useSelector ( selectNetworks ) ; const dispatch = useDispatch ( ) ; const addNetwork = ( network ) => dispatch ( createNetwork ( network ) ) ; const updateNetwork = ( item ) => dispatch ( updateNetworkRedux ( item ) ) ; const deleteNodeOrNetwork = ( network , nodeName ) => dispatch ( deleteNodeOrNetworkRedux ( { network , nodeName } ) ) ; const deleteNetwork = ( id ) => dispatch ( deleteNetworkRedux ( id ) ) ; const getNetworkById = ( networkId ) => { const foundNetwork = getNetworkByIdFunc ( networkId , networks ) ; if ( foundNetwork ) { return foundNetwork ; } throw new Error ( `No network found with network id: ${ networkId }` ) ; } ; const getNetworkByChainId = ( chainId ) => { return networks . find ( ( network ) => network . chainId === chainId ) ; } ; const getNetworkNodes = ( networkId ) => { const foundNetwork = getNetworkById ( networkId ) ; if ( foundNetwork ) { return foundNetwork . nodes ; } return [ ] ; } ; const addNodeToNetwork = ( node , network ) => { let networkToAdd = network as Network ; if ( isString ( network ) ) { networkToAdd = getNetworkById ( network ) ; } const n = { ... networkToAdd , nodes : [ ... networkToAdd . nodes , node ] , selectedNode : node . name } ; updateNetwork ( n ) ; EthersJS . updateEthersInstance ( n ) ; } ; const updateNode = ( node , network , nodeName ) => { let networkToEdit = network as Network ; if ( isString ( network ) ) { networkToEdit = getNetworkById ( network ) ; } const { nodes } = networkToEdit ; const networkUpdate = { ... networkToEdit , nodes : [ ... nodes . filter ( ( n ) => n . name !== nodeName ) , node ] , selectedNode : node . name } ; updateNetwork ( networkUpdate ) ; EthersJS . updateEthersInstance ( networkUpdate ) ; } ; const deleteNode = ( nodeName , network ) => dispatch ( deleteNodeRedux ( { network , nodeName } ) ) ; const setNetworkSelectedNode = ( networkId , selectedNode ) => { const foundNetwork = networks . find ( ( n ) => n . id === networkId ) ; if ( ! foundNetwork ) { throw new Error ( `No network found with network id: ${ networkId }` ) ; } const network = { ... foundNetwork , selectedNode } ; updateNetwork ( network ) ; EthersJS . updateEthersInstance ( network ) ; } ; const isNodeNameAvailable = ( networkId , nodeName , ignoreNames = [ ] ) => { if ( isEmpty ( networkId ) || isEmpty ( nodeName ) ) return false ; const foundNetwork = getNetworkById ( networkId ) ; if ( ! foundNetwork ) { throw new Error ( `No network found with network id: ${ networkId }` ) ; } return ! foundNetwork . nodes . filter ( ( n ) => ! ignoreNames . includes ( n . name ) ) . some ( ( n ) => n . name . toLowerCase ( ) === NetworkUtils . makeNodeName ( networkId , nodeName ) . toLowerCase ( ) ) ; } ; return { networks , addNetwork , updateNetwork , deleteNetwork , deleteNodeOrNetwork , getNetworkById , getNetworkByChainId , getNetworkNodes , addNodeToNetwork , updateNode , deleteNode , setNetworkSelectedNode , isNodeNameAvailable } ; } export default useNetworks ;