Skip to content

Commit

Permalink
add blockexplorer input in add chain form (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Jul 16, 2024
1 parent 2bc9c56 commit d6025b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export const AddCustomChainModal = forwardRef<HTMLDialogElement, AddCustomChainM
</label>
<input type="text" name="rpcUrl" className="input input-bordered bg-neutral" required />
</div>
<div className="form-control">
<label className="label">
<span className="label-text">Block explorer link (optional)</span>
</label>
<input type="text" name="blockExplorer" className="input input-bordered bg-neutral" />
</div>
<div className="form-control flex-row mt-4 items-center gap-4">
<label className="label">
<span className="label-text">Testnet?</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { useTheme } from "next-themes";
import Select, { MultiValue, SingleValue } from "react-select";
import { Chain } from "viem";
import { mainnet } from "viem/chains";
import { AddCustomChainModal, CustomOption, OtherChainsModal } from "~~/components/NetworksDropdown";
import { useGlobalState } from "~~/services/store/store";

Expand All @@ -23,9 +24,10 @@ export const NetworksDropdown = ({ onChange }: { onChange: (options: any) => any
const [groupedOptionsState, setGroupedOptionsState] = useState(initialGroupedOptions);
const [selectedOption, setSelectedOption] = useState<SingleValue<Options>>(initialGroupedOptions.mainnet.options[0]);

const { addCustomChain, removeChain } = useGlobalState(state => ({
const { addCustomChain, removeChain, resetTargetNetwork } = useGlobalState(state => ({
addCustomChain: state.addChain,
removeChain: state.removeChain,
resetTargetNetwork: () => state.setTargetNetwork(mainnet),
}));

const seeOtherChainsModalRef = useRef<HTMLDialogElement>(null);
Expand Down Expand Up @@ -108,6 +110,7 @@ export const NetworksDropdown = ({ onChange }: { onChange: (options: any) => any

removeChain(chainId);
removeChainFromLocalStorage(chainId);
resetTargetNetwork();

const newGroupedOptions = { ...groupedOptionsState };
const groupName = option.testnet ? "testnet" : "mainnet";
Expand Down
10 changes: 10 additions & 0 deletions packages/nextjs/components/NetworksDropdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ export const removeChainFromLocalStorage = (chainId: number) => {
};

export const formDataToChain = (formData: FormData): Chain => {
const blockExplorers: Chain["blockExplorers"] | undefined = Boolean(formData.get("blockExplorer"))
? {
default: {
name: "Block Explorer",
url: formData.get("blockExplorer") as string,
},
}
: undefined;

const chain = {
id: Number(formData.get("id")),
name: formData.get("name") as string,
Expand All @@ -146,6 +155,7 @@ export const formDataToChain = (formData: FormData): Chain => {
default: { http: [formData.get("rpcUrl") as string] },
},
testnet: formData.get("testnet") === "on",
blockExplorers,
} as const satisfies Chain;

return chain;
Expand Down

0 comments on commit d6025b0

Please sign in to comment.