aboutsummaryrefslogtreecommitdiff
path: root/packages/challenger-ui/src/pages/Frame.tsx
blob: 7f81b9d778fe739cd4adc171d382adf63c6bea05 (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
/*
 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 { TranslatedString } from "@gnu-taler/taler-util";
import {
  Footer,
  Header,
  ToastBanner,
  notifyError,
  notifyException,
  useTranslationContext,
} from "@gnu-taler/web-util/browser";
import { ComponentChildren, Fragment, VNode, h } from "preact";
import { useEffect, useErrorBoundary } from "preact/hooks";
import {
  getAllBooleanPreferences,
  getLabelForPreferences,
  usePreferences,
} from "../context/preferences.js";
import { useSettingsContext } from "../context/settings.js";

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

export function Frame({ children }: { children: ComponentChildren }): VNode {
  const settings = useSettingsContext();
  const [preferences, updatePreferences] = usePreferences();

  const [error, resetError] = useErrorBoundary();
  const { i18n } = useTranslationContext();
  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;"
    >
      <Header
        title="Challenger"
        onLogout={undefined}
        iconLinkURL="#"
        sites={preferences.showChallangeSetup ? [
          ["New challenge","#/setup/1"]
        ] :[]}
        supportedLangs={["en"]}
      >
        <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 class="fixed z-20 top-14 w-full">
        <div class="mx-auto w-4/5">
          <ToastBanner />
        </div>
      </div>

      <main class="flex-1">{children}</main>

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