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,
|
2022-01-20 17:13:53 +01:00
|
|
|
ProviderPaymentPaid,
|
2021-11-15 15:18:58 +01:00
|
|
|
ProviderPaymentStatus,
|
2022-01-20 17:13:53 +01:00
|
|
|
ProviderPaymentType,
|
2022-10-20 19:54:29 +02:00
|
|
|
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";
|
2023-01-09 12:38:48 +01:00
|
|
|
import { AlertView } from "../components/CurrentAlerts.js";
|
2022-03-29 04:41:07 +02:00
|
|
|
import { Loading } from "../components/Loading.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,
|
2022-10-20 19:54:29 +02:00
|
|
|
WarningBox,
|
2022-03-29 04:41:07 +02:00
|
|
|
} from "../components/styled/index.js";
|
2023-01-09 12:38:48 +01:00
|
|
|
import { alertFromError } from "../context/alert.js";
|
2022-12-15 21:11:24 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-20 19:54:29 +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 {
|
2022-03-14 19:20:32 +01:00
|
|
|
const { i18n } = useTranslationContext();
|
2022-12-15 21:11:24 +01:00
|
|
|
const api = useBackendContext();
|
2022-10-25 17:23:08 +02:00
|
|
|
const status = useAsyncAsHook(() =>
|
2022-12-15 21:11:24 +01:00
|
|
|
api.wallet.call(WalletApiOperation.GetBackupInfo, {}),
|
2022-10-25 17:23:08 +02:00
|
|
|
);
|
2022-10-20 19:54:29 +02:00
|
|
|
const [recoveryInfo, setRecoveryInfo] = useState<string>("");
|
2021-08-24 18:29:37 +02:00
|
|
|
if (!status) {
|
2022-01-20 17:13:53 +01:00
|
|
|
return <Loading />;
|
|
|
|
}
|
|
|
|
if (status.hasError) {
|
|
|
|
return (
|
2023-01-09 12:38:48 +01:00
|
|
|
<AlertView
|
|
|
|
alert={alertFromError(
|
|
|
|
i18n.str`Could not load backup providers`,
|
|
|
|
status,
|
|
|
|
)}
|
2022-02-23 19:18:37 +01:00
|
|
|
/>
|
2022-01-20 17:13:53 +01:00
|
|
|
);
|
2021-08-24 18:29:37 +02:00
|
|
|
}
|
2022-01-20 17:13:53 +01:00
|
|
|
|
2022-10-20 19:54:29 +02:00
|
|
|
async function getRecoveryInfo(): Promise<void> {
|
2022-12-15 21:11:24 +01:00
|
|
|
const r = await api.wallet.call(
|
2022-10-25 17:23:08 +02:00
|
|
|
WalletApiOperation.ExportBackupRecovery,
|
|
|
|
{},
|
|
|
|
);
|
2022-10-20 19:54:29 +02:00
|
|
|
const str = constructRecoveryUri(r);
|
|
|
|
setRecoveryInfo(str);
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:13:53 +01:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-10-20 19:54:29 +02:00
|
|
|
if (recoveryInfo) {
|
|
|
|
return (
|
|
|
|
<ShowRecoveryInfo
|
|
|
|
info={recoveryInfo}
|
|
|
|
onClose={async () => setRecoveryInfo("")}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:18:58 +01:00
|
|
|
return (
|
|
|
|
<BackupView
|
2022-01-20 17:13:53 +01:00
|
|
|
providers={providers}
|
2021-11-15 15:18:58 +01:00
|
|
|
onAddProvider={onAddProvider}
|
2022-10-25 17:23:08 +02:00
|
|
|
onSyncAll={async () =>
|
2022-12-15 21:11:24 +01:00
|
|
|
api.wallet.call(WalletApiOperation.RunBackupCycle, {}).then()
|
2022-10-25 17:23:08 +02:00
|
|
|
}
|
2022-10-20 19:54:29 +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>;
|
2022-10-20 19:54:29 +02:00
|
|
|
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,
|
2022-10-20 19:54:29 +02:00
|
|
|
onShowInfo,
|
2021-11-15 15:18:58 +01:00
|
|
|
}: ViewProps): VNode {
|
2022-03-14 19:20:32 +01:00
|
|
|
const { i18n } = useTranslationContext();
|
2021-08-24 18:29:37 +02:00
|
|
|
return (
|
2021-11-19 18:51:27 +01:00
|
|
|
<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>
|
2022-02-23 19:44:14 +01:00
|
|
|
<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}>
|
2022-02-23 19:44:14 +01:00
|
|
|
<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>
|
2022-10-20 19:54:29 +02:00
|
|
|
<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}>
|
2023-01-09 12:38:48 +01:00
|
|
|
{providers.length > 1
|
|
|
|
? i18n.str`Sync all backups`
|
|
|
|
: i18n.str`Sync now`}
|
2022-06-01 20:47:47 +02:00
|
|
|
</Button>
|
|
|
|
<Button variant="contained" color="success" onClick={onAddProvider}>
|
2022-02-23 19:44:14 +01:00
|
|
|
<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>
|
|
|
|
)}
|
2021-11-19 18:51:27 +01:00
|
|
|
</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 {
|
2022-03-14 19:20:32 +01:00
|
|
|
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 }}>
|
2022-02-23 19:44:14 +01:00
|
|
|
<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 }}>
|
2022-02-23 19:44:14 +01:00
|
|
|
<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 {
|
2022-03-14 19:20:32 +01:00
|
|
|
const { i18n } = useTranslationContext();
|
2021-11-15 15:18:58 +01:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2022-02-23 19:18:37 +01:00
|
|
|
<CenteredText>
|
2022-02-23 19:44:14 +01:00
|
|
|
<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 {
|
2021-11-19 18:51:27 +01:00
|
|
|
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}`;
|
|
|
|
}
|
2022-01-20 17:13:53 +01:00
|
|
|
|
2022-03-29 14:58:06 +02:00
|
|
|
function getStatusTypeOrder(t: ProviderPaymentStatus): number {
|
2022-01-20 17:13:53 +01:00
|
|
|
return [
|
|
|
|
ProviderPaymentType.InsufficientBalance,
|
|
|
|
ProviderPaymentType.TermsChanged,
|
|
|
|
ProviderPaymentType.Unpaid,
|
|
|
|
ProviderPaymentType.Paid,
|
|
|
|
ProviderPaymentType.Pending,
|
|
|
|
].indexOf(t.type);
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:58:06 +02:00
|
|
|
function getStatusPaidOrder(
|
|
|
|
a: ProviderPaymentPaid,
|
|
|
|
b: ProviderPaymentPaid,
|
|
|
|
): number {
|
2022-01-20 17:13:53 +01:00
|
|
|
return a.paidUntil.t_ms === "never"
|
|
|
|
? -1
|
|
|
|
: b.paidUntil.t_ms === "never"
|
|
|
|
? 1
|
|
|
|
: a.paidUntil.t_ms - b.paidUntil.t_ms;
|
|
|
|
}
|