summaryrefslogtreecommitdiff
path: root/packages/bank-ui/src/pages/admin/AdminHome.tsx
blob: acae09b40e6eca64d6502a12f79f664a1cdd1df3 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
 This file is part of GNU Taler
 (C) 2022-2024 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 <http://www.gnu.org/licenses/>
 */
import {
  AbsoluteTime,
  AmountString,
  Amounts,
  CurrencySpecification,
  HttpStatusCode,
  TalerCorebankApi,
  TalerError,
  assertUnreachable,
} from "@gnu-taler/taler-util";
import {
  Attention,
  RouteDefinition,
  useBankCoreApiContext,
  useTranslationContext,
} from "@gnu-taler/web-util/browser";
import {
  format,
  sub
} from "date-fns";
import { Fragment, VNode, h } from "preact";
import { useState } from "preact/hooks";
import { ErrorLoadingWithDebug } from "../../components/ErrorLoadingWithDebug.js";
import { Transactions } from "../../components/Transactions/index.js";
import { useConversionInfo, useLastMonitorInfo } from "../../hooks/regional.js";
import { RenderAmount } from "../PaytoWireTransferForm.js";
import { WireTransfer } from "../WireTransfer.js";
import { AccountList } from "./AccountList.js";

/**
 * Query account information and show QR code if there is pending withdrawal
 */
interface Props {
  routeCreate: RouteDefinition;
  routeDownloadStats: RouteDefinition;
  routeCreateWireTransfer: RouteDefinition<{
    account?: string;
    subject?: string;
    amount?: string;
  }>;

  routeShowAccount: RouteDefinition<{ account: string }>;
  routeRemoveAccount: RouteDefinition<{ account: string }>;
  routeUpdatePasswordAccount: RouteDefinition<{ account: string }>;
  routeShowCashoutsAccount: RouteDefinition<{ account: string }>;
  onAuthorizationRequired: () => void;
}
export function AdminHome({
  routeCreate,
  routeRemoveAccount,
  routeShowAccount,
  routeUpdatePasswordAccount,
  routeDownloadStats,
  routeCreateWireTransfer,
  onAuthorizationRequired,
}: Props): VNode {
  return (
    <Fragment>
      <Metrics routeDownloadStats={routeDownloadStats} />
      <WireTransfer
        routeHere={routeCreateWireTransfer}
        onAuthorizationRequired={onAuthorizationRequired}
      />
      <Transactions
        account="admin"
        routeCreateWireTransfer={routeCreateWireTransfer}
      />
      <AccountList
        routeCreate={routeCreate}
        routeRemoveAccount={routeRemoveAccount}
        routeShowAccount={routeShowAccount}
        routeUpdatePasswordAccount={routeUpdatePasswordAccount}
      />
    </Fragment>
  );
}

function getDateForTimeframe(
  date: AbsoluteTime,
  timeframe: TalerCorebankApi.MonitorTimeframeParam,
  locale: Locale,
): string {
  if (date.t_ms === "never") return "--";
  switch (timeframe) {
    case TalerCorebankApi.MonitorTimeframeParam.hour:
      return `${format(date.t_ms, "HH", { locale })}hs`;
    case TalerCorebankApi.MonitorTimeframeParam.day:
      return format(date.t_ms, "EEEE", { locale });
    case TalerCorebankApi.MonitorTimeframeParam.month:
      return format(date.t_ms, "MMMM", { locale });
    case TalerCorebankApi.MonitorTimeframeParam.year:
      return format(date.t_ms, "yyyy", { locale });
    case TalerCorebankApi.MonitorTimeframeParam.decade:
      return format(date.t_ms, "yyyy", { locale });
  }
  assertUnreachable(timeframe);
}

export function getTimeframesForDate(
  time: Date,
  timeframe: TalerCorebankApi.MonitorTimeframeParam,
): { current: AbsoluteTime; previous: AbsoluteTime } {
  switch (timeframe) {
    case TalerCorebankApi.MonitorTimeframeParam.hour:
      return {
        current: AbsoluteTime.fromMilliseconds(
          sub(time, { hours: 1 }).getTime(),
        ),
        previous: AbsoluteTime.fromMilliseconds(
          sub(time, { hours: 2 }).getTime(),
        ),
      };
    case TalerCorebankApi.MonitorTimeframeParam.day:
      return {
        current: AbsoluteTime.fromMilliseconds(
          sub(time, { days: 1 }).getTime(),
        ),
        previous: AbsoluteTime.fromMilliseconds(
          sub(time, { days: 4 }).getTime(),
        ),
      };
    case TalerCorebankApi.MonitorTimeframeParam.month:
      return {
        current: AbsoluteTime.fromMilliseconds(
          sub(time, { months: 1 }).getTime(),
        ),
        previous: AbsoluteTime.fromMilliseconds(
          sub(time, { months: 2 }).getTime(),
        ),
      };
    case TalerCorebankApi.MonitorTimeframeParam.year:
      return {
        current: AbsoluteTime.fromMilliseconds(
          sub(time, { years: 1 }).getTime(),
        ),
        previous: AbsoluteTime.fromMilliseconds(
          sub(time, { years: 2 }).getTime(),
        ),
      };
    case TalerCorebankApi.MonitorTimeframeParam.decade:
      return {
        current: AbsoluteTime.fromMilliseconds(
          sub(time, { years: 10 }).getTime(),
        ),
        previous: AbsoluteTime.fromMilliseconds(
          sub(time, { years: 20 }).getTime(),
        ),
      };
    default:
      assertUnreachable(timeframe);
  }
}

function Metrics({
  routeDownloadStats,
}: {
  routeDownloadStats: RouteDefinition;
}): VNode {
  const { i18n, dateLocale } = useTranslationContext();
  const [metricType, setMetricType] =
    useState<TalerCorebankApi.MonitorTimeframeParam>(
      TalerCorebankApi.MonitorTimeframeParam.hour,
    );
  const { config } = useBankCoreApiContext();
  const respInfo = useConversionInfo();
  const params = getTimeframesForDate(new Date(), metricType);

  const resp = useLastMonitorInfo(params.current, params.previous, metricType);
  if (!resp) return <Fragment />;
  if (resp instanceof TalerError) {
    return <ErrorLoadingWithDebug error={resp} />;
  }
  if (!respInfo) return <Fragment />;
  if (respInfo instanceof TalerError) {
    return <ErrorLoadingWithDebug error={respInfo} />;
  }
  if (respInfo.type === "fail") {
    switch (respInfo.case) {
      case HttpStatusCode.NotImplemented: {
        return (
          <Attention type="danger" title={i18n.str`Cashout are disabled`}>
            <i18n.Translate>
              Cashout should be enable by configuration and the conversion rate
              should be initialized with fee, ratio and rounding mode.
            </i18n.Translate>
          </Attention>
        );
      }
      default: {
        assertUnreachable(respInfo.case);
      }
    }
  }

  if (resp.current.type !== "ok") {
    switch (resp.current.case) {
      case HttpStatusCode.BadRequest:
        return (
          <Attention
            type="warning"
            title={i18n.str`Querying for the current stats failed`}
          >
            <i18n.Translate>The request parameters are wrong</i18n.Translate>
          </Attention>
        );
      case HttpStatusCode.Unauthorized:
        return (
          <Attention
            type="warning"
            title={i18n.str`Querying for the current stats failed`}
          >
            <i18n.Translate>The user is unauthorized</i18n.Translate>
          </Attention>
        );
      default: {
        assertUnreachable(resp.current);
      }
    }
  }
  if (resp.previous.type !== "ok") {
    switch (resp.previous.case) {
      case HttpStatusCode.BadRequest:
        return (
          <Attention
            type="warning"
            title={i18n.str`Querying for the previous stats failed`}
          >
            <i18n.Translate>The request parameters are wrong</i18n.Translate>
          </Attention>
        );
      case HttpStatusCode.Unauthorized:
        return (
          <Attention
            type="warning"
            title={i18n.str`Querying for the previous stats failed`}
          >
            <i18n.Translate>The user is unauthorized</i18n.Translate>
          </Attention>
        );
      default: {
        assertUnreachable(resp.previous);
      }
    }
  }
  return (
    <div class="px-4 mt-4">
      <div class="sm:flex sm:items-center mb-4">
        <div class="sm:flex-auto">
          <h1 class="text-base font-semibold leading-6 text-gray-900">
            <i18n.Translate>Transaction volume report</i18n.Translate>
          </h1>
        </div>
      </div>

      <div class="sm:hidden">
        <label for="tabs" class="sr-only">
          <i18n.Translate>Select a section</i18n.Translate>
        </label>
        <select
          id="tabs"
          name="tabs"
          class="block w-full rounded-md border-gray-300 focus:border-indigo-500 focus:ring-indigo-500"
          onChange={(e) => {
            // const op = e.currentTarget.value as typeof metricType
            setMetricType(
              e.currentTarget
                .value as unknown as TalerCorebankApi.MonitorTimeframeParam,
            );
          }}
        >
          <option
            value={TalerCorebankApi.MonitorTimeframeParam.hour}
            selected={metricType == TalerCorebankApi.MonitorTimeframeParam.hour}
          >
            <i18n.Translate>Last hour</i18n.Translate>
          </option>
          <option
            value={TalerCorebankApi.MonitorTimeframeParam.day}
            selected={metricType == TalerCorebankApi.MonitorTimeframeParam.day}
          >
            <i18n.Translate>Previous day</i18n.Translate>
          </option>
          <option
            value={TalerCorebankApi.MonitorTimeframeParam.month}
            selected={
              metricType == TalerCorebankApi.MonitorTimeframeParam.month
            }
          >
            <i18n.Translate>Last month</i18n.Translate>
          </option>
          <option
            value={TalerCorebankApi.MonitorTimeframeParam.year}
            selected={metricType == TalerCorebankApi.MonitorTimeframeParam.year}
          >
            <i18n.Translate>Last year</i18n.Translate>
          </option>
        </select>
      </div>
      <div class="hidden sm:block">
        {/* FIXME: This should be LINKS */}
        <nav
          class="isolate flex divide-x divide-gray-200 rounded-lg shadow"
          aria-label="Tabs"
        >
          <button
            type="button"
            name="set last hour"
            onClick={(e) => {
              e.preventDefault();
              setMetricType(TalerCorebankApi.MonitorTimeframeParam.hour);
            }}
            data-selected={
              metricType == TalerCorebankApi.MonitorTimeframeParam.hour
            }
            class="rounded-l-lg text-gray-500 hover:text-gray-700 data-[selected=true]:text-gray-900 group relative min-w-0 flex-1 overflow-hidden bg-white py-4 px-4 text-center text-sm font-medium hover:bg-gray-50 focus:z-10"
          >
            <span>
              <i18n.Translate>Last hour</i18n.Translate>
            </span>
            <span
              aria-hidden="true"
              data-selected={
                metricType == TalerCorebankApi.MonitorTimeframeParam.hour
              }
              class="bg-transparent data-[selected=true]:bg-indigo-500 absolute inset-x-0 bottom-0 h-0.5"
            ></span>
          </button>
          <button
            type="button"
            name="set previous day"
            onClick={(e) => {
              e.preventDefault();
              setMetricType(TalerCorebankApi.MonitorTimeframeParam.day);
            }}
            data-selected={
              metricType == TalerCorebankApi.MonitorTimeframeParam.day
            }
            class="             text-gray-500 hover:text-gray-700 data-[selected=true]:text-gray-900 group relative min-w-0 flex-1 overflow-hidden bg-white py-4 px-4 text-center text-sm font-medium hover:bg-gray-50 focus:z-10"
          >
            <span>
              <i18n.Translate>Previous day</i18n.Translate>
            </span>
            <span
              aria-hidden="true"
              data-selected={
                metricType == TalerCorebankApi.MonitorTimeframeParam.day
              }
              class="bg-transparent data-[selected=true]:bg-indigo-500 absolute inset-x-0 bottom-0 h-0.5"
            ></span>
          </button>
          <button
            type="button"
            name="set last month"
            onClick={(e) => {
              e.preventDefault();
              setMetricType(TalerCorebankApi.MonitorTimeframeParam.month);
            }}
            data-selected={
              metricType == TalerCorebankApi.MonitorTimeframeParam.month
            }
            class="rounded-r-lg text-gray-500 hover:text-gray-700 data-[selected=true]:text-gray-900 group relative min-w-0 flex-1 overflow-hidden bg-white py-4 px-4 text-center text-sm font-medium hover:bg-gray-50 focus:z-10"
          >
            <span>
              <i18n.Translate>Last month</i18n.Translate>
            </span>
            <span
              aria-hidden="true"
              data-selected={
                metricType == TalerCorebankApi.MonitorTimeframeParam.month
              }
              class="bg-transparent data-[selected=true]:bg-indigo-500 absolute inset-x-0 bottom-0 h-0.5"
            ></span>
          </button>
          <button
            type="button"
            name="set last year"
            onClick={(e) => {
              e.preventDefault();
              setMetricType(TalerCorebankApi.MonitorTimeframeParam.year);
            }}
            data-selected={
              metricType == TalerCorebankApi.MonitorTimeframeParam.year
            }
            class="rounded-r-lg text-gray-500 hover:text-gray-700 data-[selected=true]:text-gray-900 group relative min-w-0 flex-1 overflow-hidden bg-white py-4 px-4 text-center text-sm font-medium hover:bg-gray-50 focus:z-10"
          >
            <span>
              <i18n.Translate>Last Year</i18n.Translate>
            </span>
            <span
              aria-hidden="true"
              data-selected={
                metricType == TalerCorebankApi.MonitorTimeframeParam.year
              }
              class="bg-transparent data-[selected=true]:bg-indigo-500 absolute inset-x-0 bottom-0 h-0.5"
            ></span>
          </button>
        </nav>
      </div>

      <div class="w-full flex justify-between">
        <h1 class="text-base text-gray-900 mt-5">
          {i18n.str`Trading volume on ${getDateForTimeframe(
            params.current,
            metricType,
            dateLocale,
          )} compared to ${getDateForTimeframe(
            params.previous,
            metricType,
            dateLocale,
          )}`}
        </h1>
      </div>
      <dl class="mt-5 grid grid-cols-1 md:grid-cols-2  divide-y divide-gray-200 overflow-hidden rounded-lg bg-white shadow-lg md:divide-x md:divide-y-0">
        {resp.current.body.type !== "with-conversions" ||
        resp.previous.body.type !== "with-conversions" ? undefined : (
          <Fragment>
            <div class="px-4 py-5 sm:p-6">
              <dt class="text-base font-normal text-gray-900">
                <i18n.Translate>Cashin</i18n.Translate>
                <div class="text-xs text-gray-500">
                  <i18n.Translate>
                    Transferred from an external account to an account in this
                    bank.
                  </i18n.Translate>
                </div>
              </dt>
              <MetricValue
                current={resp.current.body.cashinFiatVolume}
                previous={resp.previous.body.cashinFiatVolume}
                spec={respInfo.body.fiat_currency_specification}
              />
            </div>
            <div class="px-4 py-5 sm:p-6">
              <dt class="text-base font-normal text-gray-900">
                <i18n.Translate>Cashout</i18n.Translate>
              </dt>
              <div class="text-xs text-gray-500">
                <i18n.Translate>
                  Transferred from an account in this bank to an external
                  account.
                </i18n.Translate>
              </div>
              <MetricValue
                current={resp.current.body.cashoutFiatVolume}
                previous={resp.previous.body.cashoutFiatVolume}
                spec={respInfo.body.fiat_currency_specification}
              />
            </div>
          </Fragment>
        )}
        <div class="px-4 py-5 sm:p-6">
          <dt class="text-base font-normal text-gray-900">
            <i18n.Translate>Payin</i18n.Translate>
            <div class="text-xs text-gray-500">
              <i18n.Translate>
                Transferred from an account to a Taler exchange.
              </i18n.Translate>
            </div>
          </dt>
          <MetricValue
            current={resp.current.body.talerInVolume}
            previous={resp.previous.body.talerInVolume}
            spec={config.currency_specification}
          />
        </div>
        <div class="px-4 py-5 sm:p-6">
          <dt class="text-base font-normal text-gray-900">
            <i18n.Translate>Payout</i18n.Translate>
            <div class="text-xs text-gray-500">
              <i18n.Translate>
                Transferred from a Taler exchange to another account.
              </i18n.Translate>
            </div>
          </dt>
          <MetricValue
            current={resp.current.body.talerOutVolume}
            previous={resp.previous.body.talerOutVolume}
            spec={config.currency_specification}
          />
        </div>
      </dl>
      <div class="flex justify-end mt-4">
        <a
          href={routeDownloadStats.url({})}
          name="download stats"
          class="disabled:opacity-50 disabled:cursor-default cursor-pointer rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
        >
          <i18n.Translate>Download stats as CSV</i18n.Translate>
        </a>
      </div>
    </div>
  );
}

function MetricValue({
  current,
  previous,
  spec,
}: {
  spec: CurrencySpecification;
  current: AmountString | undefined;
  previous: AmountString | undefined;
}): VNode {
  const { i18n } = useTranslationContext();
  const cmp = current && previous ? Amounts.cmp(current, previous) : 0;
  const cv = !current ? undefined : Amounts.stringifyValue(current);
  const currAmount = !cv ? undefined : Number.parseFloat(cv);
  const prevAmount = !previous
    ? undefined
    : Number.parseFloat(Amounts.stringifyValue(previous));

  const rate =
    !currAmount ||
    Number.isNaN(currAmount) ||
    !prevAmount ||
    Number.isNaN(prevAmount)
      ? 0
      : cmp === -1
        ? 1 - Math.round(currAmount) / Math.round(prevAmount)
        : cmp === 1
          ? Math.round(currAmount) / Math.round(prevAmount) - 1
          : 0;

  const negative = cmp === 0 ? undefined : cmp === -1;
  const rateStr = `${(Math.abs(rate) * 100).toFixed(2)}%`;
  return (
    <Fragment>
      <dd class="mt-1 block ">
        <div class="flex justify-start text-2xl items-baseline font-semibold text-indigo-600">
          {!current ? (
            "-"
          ) : (
            <RenderAmount
              value={Amounts.parseOrThrow(current)}
              spec={spec}
              hideSmall
            />
          )}
        </div>
        <div class="flex flex-col">
          <div class="flex justify-end items-baseline text-2xl font-semibold text-indigo-600">
            <small class="ml-2 text-sm font-medium text-gray-500">
              <i18n.Translate>from</i18n.Translate>{" "}
              {!previous ? (
                "-"
              ) : (
                <RenderAmount
                  value={Amounts.parseOrThrow(previous)}
                  spec={spec}
                  hideSmall
                />
              )}
            </small>
          </div>
          {!!rate && (
            <span
              data-negative={negative}
              class="flex items-center gap-x-1.5 w-fit rounded-md bg-green-100 text-green-800 data-[negative=true]:bg-red-100 px-2 py-1 text-xs font-medium data-[negative=true]:text-red-700 whitespace-pre"
            >
              {negative ? (
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  fill="none"
                  viewBox="0 0 24 24"
                  stroke-width="1.5"
                  stroke="currentColor"
                  class="w-6 h-6"
                >
                  <path
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    d="M12 4.5v15m0 0l6.75-6.75M12 19.5l-6.75-6.75"
                  />
                </svg>
              ) : (
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  fill="none"
                  viewBox="0 0 24 24"
                  stroke-width="1.5"
                  stroke="currentColor"
                  class="w-6 h-6"
                >
                  <path
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    d="M12 19.5v-15m0 0l-6.75 6.75M12 4.5l6.75 6.75"
                  />
                </svg>
              )}

              {negative ? (
                <span class="sr-only">
                  <i18n.Translate>Decreased by</i18n.Translate>
                </span>
              ) : (
                <span class="sr-only">
                  <i18n.Translate>Increased by</i18n.Translate>
                </span>
              )}
              {rateStr}
            </span>
          )}
        </div>
      </dd>
    </Fragment>
  );
}