Skip to content

Commit

Permalink
frontend: move account guide to its own component
Browse files Browse the repository at this point in the history
Moved isBTCScriptType to utils and the account guide to its own
component so that account.tsx is slightly simpler and easier to
refactor.
  • Loading branch information
thisconnect committed Oct 4, 2022
1 parent 6054a36 commit b1a2e72
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 85 deletions.
92 changes: 8 additions & 84 deletions frontends/web/src/routes/account/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { unsubscribe, UnsubscribeList } from '../../utils/subscriptions';
import { statusChanged, syncdone } from '../../api/subscribe-legacy';
import { alertUser } from '../../components/alert/Alert';
import { Balance } from '../../components/balance/balance';
import { Entry } from '../../components/guide/entry';
import { Guide } from '../../components/guide/guide';
import { AccountGuide } from './guide';
import { HeadersSync } from '../../components/headerssync/headerssync';
import { Header } from '../../components/layout';
import { Info } from '../../components/icon';
Expand Down Expand Up @@ -227,19 +226,6 @@ class Account extends Component<Props, State> {
.catch(console.error);
};

private isBTCScriptType = (
scriptType: accountApi.ScriptType,
account: accountApi.IAccount,
accountInfo?: accountApi.ISigningConfigurationList,
): boolean => {
if (!accountInfo || accountInfo.signingConfigurations.length !== 1) {
return false;
}
const config = accountInfo.signingConfigurations[0].bitcoinSimple;
return (account.coinCode === 'btc' || account.coinCode === 'tbtc') &&
config !== undefined && config.scriptType === scriptType;
};

private deviceIDs = (devices: TDevices) => {
return Object.keys(devices);
};
Expand Down Expand Up @@ -367,75 +353,13 @@ class Account extends Component<Props, State> {
</div>
</div>
</div>
<Guide>
<Entry key="accountDescription" entry={t('guide.accountDescription')} />
{this.isBTCScriptType('p2pkh', account, accountInfo) && (
<Entry key="guide.settings.btc-p2pkh" entry={t('guide.settings.btc-p2pkh')} />
)}
{this.isBTCScriptType('p2wpkh-p2sh', account, accountInfo) && (
<Entry key="guide.settings.btc-p2sh" entry={{
link: {
text: t('guide.settings.btc-p2sh.link.text'),
url: 'https://bitcoincore.org/en/2016/01/26/segwit-benefits/'
},
text: t('guide.settings.btc-p2sh.text'),
title: t('guide.settings.btc-p2sh.title')
}} />
)}
{this.isBTCScriptType('p2wpkh', account, accountInfo) && (
<Entry key="guide.settings.btc-p2wpkh" entry={{
link: {
text: t('guide.settings.btc-p2wpkh.link.text'),
url: 'https://en.bitcoin.it/wiki/Bech32_adoption'
},
text: t('guide.settings.btc-p2wpkh.text'),
title: t('guide.settings.btc-p2wpkh.title')
}} />
)}
{balance && balance.available.amount === '0' && (
<Entry key="accountSendDisabled" entry={t('guide.accountSendDisabled', { unit: balance.available.unit })} />
)}
<Entry key="accountReload" entry={t('guide.accountReload')} />
{transactions !== undefined && transactions.length > 0 && (
<Entry key="accountTransactionLabel" entry={t('guide.accountTransactionLabel')} />
)}
{transactions !== undefined && transactions.length > 0 && (
<Entry key="accountTransactionTime" entry={t('guide.accountTransactionTime')} />
)}
{this.isBTCScriptType('p2pkh', account, accountInfo) && (
<Entry key="accountLegacyConvert" entry={t('guide.accountLegacyConvert')} />
)}
{transactions !== undefined && transactions.length > 0 && (
<Entry key="accountTransactionAttributesGeneric" entry={t('guide.accountTransactionAttributesGeneric')} />
)}
{transactions !== undefined && transactions.length > 0 && isBitcoinBased(account.coinCode) && (
<Entry key="accountTransactionAttributesBTC" entry={t('guide.accountTransactionAttributesBTC')} />
)}
{balance && balance.hasIncoming && (
<Entry key="accountIncomingBalance" entry={t('guide.accountIncomingBalance')} />
)}
<Entry key="accountTransactionConfirmation" entry={t('guide.accountTransactionConfirmation')} />
<Entry key="accountFiat" entry={t('guide.accountFiat')} />

{ /* careful, also used in Settings */ }
<Entry key="accountRates" entry={{
link: {
text: 'www.coingecko.com',
url: 'https://www.coingecko.com/'
},
text: t('guide.accountRates.text'),
title: t('guide.accountRates.title')
}} />

<Entry key="cointracking" entry={{
link: {
text: 'CoinTracking',
url: 'https://cointracking.info/import/bitbox/?ref=BITBOX',
},
text: t('guide.cointracking.text'),
title: t('guide.cointracking.title')
}} />
</Guide>
<AccountGuide
account={account}
accountInfo={accountInfo}
unit={balance?.available.unit}
hasIncomingBalance={balance && balance.hasIncoming}
hasTransactions={transactions !== undefined && transactions.length > 0}
hasNoBalance={balance && balance.available.amount === '0'} />
</div>
);
}
Expand Down
114 changes: 114 additions & 0 deletions frontends/web/src/routes/account/guide.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { useTranslation } from 'react-i18next';
import { IAccount, ISigningConfigurationList } from '../../api/account';
import { Entry } from '../../components/guide/entry';
import { Guide } from '../../components/guide/guide';
import { isBitcoinBased, isBTCScriptType } from './utils';

type Props = {
account: IAccount;
accountInfo?: ISigningConfigurationList;
unit?: string;
hasNoBalance?: boolean;
hasIncomingBalance?: boolean;
hasTransactions?: boolean;
};

export function AccountGuide({
account,
accountInfo,
unit,
hasNoBalance,
hasIncomingBalance,
hasTransactions,
}: Props) {
const { t } = useTranslation();
return (
<Guide>
<Entry key="accountDescription" entry={t('guide.accountDescription')} />
{isBTCScriptType('p2pkh', account, accountInfo) && (
<Entry key="guide.settings.btc-p2pkh" entry={t('guide.settings.btc-p2pkh')} />
)}
{isBTCScriptType('p2wpkh-p2sh', account, accountInfo) && (
<Entry key="guide.settings.btc-p2sh" entry={{
link: {
text: t('guide.settings.btc-p2sh.link.text'),
url: 'https://bitcoincore.org/en/2016/01/26/segwit-benefits/'
},
text: t('guide.settings.btc-p2sh.text'),
title: t('guide.settings.btc-p2sh.title')
}} />
)}
{isBTCScriptType('p2wpkh', account, accountInfo) && (
<Entry key="guide.settings.btc-p2wpkh" entry={{
link: {
text: t('guide.settings.btc-p2wpkh.link.text'),
url: 'https://en.bitcoin.it/wiki/Bech32_adoption'
},
text: t('guide.settings.btc-p2wpkh.text'),
title: t('guide.settings.btc-p2wpkh.title')
}} />
)}
{hasNoBalance && (
<Entry key="accountSendDisabled" entry={t('guide.accountSendDisabled', {
unit
})} />
)}
<Entry key="accountReload" entry={t('guide.accountReload')} />
{hasTransactions && (
<Entry key="accountTransactionLabel" entry={t('guide.accountTransactionLabel')} />
)}
{hasTransactions && (
<Entry key="accountTransactionTime" entry={t('guide.accountTransactionTime')} />
)}
{isBTCScriptType('p2pkh', account, accountInfo) && (
<Entry key="accountLegacyConvert" entry={t('guide.accountLegacyConvert')} />
)}
{hasTransactions && (
<Entry key="accountTransactionAttributesGeneric" entry={t('guide.accountTransactionAttributesGeneric')} />
)}
{hasTransactions && isBitcoinBased(account.coinCode) && (
<Entry key="accountTransactionAttributesBTC" entry={t('guide.accountTransactionAttributesBTC')} />
)}
{hasIncomingBalance && (
<Entry key="accountIncomingBalance" entry={t('guide.accountIncomingBalance')} />
)}
<Entry key="accountTransactionConfirmation" entry={t('guide.accountTransactionConfirmation')} />
<Entry key="accountFiat" entry={t('guide.accountFiat')} />

{ /* careful, also used in Settings */ }
<Entry key="accountRates" entry={{
link: {
text: 'www.coingecko.com',
url: 'https://www.coingecko.com/'
},
text: t('guide.accountRates.text'),
title: t('guide.accountRates.title')
}} />

<Entry key="cointracking" entry={{
link: {
text: 'CoinTracking',
url: 'https://cointracking.info/import/bitbox/?ref=BITBOX',
},
text: t('guide.cointracking.text'),
title: t('guide.cointracking.title')
}} />
</Guide>
);
}
15 changes: 14 additions & 1 deletion frontends/web/src/routes/account/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { CoinCode, ScriptType } from '../../api/account';
import { CoinCode, IAccount, ISigningConfigurationList, ScriptType } from '../../api/account';

export function isBitcoinOnly(coinCode: CoinCode): boolean {
switch (coinCode) {
Expand Down Expand Up @@ -81,3 +81,16 @@ export function customFeeUnit(coinCode: CoinCode): string {
}
return '';
}

export function isBTCScriptType(
scriptType: ScriptType,
account: IAccount,
accountInfo?: ISigningConfigurationList,
): boolean {
if (!accountInfo || accountInfo.signingConfigurations.length !== 1) {
return false;
}
const config = accountInfo.signingConfigurations[0].bitcoinSimple;
return (account.coinCode === 'btc' || account.coinCode === 'tbtc')
&& config !== undefined && config.scriptType === scriptType;
}

0 comments on commit b1a2e72

Please sign in to comment.