Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: QR code not displayed on first attempt to connect via Wallet Connect on desktop / chromium browsers #2279

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,4 @@ const Template = (args: Props) => {

Default.args = {
onClose: () => undefined,
qrCodeUri: uriMock,
connecting: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,23 @@ import {
AccountConnectModalState,
LoginProvider,
} from './AccountConnectWalletModal.types';
import { LoginModalMobile } from './components/AccountConnectWalletModal.Mobile';
import { LoginModalSelect } from './components/AccountConnectWalletModal.Select';

export interface Props extends RegenModalProps {
state?: AccountConnectModalState;
wallets: LoginProvider[];
qrCodeUri?: string;
connecting: boolean;
}

const AccountConnectWalletModal = ({
open,
onClose,
state = 'select',
wallets,
qrCodeUri,
connecting,
}: Props): JSX.Element => {
const isSelectState = state === 'select';
const isMobileState = state === 'wallet-mobile';
return (
<Modal open={open} onClose={onClose}>
<Box>
{isSelectState && <LoginModalSelect wallets={wallets} />}
{isMobileState && (
<LoginModalMobile qrCodeUri={qrCodeUri} connecting={connecting} />
)}
</Box>
<Box>{isSelectState && <LoginModalSelect wallets={wallets} />}</Box>
</Modal>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ const LoginButton = ({ size = 'small' }: Props) => {
const styles = useLoginButtonStyles();
const { wallet } = useWallet();
const {
connecting,
isModalOpen,
modalState,
onModalClose,
qrCodeUri,
walletsUiConfig,
onButtonClick,
} = useLoginData();
Expand Down Expand Up @@ -59,8 +57,6 @@ const LoginButton = ({ size = 'small' }: Props) => {
onModalClose={onModalClose}
wallets={walletsUiConfig}
modalState={modalState}
qrCodeUri={qrCodeUri}
connecting={connecting}
/>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from 'react';
import { WalletRepo } from '@cosmos-kit/core';
import { useChain } from '@cosmos-kit/react-lite';

import { UseStateSetter } from 'types/react/use-state';
import { WalletContextType } from 'lib/wallet/wallet';
Expand All @@ -22,8 +23,8 @@ export const useConnectToWallet = ({
connect,
connectWalletConnect,
onModalClose,
setModalState,
}: Props): Response => {
const { openView } = useChain('regen');
const connectToWallet = useCallback(
async ({ walletType }: ConnectParams): Promise<void> => {
if (connect && walletType === WalletType.Keplr) {
Expand All @@ -34,11 +35,12 @@ export const useConnectToWallet = ({
connectWalletConnect &&
walletType === WalletType.WalletConnectKeplr
) {
setModalState('wallet-mobile');
onModalClose();
openView();
await connectWalletConnect(KEPLR_MOBILE, true);
}
},
[connect, connectWalletConnect, onModalClose, setModalState],
[connect, connectWalletConnect, onModalClose, openView],
);

return connectToWallet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { State, WalletStatus } from '@cosmos-kit/core';
import { useCallback, useMemo, useState } from 'react';
import { useManager } from '@cosmos-kit/react-lite';

import { useWallet } from 'lib/wallet/wallet';
Expand All @@ -14,32 +13,9 @@ export const useLoginData = () => {
const { wallet, connect } = useWallet();

const { walletRepos } = useManager();
const [qrState, setQRState] = useState<State>(State.Init); // state of QRCode

const current = walletRepos[0]?.current;
(current?.client as any)?.setActions?.({
qrUrl: {
state: setQRState,
},
});

const walletStatus = current?.walletStatus;
const message = current?.message;
const qrUrl = current?.qrUrl;

const [isModalOpen, setIsModalOpen] = useState(false);
const [modalState, setModalState] = useState<LoginModalState>('select');
const [connecting, setConnecting] = useState<boolean>(false);
const [qrCodeUri, setQrCodeUri] = useState<string | undefined>();

useEffect(() => {
if (isModalOpen) {
setConnecting(
walletStatus === WalletStatus.Connecting && qrState === State.Init,
);
setQrCodeUri(qrUrl?.data);
}
}, [isModalOpen, qrState, walletStatus, qrUrl?.data, message]);

const onButtonClick = useCallback(
(): void => setIsModalOpen(true),
Expand Down Expand Up @@ -72,8 +48,6 @@ export const useLoginData = () => {
isModalOpen,
walletsUiConfig,
modalState,
qrCodeUri,
connecting,
onButtonClick,
onModalClose,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Props = {
wallets: LoginProvider[];
modalState: LoginModalState;
qrCodeUri?: string;
connecting: boolean;
};

const LoginFlow = ({
Expand All @@ -31,7 +30,6 @@ const LoginFlow = ({
wallets,
modalState,
qrCodeUri,
connecting,
}: Props) => {
const {
isConfirmationModalOpen,
Expand Down Expand Up @@ -59,7 +57,6 @@ const LoginFlow = ({
}}
state={modalState}
qrCodeUri={qrCodeUri}
connecting={connecting}
/>
<EmailConfirmationModal
resendText={getResendCodeLabel({ resendTimeLeft })}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { LoginProvider } from './LoginModal.types';

type GetWalletsMockParams = {
onWalletConnectClick: () => void;
};

export const getWalletsMock = ({
onWalletConnectClick,
}: GetWalletsMockParams): LoginProvider[] => [
export const wallets: LoginProvider[] = [
{
name: 'Keplr Wallet',
description: 'Keplr Chrome Extension',
Expand All @@ -18,7 +12,7 @@ export const getWalletsMock = ({
name: 'WalletConnect',
description: 'Keplr Mobile',
imageUrl: '/wallets/walletconnect-keplr.png',
onClick: onWalletConnectClick,
onClick: () => void 0,
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { LoginModal, Props } from './LoginModal';
import {
getWalletsMock,
socialProvidersMock,
uriMock,
} from './LoginModal.mock';
import { socialProvidersMock, uriMock, wallets } from './LoginModal.mock';
import { LoginModalState } from './LoginModal.types';

const meta: Meta<typeof LoginModal> = {
Expand All @@ -32,13 +28,10 @@ const Template = (args: Props) => {
}, 1000);
};

const walletsConfig = getWalletsMock({
onWalletConnectClick: () => setModalState('wallet-mobile'),
});
return (
<LoginModal
{...args}
wallets={walletsConfig}
wallets={wallets}
socialProviders={socialProvidersMock}
state={modalState}
open={open}
Expand All @@ -50,7 +43,6 @@ const Template = (args: Props) => {
Default.args = {
onClose: () => undefined,
qrCodeUri: uriMock,
connecting: false,
};

Default.argTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface Props extends RegenModalProps {
wallets: LoginProvider[];
socialProviders: LoginProvider[];
qrCodeUri?: string;
connecting: boolean;
onEmailSubmit: (values: EmailFormSchemaType) => Promise<void>;
}

Expand All @@ -23,11 +22,9 @@ const LoginModal = ({
wallets,
socialProviders,
qrCodeUri,
connecting,
onEmailSubmit,
}: Props): JSX.Element => {
const isSelectState = state === 'select';
const isMobileState = state === 'wallet-mobile';
return (
<Modal open={open} onClose={onClose}>
<Box>
Expand All @@ -38,9 +35,6 @@ const LoginModal = ({
onEmailSubmit={onEmailSubmit}
/>
)}
{isMobileState && (
<LoginModalMobile qrCodeUri={qrCodeUri} connecting={connecting} />
)}
</Box>
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type LoginModalState = 'select' | 'wallet-mobile';
export type LoginModalState = 'select';

export type LoginProvider = {
name: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,74 @@
import { useEffect, useState } from 'react';
import { State, WalletModalProps, WalletStatus } from '@cosmos-kit/core';
import { Box } from '@mui/material';
import QRCode from 'qrcode.react';

import { Center } from 'web-components/src/components/box';
import { Loading } from 'web-components/src/components/loading';
import Modal from 'web-components/src/components/modal';
import { Title } from 'web-components/src/components/typography';

import { CONNECTING_LABEL, QR_CODE_LABEL } from './LoginModal.Mobile.constants';

export interface Props {
qrCodeUri?: string;
connecting: boolean;
}

const LoginModalMobile = ({ qrCodeUri, connecting }: Props): JSX.Element => {
const LoginModalMobile = ({
isOpen,
setOpen,
walletRepo,
}: WalletModalProps) => {
const onCloseModal = () => {
setOpen(false);
};
const [qrState, setQRState] = useState<State>(State.Init); // state of QRCode
const [qrMsg, setQRMsg] = useState<string>(''); // message of QRCode error
const [connecting, setConnecting] = useState<boolean>(false);

const current = walletRepo?.current;

(current?.client as any)?.setActions?.({
qrUrl: {
state: setQRState,
message: setQRMsg,
},
});

const walletStatus = current?.walletStatus;
const message = current?.message;

useEffect(() => {
if (isOpen) {
switch (walletStatus) {
case WalletStatus.Connecting:
if (qrState === State.Init) {
setConnecting(true);
} else {
setConnecting(false);
}
break;
}
}
}, [qrState, walletStatus, qrMsg, message, isOpen]);

const qrCodeUri = current?.qrUrl?.data;

return (
<Box sx={{ minHeight: 400 }}>
<Title variant="h5" sx={{ mb: 7.5, textAlign: 'center' }}>
{connecting && CONNECTING_LABEL}
{qrCodeUri && QR_CODE_LABEL}
</Title>
<Center sx={{ pt: 9, height: 340 }}>
{connecting && <Loading />}
{qrCodeUri && <QRCode size={300} value={qrCodeUri} />}
</Center>
</Box>
<Modal open={isOpen} onClose={onCloseModal}>
<Box sx={{ minHeight: 400 }}>
<Title variant="h5" sx={{ mb: 7.5, textAlign: 'center' }}>
{connecting ? CONNECTING_LABEL : qrCodeUri && QR_CODE_LABEL}
</Title>
<Center sx={{ pt: 9, height: 340 }}>
{connecting ? (
<Loading />
) : (
qrCodeUri && <QRCode size={300} value={qrCodeUri} />
)}
</Center>
</Box>
</Modal>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ const RegistryLayoutHeader: React.FC = () => {
: theme.palette.primary.light;

const {
connecting,
isModalOpen,
modalState,
onButtonClick,
onModalClose,
qrCodeUri,
walletsUiConfig,
} = useLoginData();

Expand Down Expand Up @@ -147,8 +145,6 @@ const RegistryLayoutHeader: React.FC = () => {
// so there's no wallet available on mobile and only Keplr on desktop
wallets={checkIsMobile() ? [] : [walletsUiConfig[0]]}
modalState={modalState}
qrCodeUri={qrCodeUri}
connecting={connecting}
/>
</>
);
Expand Down
Loading
Loading