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

211 lines
5.9 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 {
2021-11-15 15:18:58 +01:00
amountFractionalBase,
Amounts,
Balance,
BalancesResponse,
i18n,
2021-07-14 20:21:40 +02:00
} from "@gnu-taler/taler-util";
2021-10-11 20:59:55 +02:00
import { JSX, h, Fragment } from "preact";
import { ErrorMessage } from "../components/ErrorMessage";
2021-11-15 15:18:58 +01:00
import {
PopupBox,
Centered,
ButtonPrimary,
ErrorBox,
Middle,
} from "../components/styled/index";
2021-07-14 20:21:40 +02:00
import { BalancesHook, useBalances } from "../hooks/useBalances";
import { PageLink, renderAmount } from "../renderHtml";
2021-11-15 15:18:58 +01:00
export function BalancePage({
goToWalletManualWithdraw,
}: {
goToWalletManualWithdraw: () => void;
}) {
const balance = useBalances();
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
}
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 = (
2021-11-15 15:18:58 +01:00
<span>
<i18n.Translate>
<span style={{ color: "darkgreen" }} title="incoming amount">
{"+"}
{renderAmount(entry.pendingIncoming)}
</span>{" "}
</i18n.Translate>
</span>
2021-10-11 20:59:55 +02:00
);
}
if (!Amounts.isZero(pendingOutgoing)) {
payment = (
2021-11-15 15:18:58 +01:00
<span>
<i18n.Translate>
<span style={{ color: "darkred" }} title="outgoing amount">
{"-"}
{renderAmount(entry.pendingOutgoing)}
</span>{" "}
</i18n.Translate>
</span>
2021-07-14 20:21:40 +02:00
);
}
const l = [incoming, payment].filter((x) => x !== undefined);
if (l.length === 0) {
return <span />;
}
if (l.length === 1) {
2021-10-11 20:59:55 +02:00
return <span>{l}</span>;
2021-07-14 20:21:40 +02:00
}
return (
<span>
2021-10-11 20:59:55 +02:00
{l[0]}, {l[1]}
2021-07-14 20:21:40 +02:00
</span>
);
}
2021-11-15 15:18:58 +01:00
export function BalanceView({
balance,
Linker,
goToWalletManualWithdraw,
}: BalanceViewProps) {
2021-10-11 20:59:55 +02:00
function Content() {
if (!balance) {
2021-11-15 15:18:58 +01:00
return <span />;
2021-10-11 20:59:55 +02:00
}
if (balance.hasError) {
2021-11-15 15:18:58 +01:00
return (
<section>
<ErrorBox>{balance.message}</ErrorBox>
<p>
Click <Linker pageName="welcome">here</Linker> for help and
diagnostics.
</p>
</section>
);
2021-10-11 20:59:55 +02:00
}
if (balance.response.balances.length === 0) {
2021-11-15 15:18:58 +01:00
return (
<section data-expanded>
<Middle>
<p>
<i18n.Translate>
You have no balance to show. Need some{" "}
<Linker pageName="/welcome">help</Linker> getting started?
</i18n.Translate>
</p>
</Middle>
</section>
);
2021-10-11 20:59:55 +02:00
}
2021-11-15 15:18:58 +01:00
return (
<section data-expanded data-centered>
<table style={{ width: "100%" }}>
{balance.response.balances.map((entry) => {
const av = Amounts.parseOrThrow(entry.available);
// Create our number formatter.
let formatter;
try {
formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: av.currency,
currencyDisplay: "symbol",
// These options are needed to round to whole numbers if that's what you want.
//minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
//maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
});
} catch {
formatter = new Intl.NumberFormat("en-US", {
// style: 'currency',
// currency: av.currency,
// These options are needed to round to whole numbers if that's what you want.
//minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
//maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
});
}
const v = formatter.format(
av.value + av.fraction / amountFractionalBase,
);
const fontSize =
v.length < 8 ? "3em" : v.length < 13 ? "2em" : "1em";
return (
<tr>
<td
style={{
height: 50,
fontSize,
width: "60%",
textAlign: "right",
padding: 0,
}}
>
{v}
</td>
<td style={{ maxWidth: "2em", overflowX: "hidden" }}>
{av.currency}
</td>
<td style={{ fontSize: "small", color: "gray" }}>
{formatPending(entry)}
</td>
</tr>
);
})}
</table>
</section>
);
2021-10-11 20:59:55 +02:00
}
2021-11-15 15:18:58 +01:00
return (
<PopupBox>
{/* <section> */}
<Content />
{/* </section> */}
<footer>
<div />
<ButtonPrimary onClick={goToWalletManualWithdraw}>
Withdraw
</ButtonPrimary>
</footer>
</PopupBox>
);
2021-07-14 20:21:40 +02:00
}