generated from scaffold-eth/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
566 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
})} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.