summaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/pages/BankFrame.tsx
blob: 5b6d95ade3544e8a78e213f5488b454428c1d8f8 (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
/*
 This file is part of GNU Taler
 (C) 2022 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 { Logger, PaytoUriIBAN, TranslatedString, parsePaytoUri, stringifyPaytoUri } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { ComponentChildren, Fragment, h, VNode } from "preact";
import { StateUpdater, useEffect, useState } from "preact/hooks";
import talerLogo from "../assets/logo-white.svg";
import { LangSelectorLikePy as LangSelector } from "../components/LangSelector.js";
import { useBackendContext } from "../context/backend.js";
import { useBusinessAccountDetails } from "../hooks/circuit.js";
import { bankUiSettings } from "../settings.js";
import { useSettings } from "../hooks/settings.js";
import { ErrorMessage, onNotificationUpdate } from "../hooks/notification.js";
import { CopyButton, CopyIcon } from "../components/CopyButton.js";

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

const versionText = VERSION
  ? GIT_HASH
    ? `Version ${VERSION} (${GIT_HASH.substring(0, 8)})`
    : VERSION
  : "";

const logger = new Logger("BankFrame");

function MaybeBusinessButton({
  account,
  onClick,
}: {
  account: string;
  onClick: () => void;
}): VNode {
  const { i18n } = useTranslationContext();
  const result = useBusinessAccountDetails(account);
  if (!result.ok) return <Fragment />;
  return (
    <a
      href="#"
      class="pure-button pure-button-primary"
      onClick={(e) => {
        e.preventDefault();
        onClick();
      }}
    >{i18n.str`Business Profile`}</a>
  );
}

export function BankFrame({
  children,
  goToBusinessAccount,
}: {
  children: ComponentChildren;
  goToBusinessAccount?: () => void;
}): VNode {
  const { i18n } = useTranslationContext();
  const backend = useBackendContext();
  const [settings, updateSettings] = useSettings();
  const [open, setOpen] = useState(false)

  const demo_sites = [];
  for (const i in bankUiSettings.demoSites)
    demo_sites.push(
      <a href={bankUiSettings.demoSites[i][1]}>
        {bankUiSettings.demoSites[i][0]}
      </a>,
    );

  return (<div class="min-h-full">
    <div class="bg-indigo-600 pb-32">
      <nav class="border-b border-indigo-300 border-opacity-25 bg-indigo-600 lg:border-none">
        <div class="mx-auto max-w-7xl px-2 sm:px-4 lg:px-8">
          <div class="relative flex h-16 items-center justify-between lg:border-b lg:border-indigo-400 lg:border-opacity-25">
            <div class="flex items-center px-2 lg:px-0">
              <div class="flex-shrink-0">
                <img class="block h-8 w-8" src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=300" alt="Your Company" />
              </div>
              <div class="hidden lg:ml-10 lg:block">
                <div class="flex space-x-4">
                  {/* <!-- Current: "bg-indigo-700 text-white", Default: "text-white hover:bg-indigo-500 hover:bg-opacity-75" --> */}
                  {bankUiSettings.demoSites.map(([name, url]) => {
                    return <a href={url} class="text-white hover:bg-indigo-500 hover:bg-opacity-75 rounded-md py-2 px-3 text-sm font-medium">{name}</a>
                  })}
                </div>
              </div>
            </div>

            <div class="flex lg:hidden">
              {/* <!-- Mobile menu button --> */}
              <button type="button" class="relative inline-flex items-center justify-center rounded-md bg-indigo-600 p-2 text-indigo-200 hover:bg-indigo-500 hover:bg-opacity-75 hover:text-white focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-indigo-600" aria-controls="mobile-menu" aria-expanded="false"
                onClick={(e) => {
                  setOpen(!open)
                }}>
                <span class="absolute -inset-0.5"></span>
                <span class="sr-only">Open main menu</span>
                {/* <!-- Menu open: "hidden", Menu closed: "block" --> */}
                <svg class="block h-6 w-6" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
                  <path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
                </svg>
                {/* <!-- Menu open: "block", Menu closed: "hidden" --> */}
                <svg class="hidden h-6 w-6" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
                  <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
                </svg>
              </button>
            </div>
          </div>
        </div>

        {/* <!-- Mobile menu, show/hide based on menu state. --> */}
        {open &&
          <div class="lg:hidden" id="mobile-menu">
            <div class="space-y-1 px-2 pb-3 pt-2">
              {/* <!-- Current: "bg-indigo-700 text-white", Default: "text-white hover:bg-indigo-500 hover:bg-opacity-75" --> */}
              {bankUiSettings.demoSites.map(([name, url]) => {
                return <a href={url} class="text-white hover:bg-indigo-500 hover:bg-opacity-75 block rounded-md py-2 px-3 text-base font-medium">{name}</a>
              })}
            </div>

          </div>
        }
      </nav >
      <header class="py-10">

        <div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
          <div class=" flex flex-wrap items-center justify-between sm:flex-nowrap">
            {/* <h1 class="text-base font-semibold leading-6 text-gray-900"></h1> */}
            <h1 class="text-3xl font-bold tracking-tight text-white"><WelcomeAccount /></h1>
            <div>

              <h2 class="text-3xl font-bold tracking-tight text-white">KUDOS 100.00</h2>
            </div>
            {/* <div class="ml-4 mt-2 flex-shrink-0">
              <button type="button" class="relative inline-flex items-center 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">Create new job</button>
            </div> */}
          </div>
        </div>
      </header>
    </div >

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

    <Footer />
  </div >

    // <Fragment>
    //   <header
    //     class="demobar"
    //     style="display: flex; flex-direction: row; justify-content: space-between;"
    //   >
    //     <a href="#main" class="skip">{i18n.str`Skip to main content`}</a>
    //     <div style="max-width: 50em; margin-left: 2em; margin-right: 2em;">
    //       <h1>
    //         <span class="it">
    //           <a href="/">{bankUiSettings.bankName}</a>
    //         </span>
    //       </h1>
    //       {maybeDemoContent(
    //         <p>
    //           {IS_PUBLIC_ACCOUNT_ENABLED ? (
    //             <i18n.Translate>
    //               This part of the demo shows how a bank that supports Taler
    //               directly would work. In addition to using your own bank
    //               account, you can also see the transaction history of some{" "}
    //               <a href="/public-accounts">Public Accounts</a>.
    //             </i18n.Translate>
    //           ) : (
    //             <i18n.Translate>
    //               This part of the demo shows how a bank that supports Taler
    //               directly would work.
    //             </i18n.Translate>
    //           )}
    //         </p>,
    //       )}
    //     </div>
    //   </header>
    //   <div style="display:flex; flex-direction: column;" class="navcontainer">
    //     <nav class="demolist">
    //       {maybeDemoContent(<Fragment>{demo_sites}</Fragment>)}
    //       {backend.state.status === "loggedIn" ? (
    //         <Fragment>
    //           {goToBusinessAccount && !backend.state.isUserAdministrator ? (
    //             <MaybeBusinessButton
    //               account={backend.state.username}
    //               onClick={goToBusinessAccount}
    //             />
    //           ) : undefined}

    //           <LangSelector />

    //           <a
    //             href="#"
    //             class="pure-button logout-button"
    //             onClick={() => {
    //               backend.logOut();
    //               updateSettings("currentWithdrawalOperationId", undefined);
    //             }}
    //           >{i18n.str`Logout`}</a>
    //         </Fragment>
    //       ) : undefined}
    //     </nav>
    //   </div>
    //   <section id="main" class="content">
    //     <StatusBanner />
    //     {children}
    //   </section>
    //   <section id="footer" class="footer">
    //     <hr />
    //     <div>
    //       <p>
    //         You can learn more about GNU Taler on our{" "}
    //         <a href="https://taler.net">main website</a>.
    //       </p>
    //     </div>
    //     <div style="flex-grow:1" />
    //     <p>
    //       Copyright &copy; 2014&mdash;2022 Taler Systems SA. {versionText}{" "}
    //       <TestingTag />
    //     </p>
    //   </section>
    // </Fragment>
  );
}

function maybeDemoContent(content: VNode): VNode {
  if (bankUiSettings.showDemoNav) {
    return content;
  }
  return <Fragment />;
}

export function ErrorBannerFloat({
  error,
  onClear,
}: {
  error: ErrorMessage;
  onClear?: () => void;
}): VNode {
  return (
    <div
      style={{
        position: "fixed",
        top: 10,
        zIndex: 200,
        width: "90%",
      }}
    >
      <ErrorBanner error={error} onClear={onClear} />
    </div>
  );
}

function ErrorBanner({
  error,
  onClear,
}: {
  error: ErrorMessage;
  onClear?: () => void;
}): VNode {
  return (
    <div
      class="informational informational-fail"
      style={{
        marginTop: 8,
        paddingLeft: 16,
        paddingRight: 16,
      }}
    >
      <div style={{ display: "flex", justifyContent: "space-between" }}>
        <p>
          <b>{error.title}</b>
        </p>
        <div style={{ marginTop: "auto", marginBottom: "auto" }}>
          {onClear && (
            <input
              type="button"
              class="pure-button"
              value="Clear"
              onClick={(e) => {
                e.preventDefault();
                onClear();
              }}
            />
          )}
        </div>
      </div>
      <p>{error.description}</p>
    </div>
  );
}

function StatusBanner(): VNode | null {
  const [info, setInfo] = useState<TranslatedString>();
  const [error, setError] = useState<ErrorMessage>();
  useEffect(() => {
    return onNotificationUpdate((newValue) => {
      if (newValue === undefined) {
        setInfo(undefined);
        setError(undefined);
      } else {
        if (newValue.type === "error") {
          setError(newValue.error);
        } else {
          setInfo(newValue.info);
        }
      }
    });
  }, []);
  return (
    <div
      style={{
        position: "fixed",
        top: 10,
        zIndex: 200,
        width: "90%",
      }}
    >
      {!info ? undefined : (
        <div
          class="informational informational-ok"
          style={{ marginTop: 8, paddingLeft: 16, paddingRight: 16 }}
        >
          <div style={{ display: "flex", justifyContent: "space-between" }}>
            <p>
              <b>{info}</b>
            </p>
            <div>
              <input
                type="button"
                class="pure-button"
                value="Clear"
                onClick={async () => {
                  setInfo(undefined);
                }}
              />
            </div>
          </div>
        </div>
      )}
      {!error ? undefined : (
        <ErrorBanner
          error={error}
          onClear={() => {
            setError(undefined);
          }}
        />
      )}
    </div>
  );
}

function TestingTag(): VNode {
  const testingUrl = localStorage.getItem("bank-base-url");
  if (!testingUrl) return <Fragment />;
  return (
    <p class="text-xs leading-5 text-gray-300">
      Testing with {testingUrl}{" "}
      <a
        href=""
        onClick={(e) => {
          e.preventDefault();
          localStorage.removeItem("bank-base-url");
          window.location.reload();
        }}
      >
        stop testing
      </a>
    </p>
  );
}

function Footer() {
  return (
    <footer class="absolute bottom-4">
      <div class="mt-8 mx-8 md:order-1 md:mt-0">
        <div>
          <p class="text-xs leading-5 text-gray-400">
            You can learn more about GNU Taler on our{" "}
            <a class="font-semibold text-gray-500 hover:text-gray-400" href="https://taler.net">main website</a>.
          </p>
        </div>
        <div style="flex-grow:1" />
        <p class="text-xs leading-5 text-gray-400">
          Copyright &copy; 2014&mdash;2022 Taler Systems SA. {versionText}{" "}
          <TestingTag />
        </p>
      </div>
    </footer>
  );
}

function WelcomeAccount(): VNode {
  const { i18n } = useTranslationContext();
  const account = "Sebastian"
  const payto: PaytoUriIBAN = parsePaytoUri("payto://iban/bank.localhost/DE955922") as PaytoUriIBAN
  return <i18n.Translate>
    Welcome,  {account} (<a href={stringifyPaytoUri(payto)}>{payto.iban}</a>)! <CopyButton getContent={() => stringifyPaytoUri(payto)} />
  </i18n.Translate>

}