aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/pages/admin/AdminHome.tsx
blob: 9bc2ee571ebba4167ba696d1d604e519c139a1d3 (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
import { AmountString, Amounts, TalerCorebankApi, TalerError } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { Fragment, VNode, h } from "preact";
import { useState } from "preact/hooks";
import { ErrorLoading } from "@gnu-taler/web-util/browser";
import { Transactions } from "../../components/Transactions/index.js";
import { useLastMonitorInfo } from "../../hooks/circuit.js";
import { RenderAmount } from "../PaytoWireTransferForm.js";
import { WireTransfer } from "./Account.js";
import { AccountList } from "./AccountList.js";

/**
 * Query account information and show QR code if there is pending withdrawal
 */
interface Props {
  onRegister: () => void;

  onCreateAccount: () => void;
  onShowAccountDetails: (aid: string) => void;
  onRemoveAccount: (aid: string) => void;
  onUpdateAccountPassword: (aid: string) => void;
  onShowCashoutForAccount: (aid: string) => void;
}
export function AdminHome({ onCreateAccount, onRegister, onRemoveAccount, onShowAccountDetails, onShowCashoutForAccount, onUpdateAccountPassword }: Props): VNode {
  return <Fragment>
    {/* <Metrics /> */}
    <WireTransfer onRegister={onRegister} />

    <Transactions account="admin" />
    <AccountList
      onCreateAccount={onCreateAccount}
      onRemoveAccount={onRemoveAccount}
      onShowCashoutForAccount={onShowCashoutForAccount}
      onShowAccountDetails={onShowAccountDetails}
      onUpdateAccountPassword={onUpdateAccountPassword}
    />

  </Fragment>
}

function Metrics(): VNode {
  const { i18n } = useTranslationContext()
  const [metricType, setMetricType] = useState<TalerCorebankApi.MonitorTimeframeParam>(TalerCorebankApi.MonitorTimeframeParam.day);

  const resp = useLastMonitorInfo(new Date(), metricType);
  console.log(resp)
  if (!resp) return <Fragment />;
  if (resp instanceof TalerError) {
    return <ErrorLoading error={resp} />
  }
  if (resp.current.type !== "ok" || resp.current.type !== "ok" || resp.current.type !== "ok") {
    return <Fragment />
  }
  if (resp.previous.type !== "ok" || resp.previous.type !== "ok" || resp.previous.type !== "ok") {
    return <Fragment />
  }

  // const metric = getMetricInfo(metricType, current, previous);

  return <Fragment>
    <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 any)
      }}>
        <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>Last 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">
      <nav class="isolate flex divide-x divide-gray-200 rounded-lg shadow" aria-label="Tabs">
        <a href="#" 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>
        </a>
        <a href="#" onClick={(e) => { e.preventDefault(); setMetricType(TalerCorebankApi.MonitorTimeframeParam.day) }} data-selected={metricType == TalerCorebankApi.MonitorTimeframeParam.day} aria-current="page" 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>Last 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>
        </a>
        <a href="#" 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>
        </a>
        <a href="#" 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>
        </a>
      </nav>
    </div>

    <div class="w-full flex justify-between">

      <h1 class="text-base font-semibold leading-7 text-gray-900 mt-5">

        Trading volume on Thursday
      </h1>
      <div class="flex items-center justify-between">
        {/* <span class="flex flex-grow flex-col">
          <span class="text-sm text-black font-medium leading-6 " id="availability-label">
            ASD
          </span>
        </span> */}
        <button type="button" class="bg-gray-200 relative inline-flex h-6 w-48 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2" role="switch" aria-checked="false">
          <span class="sr-only">Use setting</span>
          {/* <!-- Enabled: "translate-x-5", Not Enabled: "translate-x-0" --> */}
          <span class="translate-x-0 pointer-events-none relative inline-block h-5 w-32 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out">
            {/* <!-- Enabled: "opacity-0 duration-100 ease-out", Not Enabled: "opacity-100 duration-200 ease-in" --> */}
            <span class="opacity-100 duration-200 ease-in absolute inset-0 flex h-full w-full items-center justify-center transition-opacity" aria-hidden="true">
              Transaction
            </span>
            {/* <!-- Enabled: "opacity-100 duration-200 ease-in", Not Enabled: "opacity-0 duration-100 ease-out" --> */}
            <span class="opacity-0 duration-100 ease-out absolute inset-0 flex h-full w-full items-center justify-center transition-opacity" aria-hidden="true">
              <svg class="h-3 w-3 text-indigo-600" fill="currentColor" viewBox="0 0 12 12">
                <path d="M3.707 5.293a1 1 0 00-1.414 1.414l1.414-1.414zM5 8l-.707.707a1 1 0 001.414 0L5 8zm4.707-3.293a1 1 0 00-1.414-1.414l1.414 1.414zm-7.414 2l2 2 1.414-1.414-2-2-1.414 1.414zm3.414 2l4-4-1.414-1.414-4 4 1.414 1.414z" />
              </svg>
            </span>
          </span>
        </button>
      </div>

    </div>
    <dl class="mt-5 grid grid-cols-1 divide-y divide-gray-200 overflow-hidden rounded-lg bg-white shadow-lg md:grid-cols-3 md:divide-x md:divide-y-0">

      {resp.current.body.type !== "with-cashout" || resp.previous.body.type !== "with-cashout" ? 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>
            </dt>
            <MetricValue
              current={resp.current.body.cashinExternalVolume}
              previous={resp.previous.body.cashinExternalVolume}
            />
          </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>
            <MetricValue
              current={resp.current.body.cashoutExternalVolume}
              previous={resp.previous.body.cashoutExternalVolume}
            />
          </div>
        </Fragment>
      }
      <div class="px-4 py-5 sm:p-6">
        <dt class="text-base font-normal text-gray-900">
          <i18n.Translate>Payout</i18n.Translate>
        </dt>
        <MetricValue
          current={resp.current.body.talerPayoutInternalVolume}
          previous={resp.previous.body.talerPayoutInternalVolume}
        />
      </div>
    </dl>
  </Fragment>

}


function MetricValue({ current, previous }: { current: AmountString | undefined, previous: AmountString | undefined }): VNode {
  const { i18n } = useTranslationContext()
  const cmp = current && previous ? Amounts.cmp(current, previous) : 0;
  const currAmount = !current ? undefined : Number.parseFloat(Amounts.stringifyValue(current))
  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 rateStr = `${(Math.abs(rate) * 100).toFixed(2)}%`
  return <dd class="mt-1 flex justify-between md:block lg:flex">
    <div class="flex justify-start  items-baseline text-2xl font-semibold text-indigo-600">
      {!current ? "-" : <RenderAmount value={Amounts.parseOrThrow(current)} />}
    </div>
    <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)} />}
      </small>
    </div>

    {cmp == 1 &&
      <div class="inline-flex items-baseline rounded-full px-2.5 py-0.5 text-sm font-medium bg-green-100 text-green-800 md:mt-2 lg:mt-0">
        <svg class="-ml-1 mr-0.5 h-5 w-5 flex-shrink-0 self-center text-green-500" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
          <path fill-rule="evenodd" d="M10 17a.75.75 0 01-.75-.75V5.612L5.29 9.77a.75.75 0 01-1.08-1.04l5.25-5.5a.75.75 0 011.08 0l5.25 5.5a.75.75 0 11-1.08 1.04l-3.96-4.158V16.25A.75.75 0 0110 17z" clip-rule="evenodd" />
        </svg>
        <span class="sr-only"><i18n.Translate>Increased by</i18n.Translate></span>
        {rateStr}
      </div>
    }
    {cmp == -1 &&
      <div class="inline-flex items-baseline rounded-full px-2.5 py-0.5 text-sm font-medium bg-red-100 text-red-800 md:mt-2 lg:mt-0">
        <svg class="-ml-1 mr-0.5 h-5 w-5 flex-shrink-0 self-center text-red-500" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
          <path fill-rule="evenodd" d="M10 3a.75.75 0 01.75.75v10.638l3.96-4.158a.75.75 0 111.08 1.04l-5.25 5.5a.75.75 0 01-1.08 0l-5.25-5.5a.75.75 0 111.08-1.04l3.96 4.158V3.75A.75.75 0 0110 3z" clip-rule="evenodd" />
        </svg>
        <span class="sr-only"><i18n.Translate>Descreased by</i18n.Translate></span>
        {rateStr}
      </div>
    }
  </dd>
}