1
- import { ToggleButton } from "@namada/components" ;
1
+ import { ActionButton , ToggleButton } from "@namada/components" ;
2
2
import { IconTooltip } from "App/Common/IconTooltip" ;
3
3
import { routes } from "App/routes" ;
4
+ import { chainParametersAtom } from "atoms/chain" ;
4
5
import {
5
6
indexerHeartbeatAtom ,
6
7
maspIndexerHeartbeatAtom ,
7
8
settingsAtom ,
9
+ updateIndexerUrlAtom ,
10
+ updateMaspIndexerUrlAtom ,
11
+ updateRpcUrlAtom ,
8
12
updateSettingsProps ,
9
13
} from "atoms/settings" ;
10
14
import { useAtomValue } from "jotai" ;
@@ -14,13 +18,75 @@ import { version as sdkVersion } from "../../../../../packages/sdk/package.json"
14
18
import { version } from "../../../package.json" ;
15
19
import { SettingsPanelMenuItem } from "./SettingsPanelMenuItem" ;
16
20
21
+ type DefaultInfraProviders = {
22
+ housefire : {
23
+ rpc : string ;
24
+ indexer : string ;
25
+ masp : string ;
26
+ } ;
27
+ mainnet : {
28
+ rpc : string ;
29
+ indexer : string ;
30
+ masp : string ;
31
+ } ;
32
+ } ;
33
+
17
34
const { VITE_REVISION : revision = "" } = import . meta. env ;
35
+ const DEFAULT_INFRA_PROVIDERS_URL =
36
+ "https://raw.githubusercontent.com/anoma/namada-chain-registry/main/default_infra_providers.json" ;
18
37
19
38
export const SettingsMain = ( ) : JSX . Element => {
20
39
const indexerHealth = useAtomValue ( indexerHeartbeatAtom ) ;
21
40
const maspIndexerHealth = useAtomValue ( maspIndexerHeartbeatAtom ) ;
22
41
const settingsMutation = useAtomValue ( updateSettingsProps ) ;
23
42
const settings = useAtomValue ( settingsAtom ) ;
43
+ const { data : chainParameters } = useAtomValue ( chainParametersAtom ) ;
44
+ const rpcMutation = useAtomValue ( updateRpcUrlAtom ) ;
45
+ const indexerMutation = useAtomValue ( updateIndexerUrlAtom ) ;
46
+ const maspIndexerMutation = useAtomValue ( updateMaspIndexerUrlAtom ) ;
47
+
48
+ const handleUseDefaultInfra = async ( ) : Promise < void > => {
49
+ try {
50
+ // Fetch the default infrastructure providers from GitHub
51
+ const response = await fetch ( DEFAULT_INFRA_PROVIDERS_URL ) ;
52
+
53
+ if ( ! response . ok ) {
54
+ throw new Error (
55
+ `Failed to fetch default infrastructure providers: ${ response . statusText } `
56
+ ) ;
57
+ }
58
+
59
+ const providers : DefaultInfraProviders = await response . json ( ) ;
60
+
61
+ // Determine which network we're on based on chainId
62
+ const chainId = chainParameters ?. chainId ;
63
+
64
+ if ( ! chainId ) {
65
+ throw new Error ( "Chain parameters not loaded" ) ;
66
+ }
67
+
68
+ let networkProviders ;
69
+
70
+ if ( chainId . includes ( "housefire" ) ) {
71
+ networkProviders = providers . housefire ;
72
+ } else if ( chainId === "namada.5f5de2dd1b88cba30586420" ) {
73
+ // This is the mainnet chain ID based on the useRegistryFeatures mapping
74
+ networkProviders = providers . mainnet ;
75
+ }
76
+
77
+ if ( ! networkProviders ) {
78
+ throw new Error ( "No network providers found" ) ;
79
+ }
80
+
81
+ await Promise . all ( [
82
+ rpcMutation . mutateAsync ( networkProviders . rpc ) ,
83
+ indexerMutation . mutateAsync ( networkProviders . indexer ) ,
84
+ maspIndexerMutation . mutateAsync ( networkProviders . masp ) ,
85
+ ] ) ;
86
+ } catch ( error ) {
87
+ console . error ( "Failed to set default infrastructure:" , error ) ;
88
+ }
89
+ } ;
24
90
25
91
return (
26
92
< div className = "flex flex-1 justify-between flex-col w-full" >
@@ -39,6 +105,23 @@ export const SettingsMain = (): JSX.Element => {
39
105
) }
40
106
< SettingsPanelMenuItem url = { routes . settingsMASP } text = "MASP" />
41
107
< SettingsPanelMenuItem url = { routes . settingsLedger } text = "Ledger" />
108
+ < ActionButton
109
+ onClick = { handleUseDefaultInfra }
110
+ className = "my-2 cursor-pointer"
111
+ disabled = {
112
+ rpcMutation . isPending ||
113
+ indexerMutation . isPending ||
114
+ maspIndexerMutation . isPending
115
+ }
116
+ >
117
+ { (
118
+ rpcMutation . isPending ||
119
+ indexerMutation . isPending ||
120
+ maspIndexerMutation . isPending
121
+ ) ?
122
+ "Setting Default Infra..."
123
+ : "Use Alternate Infra" }
124
+ </ ActionButton >
42
125
</ ul >
43
126
< div className = "text-xs" >
44
127
< div className = "relative mb-4 flex items-center gap-2 max-w-full" >
0 commit comments