|
| 1 | +import { useState, useMemo, useEffect } from "react"; |
| 2 | +import { useStore } from "../store"; |
| 3 | +import * as log from "@tauri-apps/plugin-log"; |
| 4 | +import { |
| 5 | + defaultWalletshieldListenAddress, |
| 6 | + NetworkServices, |
| 7 | + readNetworkAssetFile, |
| 8 | +} from "../utils"; |
| 9 | + |
| 10 | +export function RPCEndpoints() { |
| 11 | + const [includeTestnets, setIncludeTestnets] = useState(true); |
| 12 | + const [search, setSearch] = useState(""); |
| 13 | + const [services, setServices] = useState<NetworkServices | null>(); |
| 14 | + |
| 15 | + const networkConnected = useStore((s) => s.networkConnected); |
| 16 | + const setMessage = useStore((s) => s.setMessage); |
| 17 | + |
| 18 | + const walletshieldListenAddress = useStore( |
| 19 | + (s) => s.walletshieldListenAddress, |
| 20 | + ); |
| 21 | + const wla = walletshieldListenAddress || defaultWalletshieldListenAddress; |
| 22 | + const BASE_URL = `http://localhost${wla}`; |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + try { |
| 26 | + (async () => { |
| 27 | + // read services file of the connected network |
| 28 | + if (networkConnected) { |
| 29 | + const f = await readNetworkAssetFile( |
| 30 | + networkConnected, |
| 31 | + "services.json", |
| 32 | + ); |
| 33 | + const s = JSON.parse(f) as NetworkServices; |
| 34 | + setServices(s); |
| 35 | + } |
| 36 | + })(); |
| 37 | + } catch (error: any) { |
| 38 | + log.error(`${error}`); |
| 39 | + setMessage("error", `${error}`); |
| 40 | + } |
| 41 | + }, []); |
| 42 | + |
| 43 | + const filtered = useMemo(() => { |
| 44 | + if (!services) return []; |
| 45 | + const endpoints = services.RPCEndpoints; |
| 46 | + return endpoints |
| 47 | + .filter((n) => includeTestnets || !n.isTestnet) |
| 48 | + .filter((n) => { |
| 49 | + const term = search.toLowerCase(); |
| 50 | + return ( |
| 51 | + n.chain.toLowerCase().includes(term) || |
| 52 | + n.network.toLowerCase().includes(term) |
| 53 | + ); |
| 54 | + }); |
| 55 | + }, [includeTestnets, search, services]); |
| 56 | + |
| 57 | + if (!networkConnected) { |
| 58 | + return ( |
| 59 | + <div className="p-4 flex justify-center"> |
| 60 | + <div className="alert alert-warning shadow-lg"> |
| 61 | + <span>Connect to a network to see its supported RPC endpoints.</span> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="p-4"> |
| 69 | + <div className="flex flex-col md:flex-row md:items-center md:justify-between mb-4 space-y-2 md:space-y-0"> |
| 70 | + <div className="form-control flex flex-row gap-x-4"> |
| 71 | + <label className="label"> |
| 72 | + <span className="label-text">Search Networks</span> |
| 73 | + </label> |
| 74 | + <input |
| 75 | + type="text" |
| 76 | + placeholder="Search..." |
| 77 | + className="input input-bordered" |
| 78 | + value={search} |
| 79 | + onChange={(e) => setSearch(e.target.value)} |
| 80 | + /> |
| 81 | + <label className="label cursor-pointer"> |
| 82 | + <span className="label-text">Include Testnets</span> |
| 83 | + <input |
| 84 | + type="checkbox" |
| 85 | + className="checkbox checkbox-primary" |
| 86 | + checked={includeTestnets} |
| 87 | + onChange={(e) => setIncludeTestnets(e.target.checked)} |
| 88 | + /> |
| 89 | + </label> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + |
| 93 | + <div className="overflow-x-auto"> |
| 94 | + <table className="table w-full table-zebra"> |
| 95 | + <thead> |
| 96 | + <tr> |
| 97 | + <th>Chain</th> |
| 98 | + <th>Network</th> |
| 99 | + <th>ChainID</th> |
| 100 | + <th>RPC URL</th> |
| 101 | + </tr> |
| 102 | + </thead> |
| 103 | + <tbody> |
| 104 | + {filtered.map((n, idx) => ( |
| 105 | + <tr key={idx}> |
| 106 | + <td>{n.chain}</td> |
| 107 | + <td>{n.network}</td> |
| 108 | + <td>{n.chainId ?? ""}</td> |
| 109 | + <td>{`${BASE_URL}${n.rpcPath}`}</td> |
| 110 | + </tr> |
| 111 | + ))} |
| 112 | + </tbody> |
| 113 | + </table> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +} |
0 commit comments