-
Notifications
You must be signed in to change notification settings - Fork 96
frontend/account-summary: add coins total balance #2573
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
frontends/web/src/routes/account/summary/coinbalance.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* 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. | ||
* 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 * as accountApi from '../../../api/account'; | ||
import { SubTotalCoinRow } from './subtotalrow'; | ||
import { Amount } from '../../../components/amount/amount'; | ||
import { Skeleton } from '../../../components/skeleton/skeleton'; | ||
import style from './accountssummary.module.css'; | ||
|
||
type TProps = { | ||
accounts: accountApi.IAccount[], | ||
summaryData?: accountApi.ISummary, | ||
coinsBalances?: accountApi.TCoinsTotalBalance, | ||
} | ||
|
||
type TAccountCoinMap = { | ||
[code in accountApi.CoinCode]: accountApi.IAccount[]; | ||
}; | ||
|
||
export function CoinBalance ({ | ||
accounts, | ||
summaryData, | ||
coinsBalances, | ||
}: TProps) { | ||
const { t } = useTranslation(); | ||
|
||
const getAccountsPerCoin = () => { | ||
return accounts.reduce((accountPerCoin, account) => { | ||
accountPerCoin[account.coinCode] | ||
? accountPerCoin[account.coinCode].push(account) | ||
: accountPerCoin[account.coinCode] = [account]; | ||
return accountPerCoin; | ||
}, {} as TAccountCoinMap); | ||
}; | ||
|
||
const accountsPerCoin = getAccountsPerCoin(); | ||
const coins = Object.keys(accountsPerCoin) as accountApi.CoinCode[]; | ||
|
||
return ( | ||
<div> | ||
<div className={style.accountName}> | ||
<p>{t('accountSummary.total')}</p> | ||
</div> | ||
<div className={style.balanceTable}> | ||
<table className={style.table}> | ||
<colgroup> | ||
<col width="33%" /> | ||
<col width="33%" /> | ||
<col width="*" /> | ||
</colgroup> | ||
<thead> | ||
<tr> | ||
<th>{t('accountSummary.coin')}</th> | ||
<th>{t('accountSummary.balance')}</th> | ||
<th>{t('accountSummary.fiatBalance')}</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{ accounts.length > 0 ? ( | ||
coins.map(coinCode => { | ||
if (accountsPerCoin[coinCode]?.length >= 1) { | ||
const account = accountsPerCoin[coinCode][0]; | ||
return ( | ||
<SubTotalCoinRow | ||
key={account.coinCode} | ||
coinCode={account.coinCode} | ||
coinName={account.coinName} | ||
balance={coinsBalances && coinsBalances[coinCode]} | ||
/> | ||
); | ||
} | ||
return null; | ||
})) : null} | ||
</tbody> | ||
<tfoot> | ||
<tr> | ||
<th> | ||
<strong>{t('accountSummary.total')}</strong> | ||
</th> | ||
<td colSpan={2}> | ||
{(summaryData && summaryData.formattedChartTotal !== null) ? ( | ||
<> | ||
<strong> | ||
<Amount amount={summaryData.formattedChartTotal} unit={summaryData.chartFiat}/> | ||
</strong> | ||
{' '} | ||
<span className={style.coinUnit}> | ||
{summaryData.chartFiat} | ||
</span> | ||
</> | ||
) : (<Skeleton />) } | ||
</td> | ||
</tr> | ||
</tfoot> | ||
</table> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just merged a PR that creates a new method
backend.accountFiatBalance
(https://github.com/digitalbitbox/bitbox-wallet-app/blob/master/backend/accounts.go#L230). It would be nice to refactorgetCoinsTotalBalance
to exploit the function and avoid code duplication.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it would be nice to add unit tests for
getCoinsTotalBalance
, but it's probably easier to do it after #2633 gets mergedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I didn't realize that the function was providing the fiat conversions for each enabled fiat and that now it is possible to cycle across fiat units also in the account summary.