aboutsummaryrefslogtreecommitdiff
path: root/src/webex/renderHtml.tsx
blob: 39ff470a21684ca59f35b717618446e522d12e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 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
 */

/**
 * Imports.
 */
import { AmountJson } from "../util/amounts";
import * as Amounts from "../util/amounts";
import { ExchangeWithdrawDetails } from "../types/walletTypes";
import * as i18n from "./i18n";
import React from "react";
import { stringifyTimestamp } from "../util/time";

/**
 * Render amount as HTML, which non-breaking space between
 * decimal value and currency.
 */
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;
  return (
    <span>
      {x}&nbsp;{a.currency}
    </span>
  );
}

export const AmountView = ({
  amount,
}: {
  amount: AmountJson | string;
}): JSX.Element => renderAmount(amount);

/**
 * Abbreviate a string to a given length, and show the full
 * string on hover as a tooltip.
 */
export function abbrev(s: string, n = 5): JSX.Element {
  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;
}

/**
 * Component that shows/hides its children when clicking
 * a heading.
 */
export class Collapsible extends React.Component<
  CollapsibleProps,
  CollapsibleState
> {
  constructor(props: CollapsibleProps) {
    super(props);
    this.state = { collapsed: props.initiallyCollapsed };
  }
  render(): JSX.Element {
    const doOpen = (e: any): void => {
      this.setState({ collapsed: false });
      e.preventDefault();
    };
    const doClose = (e: any): void => {
      this.setState({ collapsed: true });
      e.preventDefault();
    };
    if (this.state.collapsed) {
      return (
        <h2>
          <a className="opener opener-collapsed" href="#" onClick={doOpen}>
            {" "}
            {this.props.title}
          </a>
        </h2>
      );
    }
    return (
      <div>
        <h2>
          <a className="opener opener-open" href="#" onClick={doClose}>
            {" "}
            {this.props.title}
          </a>
        </h2>
        {this.props.children}
      </div>
    );
  }
}

function WireFee(props: {
  s: string;
  rci: ExchangeWithdrawDetails;
}): 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}>
            <td>{stringifyTimestamp(f.endStamp)}</td>
            <td>{renderAmount(f.wireFee)}</td>
            <td>{renderAmount(f.closingFee)}</td>
          </tr>
        ))}
      </tbody>
    </>
  );
}

function AuditorDetailsView(props: {
  rci: ExchangeWithdrawDetails | null;
}): JSX.Element {
  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) {
    return <p>The exchange is not audited by any auditors.</p>;
  }
  return (
    <div>
      {(rci.exchangeInfo.details?.auditors ?? []).map((a) => (
        <div key={a.auditor_pub}>
          <h3>Auditor {a.auditor_url}</h3>
          <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>
        </div>
      ))}
    </div>
  );
}

function FeeDetailsView(props: {
  rci: ExchangeWithdrawDetails | null;
}): JSX.Element {
  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>
      <p>
        Public key:{" "}
        <ExpanderText
          text={rci.exchangeInfo.details?.masterPublicKey ?? "??"}
        />
      </p>
      <p>
        {i18n.str`Withdrawal fees:`} {withdrawFee}
      </p>
      <p>
        {i18n.str`Rounding loss:`} {overhead}
      </p>
      <p>{i18n.str`Earliest expiration (for deposit): ${stringifyTimestamp(
        rci.earliestDepositExpiration,
      )}`}</p>
      <h3>Coin Fees</h3>
      <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>
        </table>
      </div>
      <h3>Wire Fees</h3>
      <div style={{ overflow: "auto" }}>
        <table className="pure-table">
          {Object.keys(rci.wireFees.feesForType).map((s) => (
            <WireFee key={s} s={s} rci={rci} />
          ))}
        </table>
      </div>
    </div>
  );
}

/**
 * Shows details about a withdraw request.
 */
export function WithdrawDetailView(props: {
  rci: ExchangeWithdrawDetails | null;
}): JSX.Element {
  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.
 */
export function ExpanderText({ text }: ExpanderTextProps): JSX.Element {
  return <span>{text}</span>;
}

export interface LoadingButtonProps {
  loading: boolean;
}

export function ProgressButton(
  props: React.PropsWithChildren<LoadingButtonProps> &
    React.DetailedHTMLProps<
      React.ButtonHTMLAttributes<HTMLButtonElement>,
      HTMLButtonElement
    >,
): JSX.Element {
  return (
    <button
      className="pure-button pure-button-primary"
      type="button"
      {...props}
    >
      {props.loading ? (
        <span>
          <object
            className="svg-icon svg-baseline"
            data="/img/spinner-bars.svg"
          />
        </span>
      ) : null}{" "}
      {props.children}
    </button>
  );
}

export function PageLink(
  props: React.PropsWithChildren<{ pageName: string }>,
): JSX.Element {
  const url = chrome.extension.getURL(`/${props.pageName}`);
  return (
    <a
      className="actionLink"
      href={url}
      target="_blank"
      rel="noopener noreferrer"
    >
      {props.children}
    </a>
  );
}