Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch network instead of disconnect #929

Merged
merged 5 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions components/UI/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { FaDiscord, FaGithub, FaTwitter } from "react-icons/fa";
import styles from "../../styles/components/navbar.module.css";
import connectStyles from "../../styles/components/walletConnect.module.css";
import Button from "./button";
import { useConnect, useAccount, useDisconnect } from "@starknet-react/core";
import {
useConnect,
useAccount,
useDisconnect,
useSwitchChain,
} from "@starknet-react/core";
import ModalMessage from "./modalMessage";
import { useDisplayName } from "../../hooks/displayName.tsx";
import { useMediaQuery } from "@mui/material";
Expand All @@ -21,12 +26,13 @@ import ProfilFilledIcon from "./iconsComponents/icons/profilFilledIcon";
import DesktopNav from "./desktopNav";
import CloseFilledIcon from "./iconsComponents/icons/closeFilledIcon";
import { StarknetIdJsContext } from "../../context/StarknetIdJsProvider";
import { StarkProfile } from "starknetid.js";
import { StarknetChainId, StarkProfile } from "starknetid.js";
import { Connector } from "starknetkit";
import {
getConnectorIcon,
getLastConnected,
getLastConnector,
supportSwitchNetwork,
} from "@/utils/connectorWrapper";
import WalletConnect from "./walletConnect";
import ArrowDownIcon from "./iconsComponents/icons/arrowDownIcon";
Expand All @@ -40,7 +46,7 @@ const Navbar: FunctionComponent = () => {
const [desktopNav, setDesktopNav] = useState<boolean>(false);
const { address } = useAccount();
const [isConnected, setIsConnected] = useState<boolean>(false);
const { connectAsync, connectors } = useConnect();
const { connectAsync, connectors, connector } = useConnect();
const { disconnect } = useDisconnect();
const isMobile = useMediaQuery("(max-width:425px)");
const domainOrAddress = useDisplayName(address ?? "", isMobile);
Expand All @@ -54,6 +60,14 @@ const Navbar: FunctionComponent = () => {
const [showWalletConnectModal, setShowWalletConnectModal] =
useState<boolean>(false);
const router = useRouter();
const { switchChainAsync } = useSwitchChain({
params: {
chainId:
network === "testnet"
? StarknetChainId.SN_SEPOLIA
: StarknetChainId.SN_MAIN,
},
});
fricoben marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
const pageName = router.pathname.split("/")[1];
Expand Down Expand Up @@ -132,6 +146,15 @@ const Navbar: FunctionComponent = () => {
return textToReturn;
}

const switchNetwork = async () => {
if (supportSwitchNetwork(connector)) {
const res = await switchChainAsync();
if (res) setIsWrongNetwork(false);
} else {
disconnectByClick();
}
};

return (
<>
<div className={"fixed w-full z-20 bg-background top-0"}>
Expand Down Expand Up @@ -372,8 +395,10 @@ const Navbar: FunctionComponent = () => {
network to be able use it.
</p>
<div className="mt-5">
<Button onClick={() => disconnectByClick()}>
{`Disconnect`}
<Button onClick={() => switchNetwork()}>
{supportSwitchNetwork(connector)
? `Switch to ${network}`
: "Disconnect"}
</Button>
</div>
</div>
Expand Down
10 changes: 8 additions & 2 deletions hooks/isWrongNetwork.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { mainnet } from "@starknet-react/chains";
import { bigintToStringHex } from "@/utils/stringService";
import { sepolia } from "@starknet-react/chains";
import { useNetwork } from "@starknet-react/core";
import { useAccount, useNetwork } from "@starknet-react/core";
import { useEffect, useState } from "react";

const useIsWrongNetwork = () => {
const { chain } = useNetwork();
const { account } = useAccount();
const [isWrongNetwork, setIsWrongNetwork] = useState<boolean>(false);

useEffect(() => {
if (!account) {
setIsWrongNetwork(false);
return;
}

setIsWrongNetwork(
process.env.NEXT_PUBLIC_IS_TESTNET === "true"
? bigintToStringHex(chain.id) === bigintToStringHex(mainnet.id)
: bigintToStringHex(chain.id) === bigintToStringHex(sepolia.id)
);
}, [chain]);
}, [account, chain]);

return {
isWrongNetwork,
Expand Down
Loading