import { useState, Fragment, useEffect } from "react"; import { CogIcon } from "@heroicons/react/solid"; import { ButtonModal, ButtonBorderGradient } from "../Buttons"; import { RPC_URL } from "../../settings/rpc"; import { Listbox, Transition } from "@headlessui/react"; import { CheckIcon, SelectorIcon, InformationCircleIcon, CheckCircleIcon, } from "@heroicons/react/solid"; import { useValidateRpc } from "../../hooks"; import { toast } from "react-toastify"; import clsx from "clsx"; interface IRpc { name: string; url: string; } const RPCS: IRpc[] = [ { name: "Bonfida", url: RPC_URL as string }, { name: "Serum", url: "https://solana-api.projectserum.com" }, { name: "Custom", url: "" }, ]; export const RpcSettings = ({ setCustomRpc, }: { setCustomRpc: (url: string) => void; }) => { const [selected, setSelected] = useState(RPCS[0]); const [input, setInput] = useState(""); const [custom, setCustom] = useState(false); const { data: validUrl, loading } = useValidateRpc( !!input ? input : selected.url ); const [visible, setVisible] = useState(false); useEffect(() => { const rpc = RPCS.find(({ url }) => input === url); if (!rpc) { setCustom(true); setSelected(RPCS[2]); } else { setCustom(false); setSelected(rpc); } }, [input, custom]); const handleOnChange = (e: IRpc) => { setSelected(e); setCustom(false); setInput(e.url); }; const handleSave = () => { setVisible(false); if (validUrl) { setCustomRpc(custom ? input : selected.url); toast.info(`RPC node updated 👌`); } }; return ( } modalClass="py-10" >

RPC Settings

{/* Dropdown */}
{selected.name} {RPCS.slice(0, 2).map((rpc, idx) => ( `cursor-default select-none relative py-2 pl-10 pr-4 ${ active ? "font-bold text-black bg-[#E4E9EE]" : "text-gray-900" }` } value={rpc} > {({ selected }) => ( <> {rpc.name} {selected ? ( ) : null} )} ))}
{/* Input */}
setInput(e.target.value.trim())} placeholder={selected.url} type="text" className="w-full h-full p-2 text-lg font-bold rounded-[5px] bg-neutral focus:outline-none" />
{!loading && !validUrl && ( <> Please enter a valid URL )} {!loading && validUrl && ( <> Valid RPC Node )} {loading && }
Save
); };