2021-08-24 18:29:37 +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/>
|
|
|
|
*/
|
|
|
|
|
2022-03-29 14:58:06 +02:00
|
|
|
import { AbsoluteTime } 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,
|
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-03-29 04:41:07 +02:00
|
|
|
import { Loading } from "../components/Loading.js";
|
|
|
|
import { LoadingError } from "../components/LoadingError.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-03-29 04:41:07 +02:00
|
|
|
} from "../components/styled/index.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 { Pages } from "../NavigationBar.js";
|
|
|
|
import * as wxApi from "../wxApi.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 BackupPage({ onAddProvider }: Props): VNode {
|
2022-03-14 19:20:32 +01:00
|
|
|
const { i18n } = useTranslationContext();
|
2022-04-26 04:07:31 +02:00
|
|
|
const status = useAsyncAsHook(wxApi.getBackupInfo);
|
2021-08-24 18:29:37 +02:00
|
|
|
if (!status) {
|
2022-01-20 17:13:53 +01:00
|
|
|
return <Loading />;
|
|
|
|
}
|
|
|
|
if (status.hasError) {
|
|
|
|
return (
|
2022-02-23 19:18:37 +01:00
|
|
|
<LoadingError
|
2022-02-23 19:44:14 +01:00
|
|
|
title={<i18n.Translate>Could not load backup providers</i18n.Translate>}
|
2022-02-23 19:18:37 +01:00
|
|
|
error={status}
|
|
|
|
/>
|
2022-01-20 17:13:53 +01:00
|
|
|
);
|
2021-08-24 18:29:37 +02:00
|
|
|
}
|
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)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
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-01-20 17:13:53 +01:00
|
|
|
onSyncAll={wxApi.syncAllProviders}
|
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>;
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:18:58 +01:00
|
|
|
export function BackupView({
|
|
|
|
providers,
|
|
|
|
onAddProvider,
|
|
|
|
onSyncAll,
|
|
|
|
}: 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>
|
|
|
|
<div />
|
|
|
|
<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 ? (
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Sync all backups</i18n.Translate>
|
2021-11-15 15:18:58 +01:00
|
|
|
) : (
|
2022-02-23 19:44:14 +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}>
|
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;
|
|
|
|
}
|