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

Frontend send refactor #3089

Merged
merged 2 commits into from
Dec 4, 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
2 changes: 1 addition & 1 deletion frontends/web/src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface IAccount {
active: boolean;
watch: boolean;
coinCode: CoinCode;
coinUnit: string;
coinUnit: CoinUnit;
coinName: string;
code: AccountCode;
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,23 @@ import { TConfirmSendProps } from '@/routes/account/send/components/confirm/type
import { ConfirmingWaitDialog } from '@/routes/account/send/components/confirm/dialogs/confirm-wait-dialog';
import style from './confirm.module.css';

type TConfirmSend = { bb01Paired: boolean | undefined } & TConfirmSendProps;

type TFiatValueProps = {
baseCurrencyUnit: ConversionUnit;
amount: string
}

export const ConfirmSend = (props: TConfirmSendProps) => {
switch (props.device) {
case 'bitbox':
return (
<ConfirmingWaitDialog
{...props}
/>
);
case 'bitbox02':
return (
<BB02ConfirmSend
{...props}
/>
);
default:
return null;
}
export const ConfirmSend = (props: TConfirmSend) => {
return (props.bb01Paired !== undefined ? (
<ConfirmingWaitDialog
{...props}
/>
) : (
<BB02ConfirmSend
{...props}
/>
));
};

export const BB02ConfirmSend = ({
Expand All @@ -57,7 +52,7 @@ export const BB02ConfirmSend = ({
selectedUTXOs,
coinCode,
transactionDetails
}: Omit<TConfirmSendProps, 'device'>) => {
}: TConfirmSendProps) => {

const { t } = useTranslation();
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ConfirmingWaitDialog = ({
selectedUTXOs,
coinCode,
transactionDetails
}: Omit<TConfirmSendProps, 'device'>) => {
}: TConfirmSendProps) => {
const { t } = useTranslation();
const [signProgress, setSignProgress] = useState<TSignProgress>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import { ConversionUnit, CoinCode, FeeTargetCode, Fiat, IAmount } from '@/api/account';
import { TProductName } from '@/api/devices';

export type TransactionDetails = {
proposedAmount?: IAmount;
Expand All @@ -28,7 +27,6 @@ export type TransactionDetails = {
}

export type TConfirmSendProps = {
device: TProductName;
baseCurrencyUnit: ConversionUnit;
note: string;
hasSelectedUTXOs: boolean;
Expand Down
40 changes: 34 additions & 6 deletions frontends/web/src/routes/account/send/send-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2023 Shift Crypto AG
* Copyright 2024 Shift Crypto AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,11 +14,14 @@
* limitations under the License.
*/

import { useContext } from 'react';
import { useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { AccountCode, IAccount } from '@/api/account';
import { TDevices } from '@/api/devices';
import { hasMobileChannel, TDevices } from '@/api/devices';
import { getDeviceInfo } from '@/api/bitbox01';
import { RatesContext } from '@/contexts/RatesContext';
import { findAccount } from '@/routes/account/utils';
import { findAccount, isBitcoinBased } from '@/routes/account/utils';
import { alertUser } from '@/components/alert/Alert';
import { Send } from './send';

type TSendProps = {
Expand All @@ -29,14 +32,39 @@ type TSendProps = {
}

export const SendWrapper = ({ accounts, code, deviceIDs, devices }: TSendProps) => {
const { t } = useTranslation();
const { defaultCurrency } = useContext(RatesContext);

const [bb01Paired, setBB01Paired] = useState<boolean>();
const [noMobileChannelError, setNoMobileChannelError] = useState<boolean>();

const account = findAccount(accounts, code);

useEffect(() => {
const product = deviceIDs.length > 0 ? devices[deviceIDs[0]] : undefined;
if (account && product === 'bitbox') {
const fetchData = async () => {
try {
const mobileChannel = await hasMobileChannel(deviceIDs[0])();
const { pairing } = await getDeviceInfo(deviceIDs[0]);
setBB01Paired(mobileChannel && pairing);
setNoMobileChannelError(pairing && !mobileChannel && isBitcoinBased(account.coinCode));
} catch (error) {
console.error(error);
}
};
fetchData();
}
}, [account, deviceIDs, devices]);

if (noMobileChannelError) {
alertUser(t('warning.sendPairing'));
}
return (
account ? (
<Send
account={account}
devices={devices}
deviceIDs={deviceIDs}
bb01Paired={bb01Paired}
activeCurrency={defaultCurrency}
/>
) : (null)
Expand Down
57 changes: 19 additions & 38 deletions frontends/web/src/routes/account/send/send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import * as accountApi from '@/api/account';
import { syncdone } from '@/api/accountsync';
import { convertFromCurrency, convertToCurrency, parseExternalBtcAmount } from '@/api/coins';
import { View, ViewContent } from '@/components/view/view';
import { TDevices, hasMobileChannel } from '@/api/devices';
import { getDeviceInfo } from '@/api/bitbox01';
import { alertUser } from '@/components/alert/Alert';
import { Balance } from '@/components/balance/balance';
import { HideAmountsButton } from '@/components/hideamountsbutton/hideamountsbutton';
Expand All @@ -47,8 +45,8 @@ import style from './send.module.css';

interface SendProps {
account: accountApi.IAccount;
devices: TDevices;
deviceIDs: string[];
// undefined if bb02 is connected.
bb01Paired: boolean | undefined;
activeCurrency: accountApi.Fiat;
}

Expand All @@ -74,7 +72,6 @@ export type State = {
amountError?: TProposalError['amountError'];
feeError?: TProposalError['feeError'];
paired?: boolean;
noMobileChannelError?: boolean;
note: string;
}

Expand All @@ -94,7 +91,6 @@ class Send extends Component<Props, State> {
sendAll: false,
isConfirming: false,
isUpdatingProposal: false,
noMobileChannelError: false,
note: '',
customFee: '',
};
Expand All @@ -106,17 +102,6 @@ class Send extends Component<Props, State> {

updateBalance(this.props.account.code);

if (this.props.deviceIDs.length > 0 && this.props.devices[this.props.deviceIDs[0]] === 'bitbox') {
hasMobileChannel(this.props.deviceIDs[0])().then((mobileChannel: boolean) => {
return getDeviceInfo(this.props.deviceIDs[0])
.then(({ pairing }) => {
const paired = mobileChannel && pairing;
const noMobileChannelError = pairing && !mobileChannel && isBitcoinBased(this.props.account.coinCode);
this.setState(prevState => ({ ...prevState, paired, noMobileChannelError }));
});
}).catch(console.error);
}

this.unsubscribe = syncdone((code) => {
if (this.props.account.code === code) {
updateBalance(code);
Expand All @@ -131,10 +116,6 @@ class Send extends Component<Props, State> {
}

private send = async () => {
if (this.state.noMobileChannelError) {
alertUser(this.props.t('warning.sendPairing'));
return;
}
const code = this.props.account.code;
const connectResult = await accountApi.connectKeystore(code);
if (!connectResult.success) {
Expand Down Expand Up @@ -383,7 +364,13 @@ class Send extends Component<Props, State> {
};

public render() {
const { account, activeCurrency, t } = this.props;
const {
account,
bb01Paired,
activeCurrency,
t,
} = this.props;

const {
balance,
proposedFee,
Expand Down Expand Up @@ -417,8 +404,6 @@ class Send extends Component<Props, State> {
activeCurrency,
};

const device = this.props.deviceIDs.length > 0 && this.props.devices[this.props.deviceIDs[0]];

return (
<GuideWrapper>
<GuidedContent>
Expand Down Expand Up @@ -519,20 +504,16 @@ class Send extends Component<Props, State> {
</Column>
</Grid>
</ViewContent>

{device && (
<ConfirmSend
device={device}
baseCurrencyUnit={activeCurrency}
note={note}
hasSelectedUTXOs={this.hasSelectedUTXOs()}
isConfirming={isConfirming}
selectedUTXOs={Object.keys(this.selectedUTXOs)}
coinCode={account.coinCode}
transactionDetails={waitDialogTransactionDetails}
/>
)}

<ConfirmSend
bb01Paired={bb01Paired}
baseCurrencyUnit={activeCurrency}
note={note}
hasSelectedUTXOs={this.hasSelectedUTXOs()}
isConfirming={isConfirming}
selectedUTXOs={Object.keys(this.selectedUTXOs)}
coinCode={account.coinCode}
transactionDetails={waitDialogTransactionDetails}
/>
<MessageWaitDialog result={sendResult}/>
</View>
</Main>
Expand Down
4 changes: 2 additions & 2 deletions frontends/web/src/routes/account/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { describe, it, expect } from 'vitest';
import { CoinCode, IAccount } from '@/api/account';
import { CoinCode, CoinUnit, IAccount } from '@/api/account';
import { getAccountsByKeystore } from './utils';


Expand All @@ -31,7 +31,7 @@ const createAccount = ({
code: 'v0-123de678-tbtc-0',
coinCode: 'tbtc' as CoinCode,
coinName: 'Bitcoin Testnet',
coinUnit: 'TBTC',
coinUnit: 'TBTC' as CoinUnit,
isToken: false,
keystore: {
connected: false,
Expand Down