summaryrefslogtreecommitdiff
path: root/packages/bank-ui/src/pages/BankFrame.tsx
blob: db757ee0702589935ca37c84ba3b56c36ca56181 (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
/*
 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,
  Amounts,
  ObservabilityEventType,
  TalerError,
  TranslatedString,
  assertUnreachable,
} from "@gnu-taler/taler-util";
import {
  Footer,
  Header,
  Loading,
  RouteDefinition,
  ToastBanner,
  notifyError,
  notifyException,
  useBankCoreApiContext,
  useTranslationContext,
} from "@gnu-taler/web-util/browser";
import { ComponentChildren, Fragment, VNode, h } from "preact";
import { useEffect, useErrorBoundary, useState } from "preact/hooks";
import { privatePages } from "../Routing.js";
import { useSettingsContext } from "../context/settings.js";
import { useAccountDetails } from "../hooks/account.js";
import { useBankState } from "../hooks/bank-state.js";
import {
  getAllBooleanPreferences,
  getLabelForPreferences,
  usePreferences,
} from "../hooks/preferences.js";
import { useSessionState } from "../hooks/session.js";
import { RenderAmount } from "./PaytoWireTransferForm.js";

const GIT_HASH = typeof __GIT_HASH__ !== "undefined" ? __GIT_HASH__ : undefined;
const VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : undefined;

export function BankFrame({
  children,
  account,
  routeAccountDetails,
}: {
  account?: string;
  routeAccountDetails?: RouteDefinition;
  children: ComponentChildren;
}): VNode {
  const { i18n } = useTranslationContext();
  const session = useSessionState();
  const settings = useSettingsContext();
  const [preferences, updatePreferences] = usePreferences();
  const [, , resetBankState] = useBankState();

  const [error, resetError] = useErrorBoundary();

  useEffect(() => {
    if (error) {
      if (error instanceof Error) {
        console.log("Internal error, please report", error);
        notifyException(i18n.str`Internal error, please report.`, error);
      } else {
        console.log("Internal error, please report", error);
        notifyError(
          i18n.str`Internal error, please report.`,
          String(error) as TranslatedString,
        );
      }
      resetError();
    }
  }, [error]);

  return (
    <div
      class="min-h-full flex flex-col m-0 bg-slate-200"
      style="min-height: 100vh;"
    >
      <div class="bg-indigo-600 pb-32">
        <Header
          title="Bank"
          iconLinkURL={settings.iconLinkURL ?? "#"}
          profileURL={routeAccountDetails?.url({})}
          notificationURL={
            preferences.showDebugInfo
              ? privatePages.notifications.url({})
              : undefined
          }
          onLogout={
            session.state.status !== "loggedIn"
              ? undefined
              : () => {
                  session.logOut();
                  resetBankState();
                }
          }
          sites={
            !settings.topNavSites ? [] : Object.entries(settings.topNavSites)
          }
          supportedLangs={["en", "es", "de"]}
        >
          <li>
            <div class="text-xs font-semibold leading-6 text-gray-400">
              <i18n.Translate>Preferences</i18n.Translate>
            </div>
            <ul role="list" class="space-y-4">
              {getAllBooleanPreferences().map((set) => {
                const isOn: boolean = !!preferences[set];
                return (
                  <li key={set} class="pl-2">
                    <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"
                        >
                          {getLabelForPreferences(set, i18n)}
                        </span>
                      </span>
                      <button
                        type="button"
                        name={`${set} switch`}
                        data-enabled={isOn}
                        class="bg-indigo-600 data-[enabled=false]:bg-gray-200 relative inline-flex h-6 w-11 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"
                        aria-labelledby="availability-label"
                        aria-describedby="availability-description"
                        onClick={() => {
                          updatePreferences(set, !isOn);
                        }}
                      >
                        <span
                          aria-hidden="true"
                          data-enabled={isOn}
                          class="translate-x-5 data-[enabled=false]:translate-x-0 pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"
                        ></span>
                      </button>
                    </div>
                  </li>
                );
              })}
            </ul>
          </li>
        </Header>
      </div>

      <div class="fixed z-20 top-14 w-full">
        <div class="mx-auto w-4/5">
          <ToastBanner />
          {/* <Attention type="success" title={"hola" as TranslatedString} onClose={() => { }} /> */}
        </div>
      </div>

      <main class="-mt-32 flex-1">
        {account && routeAccountDetails && (
          <header class="py-6 bg-indigo-600">
            <div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
              <h1 class=" flex flex-wrap items-center justify-between sm:flex-nowrap">
                <span class="text-2xl font-bold tracking-tight text-white">
                  <WelcomeAccount
                    account={account}
                    routeAccountDetails={routeAccountDetails}
                  />
                </span>
                <span class="text-2xl font-bold tracking-tight text-white">
                  <AccountBalance account={account} />
                </span>
              </h1>
            </div>
          </header>
        )}

        <div class="mx-auto max-w-7xl px-4 pb-4 sm:px-6 lg:px-8">
          <div class="rounded-lg bg-white px-5 py-6 shadow sm:px-6">
            {children}
          </div>
        </div>
      </main>

      <AppActivity />

      <Footer
        testingUrlKey="corebank-api-base-url"
        GIT_HASH={GIT_HASH}
        VERSION={VERSION}
      />
    </div>
  );
}

function Wait({ class: clazz }: { class?: string }): VNode {
  return (
    <Fragment>
      <style>{`
      .animated-loader {
        display: inline-block;
        --b: 5px; 
        border-radius: 50%;
        aspect-ratio: 1;
        padding: 1px;
        background: conic-gradient(#0000 10%,#4f46e5) content-box;
        -webkit-mask:
          repeating-conic-gradient(#0000 0deg,#000 1deg 20deg,#0000 21deg 36deg),
          radial-gradient(farthest-side,#0000 calc(100% - var(--b) - 1px),#000 calc(100% - var(--b)));
        -webkit-mask-composite: destination-in;
                mask-composite: intersect;
        animation:spinning-loader 1s infinite steps(10);
      }
      @keyframes spinning-loader {to{transform: rotate(1turn)}}    
    `}</style>
      <div class={`animated-loader ${clazz}`} />
    </Fragment>
  );
}

function AppActivity(): VNode {
  const [lastEvent, setLastEvent] = useState<{
    url: string;
    id: string;
    when: AbsoluteTime;
  }>();
  const [status, setStatus] = useState<"ok" | "fail">();
  const d = useBankCoreApiContext();
  const onBackendActivity = !d ? undefined : d.onActivity;
  const cancelRequest = !d ? undefined : d.cancelRequest;
  const [pref] = usePreferences();
  useEffect(() => {
    // console.log("ASDASDS", onBackendActivity)
    if (!pref.showDebugInfo) return;
    if (!onBackendActivity) return;
    return onBackendActivity((ev) => {
      switch (ev.type) {
        case ObservabilityEventType.HttpFetchStart: {
          setLastEvent(ev);
          setStatus(undefined);
          return;
        }
        case ObservabilityEventType.HttpFetchFinishError: {
          setStatus("fail");
          return;
        }
        case ObservabilityEventType.HttpFetchFinishSuccess: {
          setStatus("ok");
          return;
        }
        /**
         * all of this are ignored
         */
        case ObservabilityEventType.DbQueryStart:
        case ObservabilityEventType.DbQueryFinishSuccess:
        case ObservabilityEventType.DbQueryFinishError:
        case ObservabilityEventType.RequestStart:
        case ObservabilityEventType.RequestFinishSuccess:
        case ObservabilityEventType.RequestFinishError:
        case ObservabilityEventType.TaskStart:
        case ObservabilityEventType.TaskStop:
        case ObservabilityEventType.TaskReset:
        case ObservabilityEventType.ShepherdTaskResult:
        case ObservabilityEventType.DeclareTaskDependency:
        case ObservabilityEventType.CryptoStart:
        case ObservabilityEventType.CryptoFinishSuccess:
        case ObservabilityEventType.CryptoFinishError:
        case ObservabilityEventType.Message:
          return;
        default: {
          assertUnreachable(ev);
        }
      }
    });
  });
  if (!pref.showDebugInfo || !lastEvent) return <Fragment />;
  return (
    <div
      data-status={status}
      class="fixed z-20 bottom-0 w-full ease-in-out delay-1000 transition-transform data-[status=ok]:scale-y-0"
    >
      <div
        data-status={status}
        class="mx-auto w-4/5 center flex p-1 bg-gray-300 m-1 data-[status=fail]:bg-red-200 data-[status=ok]:bg-green-200 "
      >
        {!status ? <Wait class="w-6 h-6" /> : <div class="w-6 h-6" />}

        <p class="ml-2 my-auto text-sm text-gray-500">{lastEvent.url}</p>
        {!status ? (
          <button
            onClick={() => {
              if (cancelRequest) cancelRequest(lastEvent.id);
            }}
          >
            cancel
          </button>
        ) : undefined}
      </div>
    </div>
  );
}

function WelcomeAccount({
  account,
  routeAccountDetails,
}: {
  account: string;
  routeAccountDetails: RouteDefinition;
}): VNode {
  const { i18n } = useTranslationContext();
  const result = useAccountDetails(account);
  if (!result) {
    return <Loading />;
  }
  if (result instanceof TalerError) {
    return <div />;
  }
  if (result.type === "fail") {
    return (
      <a
        name="account details"
        href={routeAccountDetails.url({})}
        class="underline underline-offset-2"
      >
        <i18n.Translate>Welcome</i18n.Translate>
      </a>
    );
  }
  return (
    <a
      name="account details"
      href={routeAccountDetails.url({})}
      class="underline underline-offset-2"
    >
      <i18n.Translate>
        Welcome, <span class="whitespace-nowrap">{result.body.name}</span>
      </i18n.Translate>
    </a>
  );
}

function AccountBalance({ account }: { account: string }): VNode {
  const result = useAccountDetails(account);
  const { config } = useBankCoreApiContext();
  if (!result) {
    return <Loading />;
  }
  if (result instanceof TalerError) {
    return <div />;
  }
  if (result.type === "fail") return <div />;

  return (
    <RenderAmount
      value={Amounts.parseOrThrow(result.body.balance.amount)}
      negative={result.body.balance.credit_debit_indicator === "debit"}
      spec={config.currency_specification}
    />
  );
}