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 2 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
26 changes: 22 additions & 4 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,7 +26,7 @@ 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,
Expand Down Expand Up @@ -54,6 +59,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 +145,11 @@ const Navbar: FunctionComponent = () => {
return textToReturn;
}

const switchNetwork = async (network: string) => {
const res = await switchChainAsync();
if (res) setIsWrongNetwork(false);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve switchNetwork function implementation

The function has a few areas for improvement:

  1. The network parameter is unused
  2. Missing error handling for failed network switches
-  const switchNetwork = async (network: string) => {
+  const switchNetwork = async () => {
+    try {
       const res = await switchChainAsync();
       if (res) setIsWrongNetwork(false);
+    } catch (error) {
+      console.error('Failed to switch network:', error);
+      // Consider showing an error message to the user
+    }
   };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const switchNetwork = async (network: string) => {
const res = await switchChainAsync();
if (res) setIsWrongNetwork(false);
};
const switchNetwork = async () => {
try {
const res = await switchChainAsync();
if (res) setIsWrongNetwork(false);
} catch (error) {
console.error('Failed to switch network:', error);
// Consider showing an error message to the user
}
};


return (
<>
<div className={"fixed w-full z-20 bg-background top-0"}>
Expand Down Expand Up @@ -372,8 +390,8 @@ const Navbar: FunctionComponent = () => {
network to be able use it.
</p>
<div className="mt-5">
<Button onClick={() => disconnectByClick()}>
{`Disconnect`}
<Button onClick={() => switchNetwork(network)}>
{`Switch to ${network}`}
</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