mirror of
https://github.com/luxfhe/example-smart-wallet.git
synced 2026-10-30 18:13:16 +00:00
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import { useChain } from "@account-kit/react";
|
|
import { useEffect, useMemo } from "react";
|
|
import scaffoldConfig from "~~/scaffold.config";
|
|
import { useGlobalState } from "~~/services/store/store";
|
|
import { ChainWithAttributes } from "~~/utils/scaffold-eth";
|
|
import { NETWORKS_EXTRA_DATA } from "~~/utils/scaffold-eth";
|
|
|
|
/**
|
|
* Retrieves the connected wallet's network from scaffold.config or defaults to the 0th network in the list if the wallet is not connected.
|
|
*/
|
|
export function useTargetNetwork(): { targetNetwork: ChainWithAttributes } {
|
|
const { chain } = useChain();
|
|
const targetNetwork = useGlobalState(({ targetNetwork }) => targetNetwork);
|
|
const setTargetNetwork = useGlobalState(({ setTargetNetwork }) => setTargetNetwork);
|
|
|
|
useEffect(() => {
|
|
const newSelectedNetwork = scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chain?.id);
|
|
if (newSelectedNetwork && newSelectedNetwork.id !== targetNetwork.id) {
|
|
setTargetNetwork(newSelectedNetwork);
|
|
}
|
|
}, [chain?.id, setTargetNetwork, targetNetwork.id]);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
targetNetwork: {
|
|
...targetNetwork,
|
|
...NETWORKS_EXTRA_DATA[targetNetwork.id],
|
|
},
|
|
}),
|
|
[targetNetwork],
|
|
);
|
|
}
|