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-01 20:42:32 +02:00
|
|
|
import { BackupBackupProviderTerms, 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";
|
|
|
|
import { ContractTermsUtil } from "@gnu-taler/taler-wallet-core/src/util/contractTerms";
|
2021-07-01 20:42:32 +02:00
|
|
|
import { formatDuration, intervalToDuration, format } from "date-fns";
|
|
|
|
import { Fragment, VNode } from "preact";
|
2021-07-01 16:33:26 +02:00
|
|
|
import { useRef, useState } from "preact/hooks";
|
|
|
|
import { useBackupStatus } from "../hooks/useProvidersByCurrency";
|
|
|
|
import * as wxApi from "../wxApi";
|
|
|
|
|
|
|
|
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 {
|
2021-07-01 16:33:26 +02:00
|
|
|
const status = useBackupStatus()
|
|
|
|
if (!status) {
|
|
|
|
return <div>Loading...</div>
|
|
|
|
}
|
2021-07-06 17:44:25 +02:00
|
|
|
const idx = parseInt(pid, 10)
|
|
|
|
if (Number.isNaN(idx) || !(status.providers[idx])) {
|
|
|
|
onBack()
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
const info = status.providers[idx];
|
|
|
|
return <ProviderView info={info}
|
2021-07-01 16:33:26 +02:00
|
|
|
onSync={() => { null }}
|
|
|
|
onDelete={() => { null }}
|
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
|
|
|
function Footer() {
|
|
|
|
return <footer style={{ marginTop: 'auto', display: 'flex', flexShrink: 0 }}>
|
2021-07-01 20:42:32 +02:00
|
|
|
<button class="pure-button" onClick={onBack}><i18n.Translate>back</i18n.Translate></button>
|
2021-07-01 16:33:26 +02:00
|
|
|
<div style={{ width: '100%', flexDirection: 'row', justifyContent: 'flex-end', display: 'flex' }}>
|
2021-07-06 17:44:25 +02:00
|
|
|
{info && <button class="pure-button button-destructive" disabled onClick={onDelete}><i18n.Translate>remove</i18n.Translate></button>}
|
|
|
|
{info && <button class="pure-button button-secondary" disabled style={{ marginLeft: 5 }} onClick={onExtend}><i18n.Translate>extend</i18n.Translate></button>}
|
|
|
|
{info && <button class="pure-button button-secondary" disabled style={{ marginLeft: 5 }} onClick={onSync}><i18n.Translate>sync now</i18n.Translate></button>}
|
2021-07-01 16:33:26 +02:00
|
|
|
</div>
|
|
|
|
</footer>
|
|
|
|
}
|
|
|
|
function Error() {
|
|
|
|
if (info?.lastError) {
|
2021-07-01 20:42:32 +02:00
|
|
|
return <Fragment>
|
|
|
|
<div class="errorbox" style={{ marginTop: 10 }} >
|
2021-07-06 17:44:25 +02:00
|
|
|
<div style={{ height: 0, textAlign: 'right', color: 'gray', fontSize: 'small' }}>last time tried {!info.lastAttemptedBackupTimestamp || info.lastAttemptedBackupTimestamp.t_ms === 'never' ? 'never' : format(new Date(info.lastAttemptedBackupTimestamp.t_ms), 'dd/MM/yyyy HH:mm:ss')}</div>
|
2021-07-01 20:42:32 +02:00
|
|
|
<p>{info.lastError.hint}</p>
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
2021-07-01 16:33:26 +02:00
|
|
|
}
|
|
|
|
if (info?.backupProblem) {
|
|
|
|
switch (info.backupProblem.type) {
|
|
|
|
case "backup-conflicting-device":
|
|
|
|
return <div class="errorbox" style={{ marginTop: 10 }}>
|
2021-07-06 17:44:25 +02:00
|
|
|
<p>There is conflict with another backup from <b>{info.backupProblem.otherDeviceId}</b></p>
|
2021-07-01 16:33:26 +02:00
|
|
|
</div>
|
|
|
|
case "backup-unreadable":
|
|
|
|
return <div class="errorbox" style={{ marginTop: 10 }}>
|
|
|
|
<p>Backup is not readable</p>
|
|
|
|
</div>
|
|
|
|
default:
|
|
|
|
return <div class="errorbox" style={{ marginTop: 10 }}>
|
2021-07-06 17:44:25 +02:00
|
|
|
<p>Unknown backup problem: {JSON.stringify(info.backupProblem)}</p>
|
2021-07-01 16:33:26 +02:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
function colorByStatus(status: ProviderPaymentType | undefined) {
|
|
|
|
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)'
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-07-06 17:44:25 +02:00
|
|
|
function descriptionByStatus(status: ProviderPaymentStatus | undefined) {
|
|
|
|
if (!status) return ''
|
|
|
|
switch (status.type) {
|
|
|
|
case ProviderPaymentType.InsufficientBalance:
|
|
|
|
return 'no enough balance to make the payment'
|
|
|
|
case ProviderPaymentType.Unpaid:
|
|
|
|
return 'not pay yet'
|
|
|
|
case ProviderPaymentType.Paid:
|
|
|
|
case ProviderPaymentType.TermsChanged:
|
|
|
|
if (status.paidUntil.t_ms === 'never') {
|
|
|
|
return 'service paid.'
|
|
|
|
} else {
|
|
|
|
return `service paid until ${format(status.paidUntil.t_ms, 'yyyy/MM/dd HH:mm:ss')}`
|
|
|
|
}
|
|
|
|
case ProviderPaymentType.Pending:
|
|
|
|
return ''
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-07-01 16:33:26 +02:00
|
|
|
return (
|
|
|
|
<div style={{ height: 'calc(320px - 34px - 16px)', overflow: 'auto' }}>
|
|
|
|
<style>{`
|
|
|
|
table td {
|
|
|
|
padding: 5px 10px;
|
|
|
|
}
|
|
|
|
`}</style>
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
2021-07-01 20:42:32 +02:00
|
|
|
<section style={{ flex: '1 0 auto', height: 'calc(320px - 34px - 34px - 16px)', overflow: 'auto' }}>
|
2021-07-01 16:33:26 +02:00
|
|
|
<span style={{ padding: 5, display: 'inline-block', backgroundColor: colorByStatus(info?.paymentStatus.type), borderRadius: 5, color: 'white' }}>{info?.paymentStatus.type}</span>
|
2021-07-06 17:44:25 +02:00
|
|
|
{/* {info && <span style={{ float: "right", fontSize: "small", color: "gray", padding: 5 }}>
|
2021-07-01 16:33:26 +02:00
|
|
|
From <b>{info.syncProviderBaseUrl}</b>
|
2021-07-06 17:44:25 +02:00
|
|
|
</span>} */}
|
|
|
|
{info && <div style={{ float: 'right', fontSize: "large", padding: 5 }}>{info.terms?.annualFee} / year</div>}
|
2021-07-01 16:33:26 +02:00
|
|
|
|
|
|
|
<Error />
|
|
|
|
|
2021-07-06 17:44:25 +02:00
|
|
|
<h3>{info?.syncProviderBaseUrl}</h3>
|
2021-07-01 16:33:26 +02:00
|
|
|
<div style={{ display: "flex", flexDirection: "row", justifyContent: "space-between", }}>
|
2021-07-06 17:44:25 +02:00
|
|
|
<div>{daysSince(info?.lastSuccessfulBackupTimestamp)} </div>
|
2021-07-01 16:33:26 +02:00
|
|
|
</div>
|
|
|
|
|
2021-07-06 17:44:25 +02:00
|
|
|
<p>{descriptionByStatus(info?.paymentStatus)}</p>
|
|
|
|
|
|
|
|
{info?.paymentStatus.type === ProviderPaymentType.TermsChanged && <div>
|
|
|
|
<p>terms has changed, extending the service will imply accepting the new terms of service</p>
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<td></td>
|
|
|
|
<td>old</td>
|
|
|
|
<td> -></td>
|
|
|
|
<td>new</td>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
|
|
|
|
<tr>
|
|
|
|
<td>fee</td>
|
|
|
|
<td>{info.paymentStatus.oldTerms.annualFee}</td>
|
|
|
|
<td>-></td>
|
|
|
|
<td>{info.paymentStatus.newTerms.annualFee}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>storage</td>
|
|
|
|
<td>{info.paymentStatus.oldTerms.storageLimitInMegabytes}</td>
|
|
|
|
<td>-></td>
|
|
|
|
<td>{info.paymentStatus.newTerms.storageLimitInMegabytes}</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>}
|
|
|
|
|
2021-07-01 16:33:26 +02:00
|
|
|
</section>
|
|
|
|
<Footer />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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: [
|
|
|
|
duration?.years ? 'years' : (
|
|
|
|
duration?.months ? 'months' : (
|
|
|
|
duration?.days ? 'days' : (
|
|
|
|
duration?.hours ? 'hours' : (
|
|
|
|
duration?.minutes ? 'minutes' : 'seconds'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
})
|
|
|
|
return `synced ${str} ago`
|
|
|
|
}
|