Skip to content

Commit

Permalink
Update network selector (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pabl0cks authored Feb 16, 2024
1 parent 0aff800 commit f4d3b60
Show file tree
Hide file tree
Showing 14 changed files with 566 additions and 15 deletions.
93 changes: 93 additions & 0 deletions packages/nextjs/components/NetworksDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { useEffect, useState } from "react";
import Image from "next/image";
import Select, { OptionProps, components } from "react-select";
import { getTargetNetworks } from "~~/utils/scaffold-eth";

type Options = {
value: number;
label: string;
icon?: string;
};
type GroupedOptions = Record<
"mainnet" | "testnet" | "localhost",
{
label: string;
options: Options[];
}
>;

const networks = getTargetNetworks();
const groupedOptions = networks.reduce<GroupedOptions>(
(groups, network) => {
// Handle the case for localhost
if (network.id === 31337) {
groups.localhost.options.push({
value: network.id,
label: network.name,
icon: network.icon,
});
return groups;
}

const groupName = network.testnet ? "testnet" : "mainnet";

groups[groupName].options.push({
value: network.id,
label: network.name,
icon: network.icon,
});

return groups;
},
{
mainnet: { label: "mainnet", options: [] },
testnet: { label: "testnet", options: [] },
localhost: { label: "localhost", options: [] },
},
);

const { Option } = components;
const IconOption = (props: OptionProps<Options>) => (
<Option {...props}>
<div className="flex items-center">
<Image src={props.data.icon || "/mainnet.svg"} alt={props.data.label} width={24} height={24} className="mr-2" />
{props.data.label}
</div>
</Option>
);

export const NetworksDropdown = ({ onChange }: { onChange: (options: any) => any }) => {
const [isMobile, setIsMobile] = useState(false);

useEffect(() => {
if (typeof window !== "undefined") {
const mediaQuery = window.matchMedia("(max-width: 640px)");
setIsMobile(mediaQuery.matches);

const handleResize = () => setIsMobile(mediaQuery.matches);
mediaQuery.addEventListener("change", handleResize);
return () => mediaQuery.removeEventListener("change", handleResize);
}
}, []);

return (
<Select
defaultValue={groupedOptions["mainnet"].options[0]}
instanceId="network-select"
options={Object.values(groupedOptions)}
onChange={onChange}
components={{ Option: IconOption }}
isSearchable={!isMobile}
className="text-sm w-44 max-w-xs bg-white relative"
theme={theme => ({
...theme,
colors: {
...theme.colors,
primary25: "#efeaff",
primary50: "#c1aeff",
primary: "#551d98",
},
})}
/>
);
};
1 change: 1 addition & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.0",
"react-select": "^5.8.0",
"use-debounce": "^8.0.4",
"usehooks-ts": "^2.7.2",
"viem": "1.19.9",
Expand Down
13 changes: 2 additions & 11 deletions packages/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Address, isAddress } from "viem";
import { usePublicClient } from "wagmi";
import { MetaHeader } from "~~/components/MetaHeader";
import { MiniFooter } from "~~/components/MiniFooter";
import { NetworksDropdown } from "~~/components/NetworksDropdown";
import { AddressInput, InputBase } from "~~/components/scaffold-eth";
import { useAbiNinjaState } from "~~/services/store/store";
import { fetchContractABIFromAnyABI, fetchContractABIFromEtherscan, parseAndCorrectJSON } from "~~/utils/abi";
Expand Down Expand Up @@ -141,17 +142,7 @@ const Home: NextPage = () => {
<h2 className="mb-0 text-5xl font-bold">ABI Ninja</h2>
<p className="">Interact with any contract on Ethereum</p>
<div className="my-4">
<select
className="select select-sm w-36 max-w-xs bg-slate-50"
value={network}
onChange={e => setNetwork(e.target.value)}
>
{networks.map(network => (
<option key={network.id} value={network.id}>
{network.name}
</option>
))}
</select>
<NetworksDropdown onChange={option => setNetwork(option ? option.value.toString() : "")} />
</div>

<div role="tablist" className="flex w-full border-b">
Expand Down
Binary file added packages/nextjs/public/arbitrum.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/nextjs/public/base.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/nextjs/public/gnosis.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/nextjs/public/hardhat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions packages/nextjs/public/mainnet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions packages/nextjs/public/optimism.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions packages/nextjs/public/polygon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/nextjs/public/scroll.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/nextjs/public/zkSync.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f4d3b60

Please sign in to comment.