|
| 1 | +import { AlgodConfig, NetworkId, useNetwork } from '@txnlab/use-wallet-react' |
| 2 | +import * as React from 'react' |
| 3 | + |
| 4 | +export function NetworkControls() { |
| 5 | + const { |
| 6 | + activeNetwork, |
| 7 | + networkConfig, |
| 8 | + activeNetworkConfig, |
| 9 | + setActiveNetwork, |
| 10 | + updateAlgodConfig, |
| 11 | + resetNetworkConfig |
| 12 | + } = useNetwork() |
| 13 | + |
| 14 | + const [error, setError] = React.useState<string>('') |
| 15 | + const [showConfig, setShowConfig] = React.useState(false) |
| 16 | + |
| 17 | + const [configForm, setConfigForm] = React.useState<Partial<AlgodConfig>>({ |
| 18 | + baseServer: activeNetworkConfig.algod.baseServer, |
| 19 | + port: activeNetworkConfig.algod.port?.toString() || '', |
| 20 | + token: activeNetworkConfig.algod.token?.toString() || '' |
| 21 | + }) |
| 22 | + |
| 23 | + React.useEffect(() => { |
| 24 | + setConfigForm({ |
| 25 | + baseServer: activeNetworkConfig.algod.baseServer, |
| 26 | + port: activeNetworkConfig.algod.port?.toString() || '', |
| 27 | + token: activeNetworkConfig.algod.token?.toString() || '' |
| 28 | + }) |
| 29 | + }, [activeNetworkConfig]) |
| 30 | + |
| 31 | + const handleNetworkSwitch = async (networkId: NetworkId) => { |
| 32 | + try { |
| 33 | + setError('') |
| 34 | + await setActiveNetwork(networkId) |
| 35 | + } catch (error) { |
| 36 | + setError(error instanceof Error ? error.message : 'Failed to switch networks') |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 41 | + const { name, value } = event.target |
| 42 | + setConfigForm((prev) => ({ ...prev, [name]: value })) |
| 43 | + } |
| 44 | + |
| 45 | + const handleConfigSubmit = async (event: React.FormEvent) => { |
| 46 | + event.preventDefault() |
| 47 | + try { |
| 48 | + setError('') |
| 49 | + updateAlgodConfig(activeNetwork, { |
| 50 | + baseServer: configForm.baseServer, |
| 51 | + port: configForm.port || undefined, |
| 52 | + token: configForm.token |
| 53 | + }) |
| 54 | + } catch (error) { |
| 55 | + setError(error instanceof Error ? error.message : 'Failed to update node configuration') |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const handleResetConfig = () => { |
| 60 | + try { |
| 61 | + setError('') |
| 62 | + resetNetworkConfig(activeNetwork) |
| 63 | + } catch (error) { |
| 64 | + setError(error instanceof Error ? error.message : 'Failed to reset node configuration') |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <div className="network-group"> |
| 70 | + <h4>Network Controls</h4> |
| 71 | + <div className="active-network">Active: {activeNetwork}</div> |
| 72 | + |
| 73 | + <div className="network-buttons"> |
| 74 | + {Object.keys(networkConfig).map((networkId) => ( |
| 75 | + <button |
| 76 | + key={networkId} |
| 77 | + onClick={() => handleNetworkSwitch(networkId as NetworkId)} |
| 78 | + disabled={networkId === activeNetwork} |
| 79 | + > |
| 80 | + Switch to {networkId} |
| 81 | + </button> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className="config-section"> |
| 86 | + <button onClick={() => setShowConfig(!showConfig)}> |
| 87 | + {showConfig ? 'Hide' : 'Show'} Network Config |
| 88 | + </button> |
| 89 | + |
| 90 | + {showConfig && ( |
| 91 | + <form onSubmit={handleConfigSubmit} className="config-form"> |
| 92 | + <div className="form-group"> |
| 93 | + <label htmlFor="baseServer">Base Server:</label> |
| 94 | + <input |
| 95 | + type="text" |
| 96 | + id="baseServer" |
| 97 | + name="baseServer" |
| 98 | + value={configForm.baseServer} |
| 99 | + onChange={handleInputChange} |
| 100 | + placeholder="https://mainnet-api.4160.nodely.dev" |
| 101 | + /> |
| 102 | + </div> |
| 103 | + |
| 104 | + <div className="form-group"> |
| 105 | + <label htmlFor="port">Port:</label> |
| 106 | + <input |
| 107 | + type="text" |
| 108 | + id="port" |
| 109 | + name="port" |
| 110 | + value={configForm.port} |
| 111 | + onChange={handleInputChange} |
| 112 | + placeholder="443" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + |
| 116 | + <div className="form-group"> |
| 117 | + <label htmlFor="token">Token:</label> |
| 118 | + <input |
| 119 | + type="text" |
| 120 | + id="token" |
| 121 | + name="token" |
| 122 | + value={configForm.token?.toString() || ''} |
| 123 | + onChange={handleInputChange} |
| 124 | + placeholder="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 125 | + /> |
| 126 | + </div> |
| 127 | + |
| 128 | + <button type="submit">Update Configuration</button> |
| 129 | + <button type="button" onClick={handleResetConfig}> |
| 130 | + Reset Configuration |
| 131 | + </button> |
| 132 | + </form> |
| 133 | + )} |
| 134 | + |
| 135 | + <div className="current-config"> |
| 136 | + <h5>Current Algod Configuration:</h5> |
| 137 | + <pre>{JSON.stringify(activeNetworkConfig.algod, null, 2)}</pre> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + |
| 141 | + {error && <div className="error-message">{error}</div>} |
| 142 | + </div> |
| 143 | + ) |
| 144 | +} |
0 commit comments