/* This file is part of GNU Taler (C) 2022 Taler Systems S.A. GNU 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. GNU 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 GNU Taler; see the file COPYING. If not, see */ import { AbsoluteTime, ProviderInfo, ProviderPaymentPaid, ProviderPaymentStatus, ProviderPaymentType, stringifyRestoreUri, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { differenceInMonths, formatDuration, intervalToDuration, } from "date-fns"; import { Fragment, VNode, h } from "preact"; import { useEffect, useState } from "preact/hooks"; import { Pages } from "../NavigationBar.js"; import { ErrorAlertView } from "../components/CurrentAlerts.js"; import { Loading } from "../components/Loading.js"; import { QR } from "../components/QR.js"; import { BoldLight, Centered, CenteredBoldText, CenteredText, RowBorderGray, SmallLightText, SmallText, WarningBox, } from "../components/styled/index.js"; import { alertFromError } from "../context/alert.js"; import { useBackendContext } from "../context/backend.js"; import { useAsyncAsHook } from "../hooks/useAsyncAsHook.js"; import { Button } from "../mui/Button.js"; interface Props { onAddProvider: () => Promise; } export function ShowRecoveryInfo({ info, onClose, }: { info: string; onClose: () => Promise; }): VNode { const [display, setDisplay] = useState(false); const [copied, setCopied] = useState(false); async function copyText(): Promise { navigator.clipboard.writeText(info); setCopied(true); } useEffect(() => { if (copied) { setTimeout(() => { setCopied(false); }, 1000); } }, [copied]); return (

Wallet Recovery

Do not share this QR or URI with anyone

The qr code can be scanned by another wallet to keep synchronized with this wallet.

{display && }

You can also use the string version

); } export function BackupPage({ onAddProvider }: Props): VNode { const { i18n } = useTranslationContext(); const api = useBackendContext(); const status = useAsyncAsHook(() => api.wallet.call(WalletApiOperation.GetBackupInfo, {}), ); const [recoveryInfo, setRecoveryInfo] = useState(""); if (!status) { return ; } if (status.hasError) { return ( ); } async function getRecoveryInfo(): Promise { const r = await api.wallet.call( WalletApiOperation.ExportBackupRecovery, {}, ); const str = stringifyRestoreUri({ walletRootPriv: r.walletRootPriv, providers: r.providers.map((p) => p.url), }); setRecoveryInfo(str); } 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) ); }); if (recoveryInfo) { return ( setRecoveryInfo("")} /> ); } return ( api.wallet.call(WalletApiOperation.RunBackupCycle, {}).then() } onShowInfo={getRecoveryInfo} /> ); } export interface ViewProps { providers: ProviderInfo[]; onAddProvider: () => Promise; onSyncAll: () => Promise; onShowInfo: () => Promise; } export function BackupView({ providers, onAddProvider, onSyncAll, onShowInfo, }: ViewProps): VNode { const { i18n } = useTranslationContext(); return (
{providers.map((provider, idx) => ( ))} {!providers.length && ( No backup providers configured )}
{!!providers.length && (
)}
); } interface TransactionLayoutProps { status: ProviderPaymentStatus; timestamp?: AbsoluteTime; title: string; id: string; active: boolean; } function BackupLayout(props: TransactionLayoutProps): VNode { const { i18n } = useTranslationContext(); const date = !props.timestamp ? undefined : new Date(props.timestamp.t_ms); const dateStr = date?.toLocaleString([], { dateStyle: "medium", timeStyle: "short", } as any); return (
{props.title} {dateStr && ( Last synced: {dateStr} )} {!dateStr && ( Not synced )}
{props.status?.type === "paid" ? ( ) : (
{props.status.type}
)}
); } function ExpirationText({ until }: { until: AbsoluteTime }): VNode { const { i18n } = useTranslationContext(); return ( Expires in {" "} {daysUntil(until)}{" "} ); } function colorByTimeToExpire(d: AbsoluteTime): string { 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)"; } function daysUntil(d: AbsoluteTime): string { if (d.t_ms === "never") return ""; 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" : "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; }