Skip to content

Commit

Permalink
fix: review issues (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Oct 6, 2023
1 parent 3d0f363 commit 796bd35
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/components/common/ConnectWallet/MPCLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const MPCLogin = ({ onLogin }: { onLogin?: () => void }) => {

return (
<>
{wallet && userInfo.email ? (
{wallet && userInfo ? (
<>
<Button
variant="outlined"
Expand Down
13 changes: 3 additions & 10 deletions src/components/common/ConnectWallet/MPCWalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useMPCWallet, MPCWalletState } from '@/hooks/wallets/mpc/useMPCWallet'
import { type UserInfo } from '@web3auth/mpc-core-kit'
import { createContext, type ReactElement } from 'react'

type MPCWalletContext = {
Expand All @@ -8,11 +9,7 @@ type MPCWalletContext = {
upsertPasswordBackup: (password: string) => Promise<void>
recoverFactorWithPassword: (password: string, storeDeviceFactor: boolean) => Promise<void>
walletState: MPCWalletState
userInfo: {
email: string | undefined
profileImage: string | undefined
name: string | undefined
}
userInfo: UserInfo | undefined
}

export const MpcWalletContext = createContext<MPCWalletContext>({
Expand All @@ -22,11 +19,7 @@ export const MpcWalletContext = createContext<MPCWalletContext>({
resetAccount: () => Promise.resolve(),
upsertPasswordBackup: () => Promise.resolve(),
recoverFactorWithPassword: () => Promise.resolve(),
userInfo: {
email: undefined,
profileImage: undefined,
name: undefined,
},
userInfo: undefined,
})

export const MpcWalletProvider = ({ children }: { children: ReactElement }) => {
Expand Down
21 changes: 5 additions & 16 deletions src/components/welcome/WelcomeLogin/WalletLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ const WalletLogin = ({ onLogin }: { onLogin?: () => void }) => {
const wallet = useWallet()
const connectWallet = useConnectWallet()

const connectedWalletInfo =
wallet !== null && wallet?.label !== ONBOARD_MPC_MODULE_LABEL
? {
address: wallet?.address,
label: wallet?.label,
icon: wallet?.icon,
}
: undefined

const [loginTriggered, setLoginTriggered] = useState(false)

const login = async () => {
Expand All @@ -32,23 +23,21 @@ const WalletLogin = ({ onLogin }: { onLogin?: () => void }) => {
}
}, [loginTriggered, onLogin, wallet])

if (connectedWalletInfo) {
if (wallet !== null && wallet?.label !== ONBOARD_MPC_MODULE_LABEL) {
return (
<Button variant="contained" sx={{ padding: '8px 16px' }} fullWidth onClick={onLogin}>
<Box width="100%" justifyContent="space-between" display="flex" flexDirection="row" alignItems="center" gap={1}>
<Box display="flex" flexDirection="column" alignItems="flex-start">
<Typography variant="subtitle2" fontWeight={700}>
Continue with {connectedWalletInfo.label}
Continue with {wallet.label}
</Typography>
{connectedWalletInfo.address && (
<EthHashInfo address={connectedWalletInfo.address} shortAddress avatarSize={16} />
)}
{wallet.address && <EthHashInfo address={wallet.address} shortAddress avatarSize={16} />}
</Box>
{connectedWalletInfo.icon && (
{wallet.icon && (
<img
width="24px"
height="24px"
src={`data:image/svg+xml;utf8,${encodeURIComponent(connectedWalletInfo.icon)}`}
src={`data:image/svg+xml;utf8,${encodeURIComponent(wallet.icon)}`}
alt="icon"
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/wallets/mpc/__tests__/useMPCWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('useMPCWallet', () => {
it('should have state NOT_INITIALIZED initially', () => {
const { result } = renderHook(() => useMPCWallet())
expect(result.current.walletState).toBe(MPCWalletState.NOT_INITIALIZED)
expect(result.current.userInfo.email).toBeUndefined()
expect(result.current.userInfo?.email).toBeUndefined()
})

describe('triggerLogin', () => {
Expand Down
22 changes: 7 additions & 15 deletions src/hooks/wallets/mpc/useMPCWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from 'react'
import useMPC from './useMPC'
import BN from 'bn.js'
import { GOOGLE_CLIENT_ID, WEB3AUTH_VERIFIER_ID } from '@/config/constants'
import { COREKIT_STATUS, getWebBrowserFactor } from '@web3auth/mpc-core-kit'
import { COREKIT_STATUS, getWebBrowserFactor, type UserInfo } from '@web3auth/mpc-core-kit'
import useOnboard, { connectWallet } from '../useOnboard'
import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/module'
import { SecurityQuestionRecovery } from './recovery/SecurityQuestionRecovery'
Expand All @@ -21,11 +21,7 @@ export type MPCWalletHook = {
walletState: MPCWalletState
triggerLogin: () => Promise<void>
resetAccount: () => Promise<void>
userInfo: {
email: string | undefined
profileImage: string | undefined
name: string | undefined
}
userInfo: UserInfo | undefined
}

export const useMPCWallet = (): MPCWalletHook => {
Expand Down Expand Up @@ -83,19 +79,19 @@ export const useMPCWallet = (): MPCWalletHook => {
}
}

finalizeLogin()
await finalizeLogin()
} catch (error) {
setWalletState(MPCWalletState.NOT_INITIALIZED)
console.error(error)
}
}

const finalizeLogin = () => {
const finalizeLogin = async () => {
if (!mpcCoreKit || !onboard) {
return
}
if (mpcCoreKit.status === COREKIT_STATUS.LOGGED_IN) {
connectWallet(onboard, {
await connectWallet(onboard, {
autoSelect: {
label: ONBOARD_MPC_MODULE_LABEL,
disableModals: true,
Expand All @@ -122,7 +118,7 @@ export const useMPCWallet = (): MPCWalletHook => {
await deviceShareRecovery.createAndStoreDeviceFactor()
}

finalizeLogin()
await finalizeLogin()
}
}

Expand All @@ -132,10 +128,6 @@ export const useMPCWallet = (): MPCWalletHook => {
recoverFactorWithPassword,
resetAccount: criticalResetAccount,
upsertPasswordBackup: () => Promise.resolve(),
userInfo: {
email: mpcCoreKit?.state.userInfo?.email,
profileImage: mpcCoreKit?.state.userInfo?.profileImage,
name: mpcCoreKit?.state.userInfo?.name,
},
userInfo: mpcCoreKit?.state.userInfo,
}
}

0 comments on commit 796bd35

Please sign in to comment.