Files
js/components/selects/NetworkSelectorButton.tsx
T
Nacho IacovinoandManan Tank 20ebbb68ac Revamp publish flow (#1218)
* Get rid of proxy option

* Add network selector for publish

* Finish publish form

* Prettier pls

* Stop disabling deploy button

* Filter networks on deployment

* Update to dev

* Add checks for customFactory

* Add tabs

* Add custom factory flow

* Add networks fielset

* Fix prettier

* Update storage

* Update chains package

* Add resolution

* Fix build?

* Fix copy and default value

* Fix validation

* Add AbiSelector and update contractParams accordingly

* Fix deployment error

* Fix default values

* Fix reduce is not a fn

* Fix back button

* Fix reduce error

* Fix tabs, fix useEffect

* Updates

* Update sdks

* Add customFactoryAbi flow

* Fix autoFactory

* Fix autoFactory again

* Ternary city

* Fix default value

* Update lock

* Update versions

* test latest nightly

* Fix customFactoryAddresses not populating on edit

* Fix error on deploy

* Copy changes

* Remove unused import

* Copy changes

* Update copy

* Add advanced parameter field

* Move accordion outside platform fees

* Add advanced toggle on deploy

* Update packages

* Dev build test

* Update to nightly

---------

Co-authored-by: Manan Tank <[email protected]>
2023-06-03 00:28:27 +00:00

147 lines
4.4 KiB
TypeScript

import { CustomChainRenderer } from "./CustomChainRenderer";
import { popularChains } from "@3rdweb-sdk/react/components/popularChains";
import { useColorMode } from "@chakra-ui/react";
import { NetworkSelector, useChain, useWallet } from "@thirdweb-dev/react";
import { ChainIcon } from "components/icons/ChainIcon";
import { StoredChain } from "contexts/configured-chains";
import { useSupportedChains } from "hooks/chains/configureChains";
import {
useAddRecentlyUsedChainId,
useRecentlyUsedChains,
} from "hooks/chains/recentlyUsedChains";
import { useSetIsNetworkConfigModalOpen } from "hooks/networkConfigModal";
import { useEffect, useMemo, useRef, useState } from "react";
import { BiChevronDown } from "react-icons/bi";
import { Button } from "tw-components";
interface NetworkSelectorButtonProps {
disabledChainIds?: number[];
networksEnabled?: number[];
isDisabled?: boolean;
onSwitchChain?: (chain: StoredChain) => void;
}
export const NetworkSelectorButton: React.FC<NetworkSelectorButtonProps> = ({
disabledChainIds,
networksEnabled,
isDisabled,
onSwitchChain,
}) => {
const [showNetworkSelector, setShowNetworkSelector] = useState(false);
const recentlyUsedChains = useRecentlyUsedChains();
const addRecentlyUsedChains = useAddRecentlyUsedChainId();
const setIsNetworkConfigModalOpen = useSetIsNetworkConfigModalOpen();
const { colorMode } = useColorMode();
const supportedChains = useSupportedChains();
const chains = useMemo(() => {
if (disabledChainIds && disabledChainIds.length > 0) {
const disabledChainIdsSet = new Set(disabledChainIds);
return supportedChains.filter(
(chain) => !disabledChainIdsSet.has(chain.chainId),
);
}
if (networksEnabled && networksEnabled.length > 0) {
const networksEnabledSet = new Set(networksEnabled);
return supportedChains.filter((chain) =>
networksEnabledSet.has(chain.chainId),
);
}
}, [disabledChainIds, networksEnabled, supportedChains]);
const filteredRecentlyUsedChains = useMemo(() => {
if (recentlyUsedChains && recentlyUsedChains.length > 0) {
if (disabledChainIds && disabledChainIds.length > 0) {
const disabledChainIdsSet = new Set(disabledChainIds);
return recentlyUsedChains.filter(
(chain) => !disabledChainIdsSet.has(chain.chainId),
);
}
if (networksEnabled && networksEnabled.length > 0) {
const networksEnabledSet = new Set(networksEnabled);
return recentlyUsedChains.filter((chain) =>
networksEnabledSet.has(chain.chainId),
);
}
}
}, [recentlyUsedChains, disabledChainIds, networksEnabled]);
const chain = useChain();
const prevChain = useRef(chain);
// handle switch network done from wallet app/extension
useEffect(() => {
if (!chain) {
return;
}
if (prevChain.current?.chainId !== chain.chainId) {
if (onSwitchChain) {
onSwitchChain(chain);
}
addRecentlyUsedChains(chain.chainId);
prevChain.current = chain;
}
}, [chain, onSwitchChain, addRecentlyUsedChains]);
const wallet = useWallet();
return (
<>
<Button
isDisabled={isDisabled || !wallet}
display="flex"
bg="inputBg"
_hover={{
bg: "inputBgHover",
}}
width="100%"
variant="solid"
style={{
textAlign: "left",
justifyContent: "start",
alignItems: "center",
gap: "0.5rem",
}}
onClick={() => {
setShowNetworkSelector(true);
}}
leftIcon={<ChainIcon ipfsSrc={chain?.icon?.url} size={20} />}
>
{chain?.name || "Select Network"}
<BiChevronDown
style={{
marginLeft: "auto",
}}
/>
</Button>
{showNetworkSelector && (
<NetworkSelector
theme={colorMode}
chains={chains}
recentChains={filteredRecentlyUsedChains}
popularChains={networksEnabled ? undefined : popularChains}
renderChain={CustomChainRenderer}
onCustomClick={
networksEnabled
? undefined
: () => {
setIsNetworkConfigModalOpen(true);
}
}
onClose={() => {
setShowNetworkSelector(false);
}}
onSwitch={(_chain) => {
onSwitchChain?.(_chain);
addRecentlyUsedChains(_chain.chainId);
}}
/>
)}
</>
);
};