Skip to content

Commit 75a30b9

Browse files
authored
feat: add preferred infra button to settings (#2162)
1 parent c667de5 commit 75a30b9

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

apps/namadillo/src/App/Settings/SettingsMain.tsx

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { ToggleButton } from "@namada/components";
1+
import { ActionButton, ToggleButton } from "@namada/components";
22
import { IconTooltip } from "App/Common/IconTooltip";
33
import { routes } from "App/routes";
4+
import { chainParametersAtom } from "atoms/chain";
45
import {
56
indexerHeartbeatAtom,
67
maspIndexerHeartbeatAtom,
78
settingsAtom,
9+
updateIndexerUrlAtom,
10+
updateMaspIndexerUrlAtom,
11+
updateRpcUrlAtom,
812
updateSettingsProps,
913
} from "atoms/settings";
1014
import { useAtomValue } from "jotai";
@@ -14,13 +18,75 @@ import { version as sdkVersion } from "../../../../../packages/sdk/package.json"
1418
import { version } from "../../../package.json";
1519
import { SettingsPanelMenuItem } from "./SettingsPanelMenuItem";
1620

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+
1734
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";
1837

1938
export const SettingsMain = (): JSX.Element => {
2039
const indexerHealth = useAtomValue(indexerHeartbeatAtom);
2140
const maspIndexerHealth = useAtomValue(maspIndexerHeartbeatAtom);
2241
const settingsMutation = useAtomValue(updateSettingsProps);
2342
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+
};
2490

2591
return (
2692
<div className="flex flex-1 justify-between flex-col w-full">
@@ -39,6 +105,23 @@ export const SettingsMain = (): JSX.Element => {
39105
)}
40106
<SettingsPanelMenuItem url={routes.settingsMASP} text="MASP" />
41107
<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>
42125
</ul>
43126
<div className="text-xs">
44127
<div className="relative mb-4 flex items-center gap-2 max-w-full">

0 commit comments

Comments
 (0)