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

342 lines
8.2 KiB
TypeScript
Raw Normal View History

2016-09-29 16:31:55 +02:00
/*
This file is part of TALER
(C) 2016 INRIA
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/>
*/
/**
* Helpers functions to render Taler-related data structures to HTML.
*
* @author Florian Dold
*/
2017-05-24 16:45:57 +02:00
/**
* Imports.
*/
2020-08-12 09:11:00 +02:00
import { AmountJson, Amounts, stringifyTimestamp, ExchangeWithdrawDetails } from "taler-wallet-core";
import * as i18n from "./i18n";
import React from "react";
2016-09-29 16:31:55 +02:00
2017-06-04 17:56:55 +02:00
/**
* Render amount as HTML, which non-breaking space between
* decimal value and currency.
*/
2020-04-06 20:02:01 +02:00
export function renderAmount(amount: AmountJson | string): JSX.Element {
let a;
if (typeof amount === "string") {
a = Amounts.parse(amount);
} else {
a = amount;
}
if (!a) {
return <span>(invalid amount)</span>;
}
const x = a.value + a.fraction / Amounts.fractionalBase;
2019-09-05 16:23:54 +02:00
return (
<span>
{x}&nbsp;{a.currency}
</span>
);
2017-06-04 17:56:55 +02:00
}
2020-04-06 20:02:01 +02:00
export const AmountView = ({
amount,
}: {
amount: AmountJson | string;
}): JSX.Element => renderAmount(amount);
2017-06-04 17:56:55 +02:00
2017-05-24 16:45:57 +02:00
/**
* Abbreviate a string to a given length, and show the full
* string on hover as a tooltip.
*/
2020-04-06 20:02:01 +02:00
export function abbrev(s: string, n = 5): JSX.Element {
2016-10-12 02:55:53 +02:00
let sAbbrev = s;
if (s.length > n) {
sAbbrev = s.slice(0, n) + "..";
}
return (
<span className="abbrev" title={s}>
{sAbbrev}
</span>
);
}
interface CollapsibleState {
collapsed: boolean;
}
interface CollapsibleProps {
initiallyCollapsed: boolean;
title: string;
}
2017-10-15 19:28:35 +02:00
/**
* Component that shows/hides its children when clicking
* a heading.
*/
2019-09-05 16:23:54 +02:00
export class Collapsible extends React.Component<
CollapsibleProps,
CollapsibleState
> {
constructor(props: CollapsibleProps) {
super(props);
this.state = { collapsed: props.initiallyCollapsed };
}
2020-04-06 20:02:01 +02:00
render(): JSX.Element {
const doOpen = (e: any): void => {
2019-09-05 16:23:54 +02:00
this.setState({ collapsed: false });
e.preventDefault();
};
2020-04-06 20:02:01 +02:00
const doClose = (e: any): void => {
2019-09-05 16:23:54 +02:00
this.setState({ collapsed: true });
e.preventDefault();
};
if (this.state.collapsed) {
2019-09-05 16:23:54 +02:00
return (
<h2>
<a className="opener opener-collapsed" href="#" onClick={doOpen}>
{" "}
2019-09-05 16:23:54 +02:00
{this.props.title}
</a>
</h2>
);
}
return (
<div>
2019-09-05 16:23:54 +02:00
<h2>
<a className="opener opener-open" href="#" onClick={doClose}>
{" "}
2019-09-05 16:23:54 +02:00
{this.props.title}
</a>
</h2>
{this.props.children}
</div>
);
}
}
2017-11-30 04:07:36 +01:00
2020-04-07 10:07:32 +02:00
function WireFee(props: {
s: string;
2020-08-12 09:11:00 +02:00
rci: ExchangeWithdrawDetails;
2020-04-07 10:07:32 +02:00
}): JSX.Element {
return (
<>
<thead>
<tr>
<th colSpan={3}>Wire Method {props.s}</th>
</tr>
<tr>
<th>Applies Until</th>
<th>Wire Fee</th>
<th>Closing Fee</th>
</tr>
</thead>
<tbody>
{props.rci.wireFees.feesForType[props.s].map((f) => (
<tr key={f.sig}>
2020-08-12 09:11:00 +02:00
<td>{stringifyTimestamp(f.endStamp)}</td>
2020-04-07 10:07:32 +02:00
<td>{renderAmount(f.wireFee)}</td>
<td>{renderAmount(f.closingFee)}</td>
</tr>
))}
</tbody>
</>
);
}
2019-09-05 16:23:54 +02:00
function AuditorDetailsView(props: {
2020-08-12 09:11:00 +02:00
rci: ExchangeWithdrawDetails | null;
2019-09-05 16:23:54 +02:00
}): JSX.Element {
2017-11-30 04:07:36 +01:00
const rci = props.rci;
console.log("rci", rci);
if (!rci) {
return (
<p>
Details will be displayed when a valid exchange provider URL is entered.
</p>
);
}
if ((rci.exchangeInfo.details?.auditors ?? []).length === 0) {
2019-09-05 16:23:54 +02:00
return <p>The exchange is not audited by any auditors.</p>;
2017-11-30 04:07:36 +01:00
}
return (
<div>
2020-03-30 12:39:32 +02:00
{(rci.exchangeInfo.details?.auditors ?? []).map((a) => (
2020-04-07 10:07:32 +02:00
<div key={a.auditor_pub}>
2017-11-30 04:07:36 +01:00
<h3>Auditor {a.auditor_url}</h3>
2019-09-05 16:23:54 +02:00
<p>
Public key: <ExpanderText text={a.auditor_pub} />
</p>
<p>
Trusted:{" "}
{rci.trustedAuditorPubs.indexOf(a.auditor_pub) >= 0 ? "yes" : "no"}
</p>
<p>
Audits {a.denomination_keys.length} of {rci.numOfferedDenoms}{" "}
denominations
</p>
2017-11-30 04:07:36 +01:00
</div>
))}
</div>
);
}
2019-09-05 16:23:54 +02:00
function FeeDetailsView(props: {
2020-08-12 09:11:00 +02:00
rci: ExchangeWithdrawDetails | null;
2019-09-05 16:23:54 +02:00
}): JSX.Element {
2017-11-30 04:07:36 +01:00
const rci = props.rci;
if (!rci) {
return (
<p>
Details will be displayed when a valid exchange provider URL is entered.
</p>
);
}
const denoms = rci.selectedDenoms;
const withdrawFee = renderAmount(rci.withdrawFee);
const overhead = renderAmount(rci.overhead);
return (
<div>
<h3>Overview</h3>
2019-09-05 16:23:54 +02:00
<p>
2020-03-30 12:39:32 +02:00
Public key:{" "}
<ExpanderText
text={rci.exchangeInfo.details?.masterPublicKey ?? "??"}
/>
2019-09-05 16:23:54 +02:00
</p>
<p>
{i18n.str`Withdrawal fees:`} {withdrawFee}
</p>
<p>
{i18n.str`Rounding loss:`} {overhead}
</p>
2020-08-12 09:11:00 +02:00
<p>{i18n.str`Earliest expiration (for deposit): ${stringifyTimestamp(
2020-03-30 12:39:32 +02:00
rci.earliestDepositExpiration,
)}`}</p>
2017-11-30 04:07:36 +01:00
<h3>Coin Fees</h3>
2019-09-05 16:23:54 +02:00
<div style={{ overflow: "auto" }}>
<table className="pure-table">
<thead>
<tr>
<th>{i18n.str`# Coins`}</th>
<th>{i18n.str`Value`}</th>
<th>{i18n.str`Withdraw Fee`}</th>
<th>{i18n.str`Refresh Fee`}</th>
<th>{i18n.str`Deposit Fee`}</th>
</tr>
</thead>
<tbody>
{denoms.selectedDenoms.map((ds) => {
return (
<tr key={ds.denom.denomPub}>
<td>{ds.count + "x"}</td>
<td>{renderAmount(ds.denom.value)}</td>
<td>{renderAmount(ds.denom.feeWithdraw)}</td>
<td>{renderAmount(ds.denom.feeRefresh)}</td>
<td>{renderAmount(ds.denom.feeDeposit)}</td>
</tr>
);
})}
</tbody>
2019-09-05 16:23:54 +02:00
</table>
</div>
2017-11-30 04:07:36 +01:00
<h3>Wire Fees</h3>
2019-09-05 16:23:54 +02:00
<div style={{ overflow: "auto" }}>
<table className="pure-table">
2020-04-07 10:07:32 +02:00
{Object.keys(rci.wireFees.feesForType).map((s) => (
<WireFee key={s} s={s} rci={rci} />
))}
</table>
</div>
2017-11-30 04:07:36 +01:00
</div>
);
}
/**
* Shows details about a withdraw request.
*/
2019-09-05 16:23:54 +02:00
export function WithdrawDetailView(props: {
2020-08-12 09:11:00 +02:00
rci: ExchangeWithdrawDetails | null;
2019-09-05 16:23:54 +02:00
}): JSX.Element {
2017-11-30 04:07:36 +01:00
const rci = props.rci;
return (
<div>
<Collapsible initiallyCollapsed={true} title="Fee and Spending Details">
<FeeDetailsView rci={rci} />
</Collapsible>
<Collapsible initiallyCollapsed={true} title="Auditor Details">
<AuditorDetailsView rci={rci} />
</Collapsible>
</div>
);
}
interface ExpanderTextProps {
text: string;
}
/**
* Show a heading with a toggle to show/hide the expandable content.
*/
2020-04-06 20:02:01 +02:00
export function ExpanderText({ text }: ExpanderTextProps): JSX.Element {
return <span>{text}</span>;
}
2019-08-31 11:49:36 +02:00
export interface LoadingButtonProps {
loading: boolean;
}
export function ProgressButton(
2019-09-05 16:23:54 +02:00
props: React.PropsWithChildren<LoadingButtonProps> &
React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>,
2020-04-06 20:02:01 +02:00
): JSX.Element {
2019-08-31 11:49:36 +02:00
return (
<button
className="pure-button pure-button-primary"
type="button"
{...props}
>
2019-09-05 16:23:54 +02:00
{props.loading ? (
<span>
<object
className="svg-icon svg-baseline"
data="/img/spinner-bars.svg"
/>
</span>
) : null}{" "}
2019-08-31 11:49:36 +02:00
{props.children}
</button>
);
}
2020-04-06 20:02:01 +02:00
export function PageLink(
props: React.PropsWithChildren<{ pageName: string }>,
): JSX.Element {
2020-04-06 21:53:29 +02:00
const url = chrome.extension.getURL(`/${props.pageName}`);
2019-09-05 16:23:54 +02:00
return (
2020-04-07 10:07:32 +02:00
<a
className="actionLink"
href={url}
target="_blank"
rel="noopener noreferrer"
>
2019-09-05 16:23:54 +02:00
{props.children}
</a>
);
}