Skip to content

Commit

Permalink
fix: initial chain
Browse files Browse the repository at this point in the history
  • Loading branch information
ngjupeng committed Oct 9, 2024
1 parent ad59b41 commit c5af2c0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/nextjs/components/ScaffoldAppWithProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const ScaffoldAppWithProviders = ({ children }: { children: React.ReactNo
if (lastSelectedChain == ChainType.Ethereum) {
if (lastEVMChain) {
setTargetNetwork(lastEVMChain);
setLastEVMChain(lastEVMChain);
}
}
setCurrentChain(lastSelectedChain);
Expand All @@ -78,6 +79,7 @@ export const ScaffoldAppWithProviders = ({ children }: { children: React.ReactNo
<RainbowKitProvider
avatar={BlockieAvatar}
theme={mounted ? (isDarkMode ? darkTheme() : lightTheme()) : lightTheme()}
initialChain={lastEVMChain ? lastEVMChain.id : undefined}
>
<ScaffoldCrossChainApp>{children}</ScaffoldCrossChainApp>
</RainbowKitProvider>
Expand Down
7 changes: 4 additions & 3 deletions packages/nextjs/components/Wallet/SelectNetworkModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default function SelectNetWorkModal() {
isError: isSwitchingError,
isSuccess: isSwitchingSuccess,
} = useSwitchChain();
const { isConnected } = useAccount();
const { currentChain } = useGlobalState(state => state);
const { isConnected: isEVMConnected } = useAccount();
const { currentChain, setLastEVMChain: setLastEVMChainGlobalState } = useGlobalState(state => state);
const setCurrentChain = useGlobalState(state => state.setCurrentChain);

const [open, setOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -92,11 +92,12 @@ export default function SelectNetWorkModal() {
const handleEthereumNetworkClick = (network: ChainWithAttributes) => {
setEthActiveNetwork(network);
setCurrentChain(ChainType.Ethereum);
if (isConnected) {
if (isEVMConnected) {
switchChain?.({ chainId: network.id });
}
setTargetNetwork(network);
setLastEVMChain(network);
setLastEVMChainGlobalState(network);
};

const handleStarknetClick = () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/nextjs/dynamic/services/store/global.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChainWithAttributes } from "@scaffold-eth-2/utils/scaffold-eth";
import { create } from "zustand";

/**
Expand All @@ -11,10 +12,14 @@ import { create } from "zustand";

export type GlobalState = {
currentChain: string;
lastEVMChain: ChainWithAttributes | null;
setCurrentChain: (newSelectedChain: string) => void;
setLastEVMChain: (newLastEVMChain: ChainWithAttributes) => void;
};

export const useGlobalState = create<GlobalState>((set: any) => ({
currentChain: "",
lastEVMChain: null,
setCurrentChain: (newSelectedChain: string): void => set(() => ({ currentChain: newSelectedChain })),
setLastEVMChain: (newLastEVMChain: ChainWithAttributes): void => set(() => ({ lastEVMChain: newLastEVMChain })),
}));

0 comments on commit c5af2c0

Please sign in to comment.