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

351 lines
9.4 KiB
TypeScript
Raw Normal View History

2021-08-24 18:29:37 +02:00
/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2021-08-24 18:29:37 +02:00
2022-06-06 17:05:26 +02:00
GNU Taler is free software; you can redistribute it and/or modify it under the
2021-08-24 18:29:37 +02:00
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
2021-08-24 18:29:37 +02:00
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-08-24 18:29:37 +02:00
2022-10-25 17:23:08 +02:00
import { AbsoluteTime, constructRecoveryUri } from "@gnu-taler/taler-util";
2021-11-15 15:18:58 +01:00
import {
ProviderInfo,
ProviderPaymentPaid,
2021-11-15 15:18:58 +01:00
ProviderPaymentStatus,
ProviderPaymentType,
WalletApiOperation,
2021-11-15 15:18:58 +01:00
} from "@gnu-taler/taler-wallet-core";
import {
differenceInMonths,
formatDuration,
intervalToDuration,
} from "date-fns";
2021-11-16 17:59:53 +01:00
import { Fragment, h, VNode } from "preact";
2022-10-25 17:23:08 +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";
2022-10-25 17:23:08 +02:00
import { QR } from "../components/QR.js";
2021-08-24 18:29:37 +02:00
import {
2021-11-15 15:18:58 +01:00
BoldLight,
Centered,
CenteredBoldText,
2021-11-16 17:59:53 +01:00
CenteredText,
2021-11-15 15:18:58 +01:00
RowBorderGray,
SmallLightText,
2021-11-16 17:59:53 +01:00
SmallText,
WarningBox,
2022-03-29 04:41:07 +02:00
} from "../components/styled/index.js";
import { useBackendContext } from "../context/backend.js";
2022-03-29 04:41:07 +02:00
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 { Pages } from "../NavigationBar.js";
2021-08-24 18:29:37 +02:00
interface Props {
2022-06-01 20:47:47 +02:00
onAddProvider: () => Promise<void>;
2021-08-24 18:29:37 +02:00
}
export function ShowRecoveryInfo({
info,
onClose,
}: {
info: string;
onClose: () => Promise<void>;
}): VNode {
const [display, setDisplay] = useState(false);
const [copied, setCopied] = useState(false);
async function copyText(): Promise<void> {
navigator.clipboard.writeText(info);
setCopied(true);
}
useEffect(() => {
if (copied) {
setTimeout(() => {
setCopied(false);
}, 1000);
}
}, [copied]);
return (
<Fragment>
<h2>Wallet Recovery</h2>
<WarningBox>Do not share this QR or URI with anyone</WarningBox>
<section>
<p>
The qr code can be scanned by another wallet to keep synchronized with
this wallet.
</p>
<Button variant="contained" onClick={async () => setDisplay((d) => !d)}>
{display ? "Hide" : "Show"} QR code
</Button>
{display && <QR text={JSON.stringify(info)} />}
</section>
<section>
<p>You can also use the string version</p>
<Button variant="contained" disabled={copied} onClick={copyText}>
Copy recovery URI
</Button>
</section>
<footer>
<div></div>
<div>
<Button variant="contained" onClick={onClose}>
Close
</Button>
</div>
</footer>
</Fragment>
);
}
2021-08-24 18:29:37 +02:00
export function BackupPage({ onAddProvider }: Props): VNode {
const { i18n } = useTranslationContext();
const api = useBackendContext();
2022-10-25 17:23:08 +02:00
const status = useAsyncAsHook(() =>
api.wallet.call(WalletApiOperation.GetBackupInfo, {}),
2022-10-25 17:23:08 +02:00
);
const [recoveryInfo, setRecoveryInfo] = useState<string>("");
2021-08-24 18:29:37 +02:00
if (!status) {
return <Loading />;
}
if (status.hasError) {
return (
2022-02-23 19:18:37 +01:00
<LoadingError
title={<i18n.Translate>Could not load backup providers</i18n.Translate>}
2022-02-23 19:18:37 +01:00
error={status}
/>
);
2021-08-24 18:29:37 +02:00
}
async function getRecoveryInfo(): Promise<void> {
const r = await api.wallet.call(
2022-10-25 17:23:08 +02:00
WalletApiOperation.ExportBackupRecovery,
{},
);
const str = constructRecoveryUri(r);
setRecoveryInfo(str);
}
const providers = status.response.providers.sort((a, b) => {
if (
a.paymentStatus.type === ProviderPaymentType.Paid &&
b.paymentStatus.type === ProviderPaymentType.Paid
) {
return getStatusPaidOrder(a.paymentStatus, b.paymentStatus);
}
return (
getStatusTypeOrder(a.paymentStatus) - getStatusTypeOrder(b.paymentStatus)
);
});
if (recoveryInfo) {
return (
<ShowRecoveryInfo
info={recoveryInfo}
onClose={async () => setRecoveryInfo("")}
/>
);
}
2021-11-15 15:18:58 +01:00
return (
<BackupView
providers={providers}
2021-11-15 15:18:58 +01:00
onAddProvider={onAddProvider}
2022-10-25 17:23:08 +02:00
onSyncAll={async () =>
api.wallet.call(WalletApiOperation.RunBackupCycle, {}).then()
2022-10-25 17:23:08 +02:00
}
onShowInfo={getRecoveryInfo}
2021-11-15 15:18:58 +01:00
/>
);
2021-08-24 18:29:37 +02:00
}
export interface ViewProps {
2021-11-15 15:18:58 +01:00
providers: ProviderInfo[];
2022-06-01 20:47:47 +02:00
onAddProvider: () => Promise<void>;
2021-08-24 18:29:37 +02:00
onSyncAll: () => Promise<void>;
onShowInfo: () => Promise<void>;
2021-08-24 18:29:37 +02:00
}
2021-11-15 15:18:58 +01:00
export function BackupView({
providers,
onAddProvider,
onSyncAll,
onShowInfo,
2021-11-15 15:18:58 +01:00
}: ViewProps): VNode {
const { i18n } = useTranslationContext();
2021-08-24 18:29:37 +02:00
return (
<Fragment>
2021-08-24 18:29:37 +02:00
<section>
2021-11-16 17:59:53 +01:00
{providers.map((provider, idx) => (
2021-11-15 15:18:58 +01:00
<BackupLayout
2021-11-16 17:59:53 +01:00
key={idx}
2021-11-15 15:18:58 +01:00
status={provider.paymentStatus}
2022-03-18 15:32:41 +01:00
timestamp={
provider.lastSuccessfulBackupTimestamp
? AbsoluteTime.fromTimestamp(
provider.lastSuccessfulBackupTimestamp,
)
: undefined
}
2021-11-15 15:18:58 +01:00
id={provider.syncProviderBaseUrl}
active={provider.active}
title={provider.name}
/>
))}
{!providers.length && (
<Centered style={{ marginTop: 100 }}>
2022-02-23 19:18:37 +01:00
<BoldLight>
<i18n.Translate>No backup providers configured</i18n.Translate>
2022-02-23 19:18:37 +01:00
</BoldLight>
2022-06-01 20:47:47 +02:00
<Button variant="contained" color="success" onClick={onAddProvider}>
<i18n.Translate>Add provider</i18n.Translate>
2022-06-01 20:47:47 +02:00
</Button>
2021-11-15 15:18:58 +01:00
</Centered>
2021-08-24 18:29:37 +02:00
)}
</section>
2021-11-15 15:18:58 +01:00
{!!providers.length && (
<footer>
<div>
<Button variant="contained" onClick={onShowInfo}>
Show recovery
</Button>
</div>
2021-11-15 15:18:58 +01:00
<div>
2022-06-01 20:47:47 +02:00
<Button variant="contained" onClick={onSyncAll}>
2021-11-15 15:18:58 +01:00
{providers.length > 1 ? (
<i18n.Translate>Sync all backups</i18n.Translate>
2021-11-15 15:18:58 +01:00
) : (
<i18n.Translate>Sync now</i18n.Translate>
2021-11-15 15:18:58 +01:00
)}
2022-06-01 20:47:47 +02:00
</Button>
<Button variant="contained" color="success" onClick={onAddProvider}>
<i18n.Translate>Add provider</i18n.Translate>
2022-06-01 20:47:47 +02:00
</Button>
2021-11-15 15:18:58 +01:00
</div>
</footer>
)}
</Fragment>
2021-11-15 15:18:58 +01:00
);
2021-08-24 18:29:37 +02:00
}
interface TransactionLayoutProps {
status: ProviderPaymentStatus;
2022-03-18 15:32:41 +01:00
timestamp?: AbsoluteTime;
2021-08-24 18:29:37 +02:00
title: string;
id: string;
active: boolean;
}
2021-11-16 17:59:53 +01:00
function BackupLayout(props: TransactionLayoutProps): VNode {
const { i18n } = useTranslationContext();
2021-08-24 18:29:37 +02:00
const date = !props.timestamp ? undefined : new Date(props.timestamp.t_ms);
const dateStr = date?.toLocaleString([], {
dateStyle: "medium",
timeStyle: "short",
} as any);
return (
<RowBorderGray>
<div style={{ color: !props.active ? "grey" : undefined }}>
2021-11-15 15:18:58 +01:00
<a
2022-06-02 17:20:36 +02:00
href={Pages.backupProviderDetail({
pid: encodeURIComponent(props.id),
})}
2021-11-15 15:18:58 +01:00
>
<span>{props.title}</span>
</a>
{dateStr && (
2022-02-23 19:18:37 +01:00
<SmallText style={{ marginTop: 5 }}>
<i18n.Translate>Last synced</i18n.Translate>: {dateStr}
2022-02-23 19:18:37 +01:00
</SmallText>
2021-11-15 15:18:58 +01:00
)}
{!dateStr && (
2022-02-23 19:18:37 +01:00
<SmallLightText style={{ marginTop: 5 }}>
<i18n.Translate>Not synced</i18n.Translate>
2022-02-23 19:18:37 +01:00
</SmallLightText>
2021-11-15 15:18:58 +01:00
)}
2021-08-24 18:29:37 +02:00
</div>
<div>
2021-11-15 15:18:58 +01:00
{props.status?.type === "paid" ? (
<ExpirationText until={props.status.paidUntil} />
) : (
2021-08-24 18:29:37 +02:00
<div>{props.status.type}</div>
2021-11-15 15:18:58 +01:00
)}
2021-08-24 18:29:37 +02:00
</div>
</RowBorderGray>
);
}
2022-03-18 15:32:41 +01:00
function ExpirationText({ until }: { until: AbsoluteTime }): VNode {
const { i18n } = useTranslationContext();
2021-11-15 15:18:58 +01:00
return (
<Fragment>
2022-02-23 19:18:37 +01:00
<CenteredText>
<i18n.Translate>Expires in</i18n.Translate>
2022-02-23 19:18:37 +01:00
</CenteredText>
2021-11-15 15:18:58 +01:00
<CenteredBoldText {...{ color: colorByTimeToExpire(until) }}>
{" "}
{daysUntil(until)}{" "}
</CenteredBoldText>
</Fragment>
);
2021-08-24 18:29:37 +02:00
}
2022-03-18 15:32:41 +01:00
function colorByTimeToExpire(d: AbsoluteTime): string {
2021-11-15 15:18:58 +01:00
if (d.t_ms === "never") return "rgb(28, 184, 65)";
const months = differenceInMonths(d.t_ms, new Date());
return months > 1 ? "rgb(28, 184, 65)" : "rgb(223, 117, 20)";
2021-08-24 18:29:37 +02:00
}
2022-03-18 15:32:41 +01:00
function daysUntil(d: AbsoluteTime): string {
if (d.t_ms === "never") return "";
2021-08-24 18:29:37 +02:00
const duration = intervalToDuration({
start: d.t_ms,
end: new Date(),
2021-11-15 15:18:58 +01:00
});
2021-08-24 18:29:37 +02:00
const str = formatDuration(duration, {
2021-11-15 15:18:58 +01:00
delimiter: ", ",
2021-08-24 18:29:37 +02:00
format: [
2021-11-15 15:18:58 +01:00
duration?.years
? "years"
: duration?.months
? "months"
: duration?.days
? "days"
: duration.hours
? "hours"
: "minutes",
],
});
return `${str}`;
}
function getStatusTypeOrder(t: ProviderPaymentStatus): number {
return [
ProviderPaymentType.InsufficientBalance,
ProviderPaymentType.TermsChanged,
ProviderPaymentType.Unpaid,
ProviderPaymentType.Paid,
ProviderPaymentType.Pending,
].indexOf(t.type);
}
function getStatusPaidOrder(
a: ProviderPaymentPaid,
b: ProviderPaymentPaid,
): number {
return a.paidUntil.t_ms === "never"
? -1
: b.paidUntil.t_ms === "never"
? 1
: a.paidUntil.t_ms - b.paidUntil.t_ms;
}