wallet-core/packages/taler-wallet-webextension/src/popup/BalancePage.tsx

125 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-07-14 20:21:40 +02:00
/*
This file is part of TALER
(C) 2016 GNUnet e.V.
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import {
amountFractionalBase, Amounts,
Balance, BalancesResponse,
i18n
} from "@gnu-taler/taler-util";
2021-08-23 21:51:49 +02:00
import { JSX, h } from "preact";
2021-09-20 19:05:40 +02:00
import { PopupBox, Centered, ButtonPrimary } from "../components/styled/index";
2021-07-14 20:21:40 +02:00
import { BalancesHook, useBalances } from "../hooks/useBalances";
import { PageLink, renderAmount } from "../renderHtml";
2021-09-20 19:05:40 +02:00
export function BalancePage({ goToWalletManualWithdraw }: { goToWalletManualWithdraw: () => void }) {
2021-07-14 20:21:40 +02:00
const balance = useBalances()
2021-09-20 19:05:40 +02:00
return <BalanceView balance={balance} Linker={PageLink} goToWalletManualWithdraw={goToWalletManualWithdraw} />
2021-07-14 20:21:40 +02:00
}
export interface BalanceViewProps {
2021-09-20 19:05:40 +02:00
balance: BalancesHook;
Linker: typeof PageLink;
goToWalletManualWithdraw: () => void;
2021-07-14 20:21:40 +02:00
}
2021-09-20 19:05:40 +02:00
export function BalanceView({ balance, Linker, goToWalletManualWithdraw }: BalanceViewProps) {
2021-07-14 20:21:40 +02:00
if (!balance) {
return <span />
}
if (balance.error) {
return (
2021-07-16 17:00:39 +02:00
<div>
2021-07-14 20:21:40 +02:00
<p>{i18n.str`Error: could not retrieve balance information.`}</p>
<p>
2021-08-13 23:04:05 +02:00
Click <Linker pageName="welcome">here</Linker> for help and
2021-07-14 20:21:40 +02:00
diagnostics.
</p>
</div>
)
}
if (balance.response.balances.length === 0) {
return (
<p><i18n.Translate>
You have no balance to show. Need some{" "}
<Linker pageName="/welcome">help</Linker> getting started?
</i18n.Translate></p>
)
}
2021-09-20 19:05:40 +02:00
return <ShowBalances wallet={balance.response}
onWithdraw={goToWalletManualWithdraw}
/>
2021-07-14 20:21:40 +02:00
}
function formatPending(entry: Balance): JSX.Element {
let incoming: JSX.Element | undefined;
let payment: JSX.Element | undefined;
const available = Amounts.parseOrThrow(entry.available);
const pendingIncoming = Amounts.parseOrThrow(entry.pendingIncoming);
const pendingOutgoing = Amounts.parseOrThrow(entry.pendingOutgoing);
if (!Amounts.isZero(pendingIncoming)) {
incoming = (
<span><i18n.Translate>
<span style={{ color: "darkgreen" }}>
{"+"}
{renderAmount(entry.pendingIncoming)}
</span>{" "}
incoming
</i18n.Translate></span>
);
}
const l = [incoming, payment].filter((x) => x !== undefined);
if (l.length === 0) {
return <span />;
}
if (l.length === 1) {
return <span>({l})</span>;
}
return (
<span>
({l[0]}, {l[1]})
</span>
);
}
2021-09-20 19:05:40 +02:00
function ShowBalances({ wallet, onWithdraw }: { wallet: BalancesResponse, onWithdraw: () => void }) {
2021-07-14 20:21:40 +02:00
return <PopupBox>
<section>
<Centered>{wallet.balances.map((entry) => {
const av = Amounts.parseOrThrow(entry.available);
const v = av.value + av.fraction / amountFractionalBase;
return (
<p key={av.currency}>
<span>
<span style={{ fontSize: "5em", display: "block" }}>{v}</span>{" "}
<span>{av.currency}</span>
</span>
{formatPending(entry)}
</p>
);
})}</Centered>
</section>
2021-09-20 19:05:40 +02:00
<footer>
<div />
<ButtonPrimary onClick={onWithdraw} >Withdraw</ButtonPrimary>
</footer>
2021-07-14 20:21:40 +02:00
</PopupBox>
}