wallet-core/packages/taler-wallet-webextension/src/wallet/History.tsx

258 lines
7.5 KiB
TypeScript
Raw Normal View History

/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2022-06-06 17:05:26 +02:00
GNU 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.
2022-06-06 17:05:26 +02:00
GNU 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
2022-06-06 17:05:26 +02:00
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2021-11-15 15:18:58 +01:00
import {
Amounts,
2021-11-15 15:18:58 +01:00
Balance,
NotificationType,
2021-11-15 15:18:58 +01:00
Transaction,
} from "@gnu-taler/taler-util";
2022-10-25 17:23:08 +02:00
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
2021-11-16 17:59:53 +01:00
import { Fragment, h, VNode } from "preact";
2022-04-26 03:37:41 +02:00
import { useEffect, useState } from "preact/hooks";
2022-03-29 04:41:07 +02:00
import { Loading } from "../components/Loading.js";
import { LoadingError } from "../components/LoadingError.js";
import {
CenteredBoldText,
CenteredText,
DateSeparator,
NiceSelect,
2022-03-29 04:41:07 +02:00
} from "../components/styled/index.js";
import { Time } from "../components/Time.js";
import { TransactionItem } from "../components/TransactionItem.js";
import { useTranslationContext } from "../context/translation.js";
2022-04-26 04:07:31 +02:00
import { useAsyncAsHook } from "../hooks/useAsyncAsHook.js";
2022-06-01 20:47:47 +02:00
import { Button } from "../mui/Button.js";
2022-03-29 04:41:07 +02:00
import { NoBalanceHelp } from "../popup/NoBalanceHelp.js";
2022-09-13 18:39:25 +02:00
import DownloadIcon from "../svg/download_24px.svg";
import UploadIcon from "../svg/upload_24px.svg";
2022-10-25 17:23:08 +02:00
import { wxApi } from "../wxApi.js";
interface Props {
currency?: string;
2022-06-01 20:47:47 +02:00
goToWalletDeposit: (currency: string) => Promise<void>;
goToWalletManualWithdraw: (currency?: string) => Promise<void>;
}
export function HistoryPage({
currency,
goToWalletManualWithdraw,
goToWalletDeposit,
}: Props): VNode {
const { i18n } = useTranslationContext();
2022-04-26 04:07:31 +02:00
const state = useAsyncAsHook(async () => ({
2022-10-25 17:23:08 +02:00
b: await wxApi.wallet.call(WalletApiOperation.GetBalances, {}),
tx: await wxApi.wallet.call(WalletApiOperation.GetTransactions, {}),
2022-04-26 03:37:41 +02:00
}));
useEffect(() => {
2022-10-25 17:23:08 +02:00
return wxApi.listener.onUpdateNotification(
2022-06-09 19:16:28 +02:00
[NotificationType.WithdrawGroupFinished],
2022-10-25 17:23:08 +02:00
state?.retry,
2022-06-09 19:16:28 +02:00
);
2022-04-26 03:37:41 +02:00
});
if (!state) {
return <Loading />;
}
if (state.hasError) {
return (
<LoadingError
title={
<i18n.Translate>
Could not load the list of transactions
</i18n.Translate>
}
error={state}
/>
);
}
2021-11-15 15:18:58 +01:00
return (
<HistoryView
balances={state.response.b.balances}
defaultCurrency={currency}
goToWalletManualWithdraw={goToWalletManualWithdraw}
goToWalletDeposit={goToWalletDeposit}
transactions={[...state.response.tx.transactions].reverse()}
2021-11-15 15:18:58 +01:00
/>
);
}
2021-11-16 17:59:53 +01:00
const term = 1000 * 60 * 60 * 24;
function normalizeToDay(x: number): number {
return Math.round(x / term) * term;
}
2021-11-15 15:18:58 +01:00
export function HistoryView({
defaultCurrency,
transactions,
2021-11-15 15:18:58 +01:00
balances,
goToWalletManualWithdraw,
goToWalletDeposit,
2021-11-15 15:18:58 +01:00
}: {
2022-06-01 20:47:47 +02:00
goToWalletDeposit: (currency: string) => Promise<void>;
goToWalletManualWithdraw: (currency?: string) => Promise<void>;
defaultCurrency?: string;
transactions: Transaction[];
2021-11-15 15:18:58 +01:00
balances: Balance[];
2021-11-16 17:59:53 +01:00
}): VNode {
const { i18n } = useTranslationContext();
const currencies = balances.map((b) => b.available.split(":")[0]);
2021-11-16 17:59:53 +01:00
const defaultCurrencyIndex = currencies.findIndex(
(c) => c === defaultCurrency,
);
const [currencyIndex, setCurrencyIndex] = useState(
defaultCurrencyIndex === -1 ? 0 : defaultCurrencyIndex,
);
const selectedCurrency =
currencies.length > 0 ? currencies[currencyIndex] : undefined;
const currencyAmount = balances[currencyIndex]
? Amounts.jsonifyAmount(balances[currencyIndex].available)
: undefined;
const byDate = transactions
.filter((t) => t.amountRaw.split(":")[0] === selectedCurrency)
.reduce((rv, x) => {
const theDate =
2022-03-29 04:41:07 +02:00
x.timestamp.t_s === "never"
? 0
: normalizeToDay(x.timestamp.t_s * 1000);
if (theDate) {
(rv[theDate] = rv[theDate] || []).push(x);
}
return rv;
}, {} as { [x: string]: Transaction[] });
const datesWithTransaction = Object.keys(byDate);
if (balances.length === 0 || !selectedCurrency) {
return (
<NoBalanceHelp
goToWalletManualWithdraw={{
onClick: goToWalletManualWithdraw,
}}
/>
);
}
2021-11-15 15:18:58 +01:00
return (
<Fragment>
<section>
<div
style={{
display: "flex",
flexWrap: "wrap",
alignItems: "center",
justifyContent: "space-between",
}}
>
<div
style={{
width: "fit-content",
display: "flex",
}}
>
{currencies.length === 1 ? (
<CenteredText style={{ fontSize: "x-large", margin: 8 }}>
{selectedCurrency}
</CenteredText>
) : (
<NiceSelect>
<select
style={{
fontSize: "x-large",
}}
value={currencyIndex}
onChange={(e) => {
setCurrencyIndex(Number(e.currentTarget.value));
}}
>
{currencies.map((currency, index) => {
return (
<option value={index} key={currency}>
{currency}
</option>
);
})}
</select>
</NiceSelect>
)}
{currencyAmount && (
<CenteredBoldText
style={{
display: "inline-block",
fontSize: "x-large",
margin: 8,
}}
>
{Amounts.stringifyValue(currencyAmount, 2)}
</CenteredBoldText>
)}
</div>
<div>
2022-06-01 20:47:47 +02:00
<Button
2022-08-17 21:12:21 +02:00
tooltip="Transfer money to the wallet"
startIcon={DownloadIcon}
2022-06-01 20:47:47 +02:00
variant="contained"
onClick={() => goToWalletManualWithdraw(selectedCurrency)}
>
2022-08-16 02:18:39 +02:00
<i18n.Translate>Add</i18n.Translate>
2022-06-01 20:47:47 +02:00
</Button>
{currencyAmount && Amounts.isNonZero(currencyAmount) && (
2022-06-01 20:47:47 +02:00
<Button
2022-08-17 21:12:21 +02:00
tooltip="Transfer money from the wallet"
startIcon={UploadIcon}
2022-06-01 20:47:47 +02:00
variant="outlined"
color="primary"
onClick={() => goToWalletDeposit(selectedCurrency)}
>
2022-08-14 15:10:37 +02:00
<i18n.Translate>Send</i18n.Translate>
2022-06-01 20:47:47 +02:00
</Button>
)}
</div>
</div>
2021-11-15 15:18:58 +01:00
</section>
{datesWithTransaction.length === 0 ? (
2022-02-23 19:18:37 +01:00
<section>
2022-09-13 16:07:39 +02:00
<i18n.Translate>
Your transaction history is empty for this currency.
</i18n.Translate>
2022-02-23 19:18:37 +01:00
</section>
) : (
<section>
{datesWithTransaction.map((d, i) => {
return (
<Fragment key={i}>
<DateSeparator>
<Time
timestamp={{ t_ms: Number.parseInt(d, 10) }}
format="dd MMMM yyyy"
/>
</DateSeparator>
{byDate[d].map((tx, i) => (
<TransactionItem key={i} tx={tx} />
))}
</Fragment>
);
})}
</section>
)}
</Fragment>
2021-11-15 15:18:58 +01:00
);
}