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 (