wallet-core/packages/taler-wallet-webextension/src/popup/ProviderDetailPage.tsx

189 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-07-01 16:33:26 +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/>
*/
2021-07-10 04:15:49 +02:00
import { i18n, Timestamp } from "@gnu-taler/taler-util";
2021-07-06 17:44:25 +02:00
import { ProviderInfo, ProviderPaymentStatus, ProviderPaymentType } from "@gnu-taler/taler-wallet-core";
2021-07-10 04:15:49 +02:00
import { format, formatDuration, intervalToDuration } from "date-fns";
2021-07-01 20:42:32 +02:00
import { Fragment, VNode } from "preact";
2021-07-12 19:47:13 +02:00
import { ErrorMessage } from "../components/ErrorMessage";
import { Button, ButtonDestructive, ButtonPrimary, PaymentStatus, PopupBox, SmallTextLight } from "../components/styled";
2021-07-10 04:15:49 +02:00
import { useProviderStatus } from "../hooks/useProviderStatus";
2021-07-01 16:33:26 +02:00
interface Props {
2021-07-06 17:44:25 +02:00
pid: string;
2021-07-01 20:42:32 +02:00
onBack: () => void;
2021-07-01 16:33:26 +02:00
}
2021-07-06 17:44:25 +02:00
export function ProviderDetailPage({ pid, onBack }: Props): VNode {
const status = useProviderStatus(pid)
2021-07-01 16:33:26 +02:00
if (!status) {
2021-07-26 16:27:56 +02:00
return <div><i18n.Translate>Loading...</i18n.Translate></div>
2021-07-01 16:33:26 +02:00
}
if (!status.info) {
2021-07-06 17:44:25 +02:00
onBack()
return <div />
}
return <ProviderView info={status.info}
onSync={status.sync}
onDelete={() => status.remove().then(onBack) }
2021-07-01 20:42:32 +02:00
onBack={onBack}
2021-07-06 17:44:25 +02:00
onExtend={() => { null }}
2021-07-01 16:33:26 +02:00
/>;
}
export interface ViewProps {
2021-07-06 17:44:25 +02:00
info: ProviderInfo;
2021-07-01 16:33:26 +02:00
onDelete: () => void;
onSync: () => void;
onBack: () => void;
2021-07-06 17:44:25 +02:00
onExtend: () => void;
2021-07-01 16:33:26 +02:00
}
2021-07-06 17:44:25 +02:00
export function ProviderView({ info, onDelete, onSync, onBack, onExtend }: ViewProps): VNode {
2021-07-01 16:33:26 +02:00
return (
2021-07-12 19:47:13 +02:00
<PopupBox>
<header>
<PaymentStatus color={colorByStatus(info.paymentStatus.type)}>{info.paymentStatus.type}</PaymentStatus>
{info.terms && <div>{info.terms.annualFee} / year</div>}
</header>
<section>
<Error info={info} />
<h3>{info.name} <SmallTextLight>{info.syncProviderBaseUrl}</SmallTextLight></h3>
2021-07-12 19:47:13 +02:00
<p>{daysSince(info?.lastSuccessfulBackupTimestamp)} </p>
<p>{descriptionByStatus(info.paymentStatus)}</p>
{info.paymentStatus.type === ProviderPaymentType.TermsChanged && <div>
2021-07-26 16:27:56 +02:00
<p><i18n.Translate>terms has changed, extending the service will imply accepting the new terms of service</i18n.Translate></p>
2021-07-12 19:47:13 +02:00
<table>
<thead>
<tr>
<td></td>
2021-07-26 16:27:56 +02:00
<td><i18n.Translate>old</i18n.Translate></td>
2021-07-12 19:47:13 +02:00
<td> -&gt;</td>
2021-07-26 16:27:56 +02:00
<td><i18n.Translate>new</i18n.Translate></td>
2021-07-12 19:47:13 +02:00
</tr>
</thead>
<tbody>
<tr>
2021-07-26 16:27:56 +02:00
<td><i18n.Translate>fee</i18n.Translate></td>
2021-07-12 19:47:13 +02:00
<td>{info.paymentStatus.oldTerms.annualFee}</td>
<td>-&gt;</td>
<td>{info.paymentStatus.newTerms.annualFee}</td>
</tr>
<tr>
2021-07-26 16:27:56 +02:00
<td><i18n.Translate>storage</i18n.Translate></td>
2021-07-12 19:47:13 +02:00
<td>{info.paymentStatus.oldTerms.storageLimitInMegabytes}</td>
<td>-&gt;</td>
<td>{info.paymentStatus.newTerms.storageLimitInMegabytes}</td>
</tr>
</tbody>
</table>
</div>}
</section>
<footer>
<Button onClick={onBack}><i18n.Translate> &lt; back</i18n.Translate></Button>
<div>
<ButtonDestructive onClick={onDelete}><i18n.Translate>remove</i18n.Translate></ButtonDestructive>
2021-07-12 19:47:13 +02:00
<ButtonPrimary disabled onClick={onExtend}><i18n.Translate>extend</i18n.Translate></ButtonPrimary>
<ButtonPrimary onClick={onSync}><i18n.Translate>sync now</i18n.Translate></ButtonPrimary>
</div>
</footer>
</PopupBox>
2021-07-01 16:33:26 +02:00
)
}
function daysSince(d?: Timestamp) {
if (!d || d.t_ms === 'never') return 'never synced'
const duration = intervalToDuration({
start: d.t_ms,
end: new Date(),
})
const str = formatDuration(duration, {
delimiter: ', ',
format: [
2021-07-26 16:27:56 +02:00
duration?.years ? i18n.str`years` : (
duration?.months ? i18n.str`months` : (
duration?.days ? i18n.str`days` : (
duration?.hours ? i18n.str`hours` : (
duration?.minutes ? i18n.str`minutes` : i18n.str`seconds`
2021-07-01 16:33:26 +02:00
)
)
)
)
]
})
return `synced ${str} ago`
}
function Error({ info }: { info: ProviderInfo }) {
if (info.lastError) {
return <ErrorMessage title={info.lastError.hint} />
}
if (info.backupProblem) {
switch (info.backupProblem.type) {
case "backup-conflicting-device":
return <ErrorMessage title={<Fragment>
2021-07-26 16:27:56 +02:00
<i18n.Translate>There is conflict with another backup from <b>{info.backupProblem.otherDeviceId}</b></i18n.Translate>
</Fragment>} />
case "backup-unreadable":
return <ErrorMessage title="Backup is not readable" />
default:
return <ErrorMessage title={<Fragment>
2021-07-26 16:27:56 +02:00
<i18n.Translate>Unknown backup problem: {JSON.stringify(info.backupProblem)}</i18n.Translate>
</Fragment>} />
}
}
return null
}
function colorByStatus(status: ProviderPaymentType) {
switch (status) {
case ProviderPaymentType.InsufficientBalance:
return 'rgb(223, 117, 20)'
case ProviderPaymentType.Unpaid:
return 'rgb(202, 60, 60)'
case ProviderPaymentType.Paid:
return 'rgb(28, 184, 65)'
case ProviderPaymentType.Pending:
return 'gray'
case ProviderPaymentType.InsufficientBalance:
return 'rgb(202, 60, 60)'
case ProviderPaymentType.TermsChanged:
return 'rgb(202, 60, 60)'
}
}
function descriptionByStatus(status: ProviderPaymentStatus) {
switch (status.type) {
case ProviderPaymentType.InsufficientBalance:
2021-07-26 16:27:56 +02:00
return i18n.str`no enough balance to make the payment`
case ProviderPaymentType.Unpaid:
2021-07-26 16:27:56 +02:00
return i18n.str`not paid yet`
case ProviderPaymentType.Paid:
case ProviderPaymentType.TermsChanged:
if (status.paidUntil.t_ms === 'never') {
2021-07-26 16:27:56 +02:00
return i18n.str`service paid`
} else {
2021-07-26 16:27:56 +02:00
return i18n.str`service paid until ${format(status.paidUntil.t_ms, 'yyyy/MM/dd HH:mm:ss')}`
}
case ProviderPaymentType.Pending:
return ''
}
}