From 603627b87cf90f07dedbee347ac7a3a463edbfca Mon Sep 17 00:00:00 2001 From: bigint <69431456+bigint@users.noreply.github.com> Date: Wed, 7 Feb 2024 11:32:53 +0530 Subject: [PATCH 1/2] fix: network switch, another great ux improvement --- .../components/Composer/NewPublication.tsx | 7 +---- .../Publication/Actions/Share/Mirror.tsx | 5 +--- .../CollectModule/CollectAction.tsx | 5 +--- .../Settings/Account/SuperFollow.tsx | 5 +--- .../components/Settings/Allowance/Button.tsx | 5 +--- .../src/components/Settings/Danger/Delete.tsx | 7 ++--- .../components/Settings/Danger/Guardian.tsx | 7 ++--- .../Settings/Handles/LinkHandle.tsx | 5 +--- .../Settings/Handles/UnlinkHandle.tsx | 5 +--- .../Manager/LensManager/ToggleLensManager.tsx | 7 ++--- .../ProfileManager/AddProfileManager.tsx | 6 ++-- .../Manager/ProfileManager/Managers/List.tsx | 9 ++---- .../components/Settings/Profile/Profile.tsx | 13 +-------- .../Shared/Alert/BlockOrUnBlockProfile.tsx | 7 ++--- .../components/Shared/Auth/WrongNetwork.tsx | 29 ------------------- .../Shared/GlobalBanners/ProtectProfile.tsx | 7 ++--- .../src/components/Shared/GlobalModals.tsx | 14 --------- .../src/components/Shared/Profile/Follow.tsx | 5 +--- .../components/Shared/Profile/Unfollow.tsx | 5 +--- .../Shared/SuperFollow/FollowModule.tsx | 5 +--- .../src/hooks/useActOnUnknownOpenAction.tsx | 4 +++ apps/web/src/hooks/useCreatePublication.tsx | 4 +++ apps/web/src/hooks/useHandleWrongNetwork.ts | 19 +++++------- .../non-persisted/useGlobalModalStateStore.ts | 5 ---- 24 files changed, 41 insertions(+), 149 deletions(-) delete mode 100644 apps/web/src/components/Shared/Auth/WrongNetwork.tsx diff --git a/apps/web/src/components/Composer/NewPublication.tsx b/apps/web/src/components/Composer/NewPublication.tsx index c66977c52bf..4f1cb182beb 100644 --- a/apps/web/src/components/Composer/NewPublication.tsx +++ b/apps/web/src/components/Composer/NewPublication.tsx @@ -44,7 +44,6 @@ import { useState } from 'react'; import toast from 'react-hot-toast'; import useCreatePoll from 'src/hooks/useCreatePoll'; import useCreatePublication from 'src/hooks/useCreatePublication'; -import useHandleWrongNetwork from 'src/hooks/useHandleWrongNetwork'; import usePublicationMetadata from 'src/hooks/usePublicationMetadata'; import { useCollectModuleStore } from 'src/store/non-persisted/publication/useCollectModuleStore'; import { useOpenActionStore } from 'src/store/non-persisted/publication/useOpenActionStore'; @@ -180,6 +179,7 @@ const NewPublication: FC = ({ publication }) => { const resetLiveVideoConfig = usePublicationLiveStore( (state) => state.resetLiveVideoConfig ); + const setLicense = usePublicationLicenseStore((state) => state.setLicense); // Collect module store @@ -207,7 +207,6 @@ const NewPublication: FC = ({ publication }) => { const [editor] = useLexicalComposerContext(); const createPoll = useCreatePoll(); const getMetadata = usePublicationMetadata(); - const handleWrongNetwork = useHandleWrongNetwork(); const { canUseLensManager, isSponsored } = checkDispatcherPermissions(currentProfile); @@ -354,10 +353,6 @@ const NewPublication: FC = ({ publication }) => { toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - if (isComment && publication.momoka?.proof && !isSponsored) { return toast.error( 'Momoka is currently in beta - during this time certain actions are not available to all profiles.' diff --git a/apps/web/src/components/Publication/Actions/Share/Mirror.tsx b/apps/web/src/components/Publication/Actions/Share/Mirror.tsx index 72eb0c5df3e..2d2f6ae4850 100644 --- a/apps/web/src/components/Publication/Actions/Share/Mirror.tsx +++ b/apps/web/src/components/Publication/Actions/Share/Mirror.tsx @@ -146,6 +146,7 @@ const Mirror: FC = ({ isLoading, publication, setIsLoading }) => { isMomokaPublication = false ) => { const { id, typedData } = generatedData; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -227,10 +228,6 @@ const Mirror: FC = ({ isLoading, publication, setIsLoading }) => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - if (publication.momoka?.proof && !isSponsored) { return toast.error( 'Momoka is currently in beta - during this time certain actions are not available to all profiles.' diff --git a/apps/web/src/components/Publication/OpenAction/CollectModule/CollectAction.tsx b/apps/web/src/components/Publication/OpenAction/CollectModule/CollectAction.tsx index f57df6bad5d..56cd2d0f06d 100644 --- a/apps/web/src/components/Publication/OpenAction/CollectModule/CollectAction.tsx +++ b/apps/web/src/components/Publication/OpenAction/CollectModule/CollectAction.tsx @@ -250,6 +250,7 @@ const CollectAction: FC = ({ const typedDataGenerator = async (generatedData: any) => { const { id, typedData } = generatedData; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -335,10 +336,6 @@ const CollectAction: FC = ({ return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); if (isLegacyCollectModule) { diff --git a/apps/web/src/components/Settings/Account/SuperFollow.tsx b/apps/web/src/components/Settings/Account/SuperFollow.tsx index bd1d2dd9797..f815420c5ed 100644 --- a/apps/web/src/components/Settings/Account/SuperFollow.tsx +++ b/apps/web/src/components/Settings/Account/SuperFollow.tsx @@ -128,6 +128,7 @@ const SuperFollow: FC = () => { const { followModule, followModuleInitData, profileId } = typedData.value; const args = [profileId, followModule, followModuleInitData]; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -158,10 +159,6 @@ const SuperFollow: FC = () => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); return await createSetFollowModuleTypedData({ diff --git a/apps/web/src/components/Settings/Allowance/Button.tsx b/apps/web/src/components/Settings/Allowance/Button.tsx index 29531c863ae..c1da902f91d 100644 --- a/apps/web/src/components/Settings/Allowance/Button.tsx +++ b/apps/web/src/components/Settings/Allowance/Button.tsx @@ -85,10 +85,6 @@ const AllowanceButton: FC = ({ value: string, selectedModule: string ) => { - if (handleWrongNetwork()) { - return; - } - try { const isUnknownModule = module.moduleName === OpenActionModuleType.UnknownOpenActionModule; @@ -107,6 +103,7 @@ const AllowanceButton: FC = ({ } } }); + await handleWrongNetwork(); return sendTransaction?.({ account: data?.generateModuleCurrencyApprovalData.from, diff --git a/apps/web/src/components/Settings/Danger/Delete.tsx b/apps/web/src/components/Settings/Danger/Delete.tsx index 482d3e920a4..e7f5480b118 100644 --- a/apps/web/src/components/Settings/Danger/Delete.tsx +++ b/apps/web/src/components/Settings/Danger/Delete.tsx @@ -52,17 +52,14 @@ const DeleteSettings: FC = () => { }); }; - const handleDelete = () => { + const handleDelete = async () => { if (!currentProfile) { return toast.error(Errors.SignWallet); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); + await handleWrongNetwork(); return write({ args: [currentProfile?.id] }); } catch (error) { onError(error); diff --git a/apps/web/src/components/Settings/Danger/Guardian.tsx b/apps/web/src/components/Settings/Danger/Guardian.tsx index b3dd57d4337..4a1b2823c9d 100644 --- a/apps/web/src/components/Settings/Danger/Guardian.tsx +++ b/apps/web/src/components/Settings/Danger/Guardian.tsx @@ -48,17 +48,14 @@ const GuardianSettings: FC = () => { }); }; - const handleDisable = () => { + const handleDisable = async () => { if (!currentProfile) { return toast.error(Errors.SignWallet); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); + await handleWrongNetwork(); return write(); } catch (error) { onError(error); diff --git a/apps/web/src/components/Settings/Handles/LinkHandle.tsx b/apps/web/src/components/Settings/Handles/LinkHandle.tsx index 423c7673ac0..fd4064e268d 100644 --- a/apps/web/src/components/Settings/Handles/LinkHandle.tsx +++ b/apps/web/src/components/Settings/Handles/LinkHandle.tsx @@ -98,6 +98,7 @@ const LinkHandle: FC = () => { onCompleted: async ({ createLinkHandleToProfileTypedData }) => { const { id, typedData } = createLinkHandleToProfileTypedData; const signature = await signTypedDataAsync(getSignature(typedData)); + await handleWrongNetwork(); setLensHubOnchainSigNonce(lensHubOnchainSigNonce + 1); if (canBroadcast) { @@ -145,10 +146,6 @@ const LinkHandle: FC = () => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - const confirmation = confirm('Are you sure you want to link this handle?'); if (!confirmation) { diff --git a/apps/web/src/components/Settings/Handles/UnlinkHandle.tsx b/apps/web/src/components/Settings/Handles/UnlinkHandle.tsx index e624f262b1b..73e9541e258 100644 --- a/apps/web/src/components/Settings/Handles/UnlinkHandle.tsx +++ b/apps/web/src/components/Settings/Handles/UnlinkHandle.tsx @@ -85,6 +85,7 @@ const UnlinkHandle: FC = () => { useCreateUnlinkHandleFromProfileTypedDataMutation({ onCompleted: async ({ createUnlinkHandleFromProfileTypedData }) => { const { id, typedData } = createUnlinkHandleFromProfileTypedData; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -135,10 +136,6 @@ const UnlinkHandle: FC = () => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setUnlinking(true); const request: UnlinkHandleFromProfileRequest = { diff --git a/apps/web/src/components/Settings/Manager/LensManager/ToggleLensManager.tsx b/apps/web/src/components/Settings/Manager/LensManager/ToggleLensManager.tsx index 11eb33a1c72..89637628639 100644 --- a/apps/web/src/components/Settings/Manager/LensManager/ToggleLensManager.tsx +++ b/apps/web/src/components/Settings/Manager/LensManager/ToggleLensManager.tsx @@ -94,7 +94,6 @@ const ToggleLensManager: FC = ({ useCreateChangeProfileManagersTypedDataMutation({ onCompleted: async ({ createChangeProfileManagersTypedData }) => { const { id, typedData } = createChangeProfileManagersTypedData; - const signature = await signTypedDataAsync(getSignature(typedData)); const { approvals, configNumber, @@ -109,8 +108,10 @@ const ToggleLensManager: FC = ({ configNumber, switchToGivenConfig ]; + await handleWrongNetwork(); if (!isTba && canBroadcast) { + const signature = await signTypedDataAsync(getSignature(typedData)); const { data } = await broadcastOnchain({ variables: { request: { id, signature } } }); @@ -130,10 +131,6 @@ const ToggleLensManager: FC = ({ return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); return await createChangeProfileManagersTypedData({ diff --git a/apps/web/src/components/Settings/Manager/ProfileManager/AddProfileManager.tsx b/apps/web/src/components/Settings/Manager/ProfileManager/AddProfileManager.tsx index fa203fd4c11..0b951cbc44b 100644 --- a/apps/web/src/components/Settings/Manager/ProfileManager/AddProfileManager.tsx +++ b/apps/web/src/components/Settings/Manager/ProfileManager/AddProfileManager.tsx @@ -111,6 +111,8 @@ const AddProfileManager: FC = ({ configNumber, switchToGivenConfig ]; + await handleWrongNetwork(); + try { if (!isTba && canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -144,10 +146,6 @@ const AddProfileManager: FC = ({ return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); return await createChangeProfileManagersTypedData({ diff --git a/apps/web/src/components/Settings/Manager/ProfileManager/Managers/List.tsx b/apps/web/src/components/Settings/Manager/ProfileManager/Managers/List.tsx index 332b25256c4..d20a510578a 100644 --- a/apps/web/src/components/Settings/Manager/ProfileManager/Managers/List.tsx +++ b/apps/web/src/components/Settings/Manager/ProfileManager/Managers/List.tsx @@ -92,8 +92,6 @@ const List: FC = () => { useCreateChangeProfileManagersTypedDataMutation({ onCompleted: async ({ createChangeProfileManagersTypedData }) => { const { id, typedData } = createChangeProfileManagersTypedData; - const signature = await signTypedDataAsync(getSignature(typedData)); - setLensHubOnchainSigNonce(lensHubOnchainSigNonce + 1); const { approvals, configNumber, @@ -108,8 +106,11 @@ const List: FC = () => { configNumber, switchToGivenConfig ]; + await handleWrongNetwork(); + setLensHubOnchainSigNonce(lensHubOnchainSigNonce + 1); if (canBroadcast) { + const signature = await signTypedDataAsync(getSignature(typedData)); const { data } = await broadcastOnchain({ variables: { request: { id, signature } } }); @@ -129,10 +130,6 @@ const List: FC = () => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setRemovingAddress(address); return await createChangeProfileManagersTypedData({ diff --git a/apps/web/src/components/Settings/Profile/Profile.tsx b/apps/web/src/components/Settings/Profile/Profile.tsx index 77d5d107c62..68a6ce273fe 100644 --- a/apps/web/src/components/Settings/Profile/Profile.tsx +++ b/apps/web/src/components/Settings/Profile/Profile.tsx @@ -165,6 +165,7 @@ const ProfileSettingsForm: FC = () => { onCompleted: async ({ createOnchainSetProfileMetadataTypedData }) => { const { id, typedData } = createOnchainSetProfileMetadataTypedData; const { metadataURI, profileId } = typedData.value; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -231,10 +232,6 @@ const ProfileSettingsForm: FC = () => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); const otherAttributes = @@ -300,14 +297,6 @@ const ProfileSettingsForm: FC = () => { }; const uploadAndSave = async (type: 'avatar' | 'cover') => { - if (!currentProfile) { - return toast.error(Errors.SignWallet); - } - - if (handleWrongNetwork()) { - return; - } - try { const croppedImage = await getCroppedImg( type === 'avatar' ? profilePictureSrc : coverPictureSrc, diff --git a/apps/web/src/components/Shared/Alert/BlockOrUnBlockProfile.tsx b/apps/web/src/components/Shared/Alert/BlockOrUnBlockProfile.tsx index de19c0f84d3..787f5705c91 100644 --- a/apps/web/src/components/Shared/Alert/BlockOrUnBlockProfile.tsx +++ b/apps/web/src/components/Shared/Alert/BlockOrUnBlockProfile.tsx @@ -111,10 +111,11 @@ const BlockOrUnBlockProfile: FC = () => { const typedDataGenerator = async (generatedData: any) => { const { id, typedData } = generatedData; - const signature = await signTypedDataAsync(getSignature(typedData)); + await handleWrongNetwork(); setLensHubOnchainSigNonce(lensHubOnchainSigNonce + 1); if (canBroadcast) { + const signature = await signTypedDataAsync(getSignature(typedData)); const { data } = await broadcastOnchain({ variables: { request: { id, signature } } }); @@ -176,10 +177,6 @@ const BlockOrUnBlockProfile: FC = () => { return; } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); const request: BlockRequest | UnblockRequest = { diff --git a/apps/web/src/components/Shared/Auth/WrongNetwork.tsx b/apps/web/src/components/Shared/Auth/WrongNetwork.tsx deleted file mode 100644 index 93b65de8cd6..00000000000 --- a/apps/web/src/components/Shared/Auth/WrongNetwork.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import type { FC } from 'react'; - -import { CHAIN } from 'src/constants'; -import { useGlobalModalStateStore } from 'src/store/non-persisted/useGlobalModalStateStore'; - -import SwitchNetwork from '../SwitchNetwork'; - -const WrongNetwork: FC = () => { - const setShowWrongNetworkModal = useGlobalModalStateStore( - (state) => state.setShowWrongNetworkModal - ); - - return ( -
-
-
Change network.
-
- Connect to the correct network to continue -
-
- setShowWrongNetworkModal(false)} - toChainId={CHAIN.id} - /> -
- ); -}; - -export default WrongNetwork; diff --git a/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx b/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx index 417ead1c122..5a7c962cccc 100644 --- a/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx +++ b/apps/web/src/components/Shared/GlobalBanners/ProtectProfile.tsx @@ -60,16 +60,13 @@ const ProtectProfile: FC = () => { ).toISOString(); const isCoolOffPassed = new Date(coolOffDate).getTime() < Date.now(); - const handleProtect = () => { + const handleProtect = async () => { if (!currentProfile) { return toast.error(Errors.SignWallet); } - if (handleWrongNetwork()) { - return; - } - try { + await handleWrongNetwork(); return write(); } catch (error) { onError(error); diff --git a/apps/web/src/components/Shared/GlobalModals.tsx b/apps/web/src/components/Shared/GlobalModals.tsx index feb798e2af7..53ae0bab39a 100644 --- a/apps/web/src/components/Shared/GlobalModals.tsx +++ b/apps/web/src/components/Shared/GlobalModals.tsx @@ -18,7 +18,6 @@ import { useGlobalModalStateStore } from 'src/store/non-persisted/useGlobalModal import Auth from './Auth'; import { useSignupStore } from './Auth/Signup'; -import WrongNetwork from './Auth/WrongNetwork'; import Invites from './Modal/Invites'; import ReportProfile from './Modal/ReportProfile'; import Views from './Modal/Views'; @@ -56,12 +55,6 @@ const GlobalModals: FC = () => { const setShowAuthModal = useGlobalModalStateStore( (state) => state.setShowAuthModal ); - const showWrongNetworkModal = useGlobalModalStateStore( - (state) => state.showWrongNetworkModal - ); - const setShowWrongNetworkModal = useGlobalModalStateStore( - (state) => state.setShowWrongNetworkModal - ); const showInvitesModal = useGlobalModalStateStore( (state) => state.showInvitesModal ); @@ -175,13 +168,6 @@ const GlobalModals: FC = () => { > - setShowWrongNetworkModal(false)} - show={showWrongNetworkModal} - title="Wrong Network" - > - - { if (checkIfPublicationNotDrafted()) { diff --git a/apps/web/src/components/Shared/Profile/Follow.tsx b/apps/web/src/components/Shared/Profile/Follow.tsx index b6b9fc8a540..0704ad49a30 100644 --- a/apps/web/src/components/Shared/Profile/Follow.tsx +++ b/apps/web/src/components/Shared/Profile/Follow.tsx @@ -117,6 +117,7 @@ const Follow: FC = ({ profile, showText = false }) => { followTokenIds, datas ]; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -159,10 +160,6 @@ const Follow: FC = ({ profile, showText = false }) => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); const request: FollowRequest = { follow: [{ profileId: profile?.id }] }; diff --git a/apps/web/src/components/Shared/Profile/Unfollow.tsx b/apps/web/src/components/Shared/Profile/Unfollow.tsx index cabdee82b9d..c4135430884 100644 --- a/apps/web/src/components/Shared/Profile/Unfollow.tsx +++ b/apps/web/src/components/Shared/Profile/Unfollow.tsx @@ -107,6 +107,7 @@ const Unfollow: FC = ({ profile, showText = false }) => { const { id, typedData } = createUnfollowTypedData; const { idsOfProfilesToUnfollow, unfollowerProfileId } = typedData.value; const args = [unfollowerProfileId, idsOfProfilesToUnfollow]; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -148,10 +149,6 @@ const Unfollow: FC = ({ profile, showText = false }) => { return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); const request: UnfollowRequest = { unfollow: [profile?.id] }; diff --git a/apps/web/src/components/Shared/SuperFollow/FollowModule.tsx b/apps/web/src/components/Shared/SuperFollow/FollowModule.tsx index 1c1ff0ab6a0..f78672df068 100644 --- a/apps/web/src/components/Shared/SuperFollow/FollowModule.tsx +++ b/apps/web/src/components/Shared/SuperFollow/FollowModule.tsx @@ -192,6 +192,7 @@ const FollowModule: FC = ({ followTokenIds, datas ]; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); @@ -219,10 +220,6 @@ const FollowModule: FC = ({ return toast.error(Errors.Suspended); } - if (handleWrongNetwork()) { - return; - } - try { setIsLoading(true); return await createFollowTypedData({ diff --git a/apps/web/src/hooks/useActOnUnknownOpenAction.tsx b/apps/web/src/hooks/useActOnUnknownOpenAction.tsx index 0c8c8981279..32fdda5a9af 100644 --- a/apps/web/src/hooks/useActOnUnknownOpenAction.tsx +++ b/apps/web/src/hooks/useActOnUnknownOpenAction.tsx @@ -17,6 +17,8 @@ import { useNonceStore } from 'src/store/non-persisted/useNonceStore'; import useProfileStore from 'src/store/persisted/useProfileStore'; import { useSignTypedData, useWriteContract } from 'wagmi'; +import useHandleWrongNetwork from './useHandleWrongNetwork'; + interface CreatePublicationProps { signlessApproved?: boolean; successToast?: string; @@ -34,6 +36,7 @@ const useActOnUnknownOpenAction = ({ (state) => state.setLensHubOnchainSigNonce ); const [isLoading, setIsLoading] = useState(false); + const handleWrongNetwork = useHandleWrongNetwork(); const { canBroadcast, canUseLensManager } = checkDispatcherPermissions(currentProfile); @@ -89,6 +92,7 @@ const useActOnUnknownOpenAction = ({ useCreateActOnOpenActionTypedDataMutation({ onCompleted: async ({ createActOnOpenActionTypedData }) => { const { id, typedData } = createActOnOpenActionTypedData; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); diff --git a/apps/web/src/hooks/useCreatePublication.tsx b/apps/web/src/hooks/useCreatePublication.tsx index 8f9a251c6a8..7ba426b27e2 100644 --- a/apps/web/src/hooks/useCreatePublication.tsx +++ b/apps/web/src/hooks/useCreatePublication.tsx @@ -39,6 +39,8 @@ import useProfileStore from 'src/store/persisted/useProfileStore'; import { useTransactionStore } from 'src/store/persisted/useTransactionStore'; import { useSignTypedData, useWriteContract } from 'wagmi'; +import useHandleWrongNetwork from './useHandleWrongNetwork'; + interface CreatePublicationProps { commentOn?: AnyPublication; onCompleted: (status?: any) => void; @@ -66,6 +68,7 @@ const useCreatePublication = ({ ); const txnQueue = useTransactionStore((state) => state.txnQueue); const setTxnQueue = useTransactionStore((state) => state.setTxnQueue); + const handleWrongNetwork = useHandleWrongNetwork(); const { canBroadcast } = checkDispatcherPermissions(currentProfile); const isComment = Boolean(commentOn); @@ -163,6 +166,7 @@ const useCreatePublication = ({ isMomokaPublication = false ) => { const { id, typedData } = generatedData; + await handleWrongNetwork(); if (canBroadcast) { const signature = await signTypedDataAsync(getSignature(typedData)); diff --git a/apps/web/src/hooks/useHandleWrongNetwork.ts b/apps/web/src/hooks/useHandleWrongNetwork.ts index ea881a12a9f..b6f82181057 100644 --- a/apps/web/src/hooks/useHandleWrongNetwork.ts +++ b/apps/web/src/hooks/useHandleWrongNetwork.ts @@ -1,22 +1,17 @@ -import { useCallback } from 'react'; import { CHAIN } from 'src/constants'; -import { useGlobalModalStateStore } from 'src/store/non-persisted/useGlobalModalStateStore'; -import { useChainId } from 'wagmi'; +import { useConnections, useSwitchChain } from 'wagmi'; const useHandleWrongNetwork = () => { - const setShowWrongNetworkModal = useGlobalModalStateStore( - (state) => state.setShowWrongNetworkModal - ); - const chain = useChainId(); + const activeConnection = useConnections(); + const { switchChainAsync } = useSwitchChain(); - const handleWrongNetwork = useCallback(() => { - if (chain !== CHAIN.id) { - setShowWrongNetworkModal(true); - return true; + const handleWrongNetwork = async () => { + if (activeConnection[0].chainId !== CHAIN.id) { + return await switchChainAsync({ chainId: CHAIN.id }); } return false; - }, [chain, setShowWrongNetworkModal]); + }; return handleWrongNetwork; }; diff --git a/apps/web/src/store/non-persisted/useGlobalModalStateStore.ts b/apps/web/src/store/non-persisted/useGlobalModalStateStore.ts index b4bb736efee..098ccbbf5e2 100644 --- a/apps/web/src/store/non-persisted/useGlobalModalStateStore.ts +++ b/apps/web/src/store/non-persisted/useGlobalModalStateStore.ts @@ -29,7 +29,6 @@ interface GlobalModalState { reportProfileModal: boolean, reportingProfile: null | Profile ) => void; - setShowWrongNetworkModal: (showWrongNetworkModal: boolean) => void; showAuthModal: boolean; showDiscardModal: boolean; showInvitesModal: boolean; @@ -39,7 +38,6 @@ interface GlobalModalState { showPublicationReportModal: boolean; showPublicationStatsModal: boolean; showReportProfileModal: boolean; - showWrongNetworkModal: boolean; statsPublicationId: null | string; } @@ -74,8 +72,6 @@ export const useGlobalModalStateStore = create((set) => ({ })), setShowReportProfileModal: (showReportProfileModal, reportingProfile) => set(() => ({ reportingProfile, showReportProfileModal })), - setShowWrongNetworkModal: (showWrongNetworkModal) => - set(() => ({ showWrongNetworkModal })), showAuthModal: false, showDiscardModal: false, showInvitesModal: false, @@ -85,6 +81,5 @@ export const useGlobalModalStateStore = create((set) => ({ showPublicationReportModal: false, showPublicationStatsModal: false, showReportProfileModal: false, - showWrongNetworkModal: false, statsPublicationId: null })); From a706155a6794ec5b7fa6ddbc2d1f47afe0911986 Mon Sep 17 00:00:00 2001 From: bigint <69431456+bigint@users.noreply.github.com> Date: Wed, 7 Feb 2024 11:48:18 +0530 Subject: [PATCH 2/2] fix: lint --- apps/web/src/components/Shared/SwitchNetwork.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/web/src/components/Shared/SwitchNetwork.tsx b/apps/web/src/components/Shared/SwitchNetwork.tsx index 88c0a921d39..401f2af519a 100644 --- a/apps/web/src/components/Shared/SwitchNetwork.tsx +++ b/apps/web/src/components/Shared/SwitchNetwork.tsx @@ -28,9 +28,7 @@ const SwitchNetwork: FC = ({ onClick={() => { onSwitch?.(); switchChain?.({ chainId: toChainId }); - Leafwatch.track(SYSTEM.SWITCH_NETWORK, { - chain: toChainId - }); + Leafwatch.track(SYSTEM.SWITCH_NETWORK, { chain: toChainId }); }} type="button" variant="danger"