Commit a67f0714 authored by tom's avatar tom

pass api error to the toast while signing in with wallet

parent d2682889
...@@ -48,12 +48,12 @@ export default function useWeb3Wallet({ source }: Params) { ...@@ -48,12 +48,12 @@ export default function useWeb3Wallet({ source }: Params) {
const isConnected = isClientLoaded && !isDisconnected && address !== undefined; const isConnected = isClientLoaded && !isDisconnected && address !== undefined;
return { return React.useMemo(() => ({
connect: handleConnect, connect: handleConnect,
disconnect: handleDisconnect, disconnect: handleDisconnect,
isOpen: isOpening || isOpen, isOpen: isOpening || isOpen,
isConnected, isConnected,
address, address,
openModal, openModal,
}; }), [ handleConnect, handleDisconnect, isOpen, isOpening, isConnected, address, openModal ]);
} }
import { useWeb3Modal } from '@web3modal/wagmi/react';
import React from 'react'; import React from 'react';
import { useSignMessage } from 'wagmi'; import { useSignMessage } from 'wagmi';
...@@ -8,9 +7,10 @@ import config from 'configs/app'; ...@@ -8,9 +7,10 @@ import config from 'configs/app';
import useApiFetch from 'lib/api/useApiFetch'; import useApiFetch from 'lib/api/useApiFetch';
import getErrorMessage from 'lib/errors/getErrorMessage'; import getErrorMessage from 'lib/errors/getErrorMessage';
import getErrorObj from 'lib/errors/getErrorObj'; import getErrorObj from 'lib/errors/getErrorObj';
import getErrorObjPayload from 'lib/errors/getErrorObjPayload';
import useToast from 'lib/hooks/useToast'; import useToast from 'lib/hooks/useToast';
import * as mixpanel from 'lib/mixpanel'; import type * as mixpanel from 'lib/mixpanel';
import useAccount from 'lib/web3/useAccount'; import useWeb3Wallet from 'lib/web3/useWallet';
interface Props { interface Props {
onSuccess?: ({ address, profile }: { address: string; profile: UserInfo }) => void; onSuccess?: ({ address, profile }: { address: string; profile: UserInfo }) => void;
...@@ -25,8 +25,7 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth }: P ...@@ -25,8 +25,7 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth }: P
const apiFetch = useApiFetch(); const apiFetch = useApiFetch();
const toast = useToast(); const toast = useToast();
const web3Modal = useWeb3Modal(); const web3Wallet = useWeb3Wallet({ source });
const { isConnected, address } = useAccount();
const { signMessageAsync } = useSignMessage(); const { signMessageAsync } = useSignMessage();
const proceedToAuth = React.useCallback(async(address: string) => { const proceedToAuth = React.useCallback(async(address: string) => {
...@@ -46,12 +45,13 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth }: P ...@@ -46,12 +45,13 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth }: P
onSuccess?.({ address, profile: response }); onSuccess?.({ address, profile: response });
} catch (error) { } catch (error) {
const errorObj = getErrorObj(error); const errorObj = getErrorObj(error);
const apiErrorMessage = getErrorObjPayload<{ message: string }>(error)?.message;
const shortMessage = errorObj && 'shortMessage' in errorObj && typeof errorObj.shortMessage === 'string' ? errorObj.shortMessage : undefined; const shortMessage = errorObj && 'shortMessage' in errorObj && typeof errorObj.shortMessage === 'string' ? errorObj.shortMessage : undefined;
onError?.(); onError?.();
toast({ toast({
status: 'error', status: 'error',
title: 'Error', title: 'Error',
description: shortMessage || getErrorMessage(error) || 'Something went wrong', description: apiErrorMessage || shortMessage || getErrorMessage(error) || 'Something went wrong',
}); });
} finally { } finally {
setIsPending(false); setIsPending(false);
...@@ -60,22 +60,20 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth }: P ...@@ -60,22 +60,20 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth }: P
const start = React.useCallback(() => { const start = React.useCallback(() => {
setIsPending(true); setIsPending(true);
if (address) { if (web3Wallet.address) {
proceedToAuth(address); proceedToAuth(web3Wallet.address);
} else { } else {
isConnectingWalletRef.current = true; isConnectingWalletRef.current = true;
web3Modal.open(); web3Wallet.openModal();
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: source, Status: 'Started' });
} }
}, [ address, proceedToAuth, source, web3Modal ]); }, [ proceedToAuth, web3Wallet ]);
React.useEffect(() => { React.useEffect(() => {
if (address && isConnectingWalletRef.current) { if (web3Wallet.address && isConnectingWalletRef.current) {
isConnectingWalletRef.current = false; isConnectingWalletRef.current = false;
proceedToAuth(address); proceedToAuth(web3Wallet.address);
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: source, Status: 'Connected' });
} }
}, [ address, isConnected, proceedToAuth, source ]); }, [ proceedToAuth, web3Wallet.address ]);
return React.useMemo(() => ({ start, isPending }), [ start, isPending ]); return React.useMemo(() => ({ start, isPending }), [ start, isPending ]);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment