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

265 lines
7.0 KiB
TypeScript
Raw Normal View History

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/>
*/
import { AbsoluteTime } 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,
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,
ButtonPrimary,
ButtonSuccess,
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 03:37:41 +02:00
import { useAsyncAsHook2 } from "../hooks/useAsyncAsHook.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 {
onAddProvider: () => void;
}
export function BackupPage({ onAddProvider }: Props): VNode {
const { i18n } = useTranslationContext();
2022-04-26 03:37:41 +02:00
const status = useAsyncAsHook2(wxApi.getBackupInfo);
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
}
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
providers={providers}
2021-11-15 15:18:58 +01:00
onAddProvider={onAddProvider}
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[];
2021-08-24 18:29:37 +02:00
onAddProvider: () => void;
onSyncAll: () => Promise<void>;
}
2021-11-15 15:18:58 +01:00
export function BackupView({
providers,
onAddProvider,
onSyncAll,
}: 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>
2021-11-15 15:18:58 +01:00
<ButtonSuccess onClick={onAddProvider}>
<i18n.Translate>Add provider</i18n.Translate>
2021-11-15 15:18:58 +01:00
</ButtonSuccess>
</Centered>
2021-08-24 18:29:37 +02:00
)}
</section>
2021-11-15 15:18:58 +01:00
{!!providers.length && (
<footer>
<div />
<div>
<ButtonPrimary onClick={onSyncAll}>
{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
)}
</ButtonPrimary>
2022-02-23 19:18:37 +01:00
<ButtonSuccess onClick={onAddProvider}>
<i18n.Translate>Add provider</i18n.Translate>
2022-02-23 19:18:37 +01:00
</ButtonSuccess>
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
href={Pages.backup_provider_detail.replace(
2021-11-15 15:18:58 +01:00
":pid",
encodeURIComponent(props.id),
)}
>
<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;
}