diff options
-rw-r--r-- | packages/challenger-ui/src/Routing.tsx | 2 | ||||
-rw-r--r-- | packages/challenger-ui/src/context/preferences.ts | 87 | ||||
-rw-r--r-- | packages/challenger-ui/src/declaration.d.ts | 35 | ||||
-rw-r--r-- | packages/challenger-ui/src/hooks/session.ts | 6 | ||||
-rw-r--r-- | packages/challenger-ui/src/pages/AnswerChallenge.tsx | 5 | ||||
-rw-r--r-- | packages/challenger-ui/src/pages/AskChallenge.tsx | 4 | ||||
-rw-r--r-- | packages/challenger-ui/src/pages/Frame.tsx | 151 |
7 files changed, 241 insertions, 49 deletions
diff --git a/packages/challenger-ui/src/Routing.tsx b/packages/challenger-ui/src/Routing.tsx index 7f9f52a19..f7488cb8d 100644 --- a/packages/challenger-ui/src/Routing.tsx +++ b/packages/challenger-ui/src/Routing.tsx @@ -23,6 +23,7 @@ import { import { Fragment, VNode, h } from "preact"; import { assertUnreachable } from "@gnu-taler/taler-util"; +import { useErrorBoundary } from "preact/hooks"; import { CheckChallengeIsUpToDate } from "./components/CheckChallengeIsUpToDate.js"; import { SessionId, useSessionState } from "./hooks/session.js"; import { AnswerChallenge } from "./pages/AnswerChallenge.js"; @@ -32,7 +33,6 @@ import { Frame } from "./pages/Frame.js"; import { MissingParams } from "./pages/MissingParams.js"; import { NonceNotFound } from "./pages/NonceNotFound.js"; import { Setup } from "./pages/Setup.js"; -import { useErrorBoundary } from "preact/hooks"; export function Routing(): VNode { // check session and defined if this is diff --git a/packages/challenger-ui/src/context/preferences.ts b/packages/challenger-ui/src/context/preferences.ts new file mode 100644 index 000000000..3188bd71c --- /dev/null +++ b/packages/challenger-ui/src/context/preferences.ts @@ -0,0 +1,87 @@ +/* + 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 { + Codec, + TranslatedString, + buildCodecForObject, + codecForBoolean, +} from "@gnu-taler/taler-util"; +import { + buildStorageKey, + useLocalStorage, + useTranslationContext, +} from "@gnu-taler/web-util/browser"; + +interface Preferences { + showChallangeSetup: boolean; + showDebugInfo: boolean; +} + +export const codecForPreferences = (): Codec<Preferences> => + buildCodecForObject<Preferences>() + .property("showChallangeSetup", codecForBoolean()) + .property("showDebugInfo", codecForBoolean()) + .build("Preferences"); + +const defaultPreferences: Preferences = { + showChallangeSetup: false, + showDebugInfo: false, +}; + +const PREFERENCES_KEY = buildStorageKey( + "challenger-preferences", + codecForPreferences(), +); +/** + * User preferences. + * + * @returns tuple of [state, update()] + */ +export function usePreferences(): [ + Readonly<Preferences>, + <T extends keyof Preferences>(key: T, value: Preferences[T]) => void, +] { + const { value, update } = useLocalStorage( + PREFERENCES_KEY, + defaultPreferences, + ); + + function updateField<T extends keyof Preferences>(k: T, v: Preferences[T]) { + const newValue = { ...value, [k]: v }; + update(newValue); + } + return [value, updateField]; +} + +export function getAllBooleanPreferences(): Array<keyof Preferences> { + return [ + "showChallangeSetup", + "showDebugInfo", + ]; +} + +export function getLabelForPreferences( + k: keyof Preferences, + i18n: ReturnType<typeof useTranslationContext>["i18n"], +): TranslatedString { + switch (k) { + case "showChallangeSetup": + return i18n.str`Show challenger setup screen`; + case "showDebugInfo": + return i18n.str`Show debug info`; + } +} diff --git a/packages/challenger-ui/src/declaration.d.ts b/packages/challenger-ui/src/declaration.d.ts new file mode 100644 index 000000000..581cbcd07 --- /dev/null +++ b/packages/challenger-ui/src/declaration.d.ts @@ -0,0 +1,35 @@ +/* + 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/> + */ + +declare module "*.css" { + const mapping: Record<string, string>; + export default mapping; +} +declare module "*.svg" { + const content: string; + export default content; +} +declare module "*.jpeg" { + const content: string; + export default content; +} +declare module "*.png" { + const content: string; + export default content; +} + +declare const __VERSION__: string; +declare const __GIT_HASH__: string; diff --git a/packages/challenger-ui/src/hooks/session.ts b/packages/challenger-ui/src/hooks/session.ts index eeb330493..54eeb2fdc 100644 --- a/packages/challenger-ui/src/hooks/session.ts +++ b/packages/challenger-ui/src/hooks/session.ts @@ -15,9 +15,11 @@ */ import { + AbsoluteTime, ChallengerApi, Codec, buildCodecForObject, + codecForAbsoluteTime, codecForBoolean, codecForChallengeStatus, codecForNumber, @@ -40,7 +42,7 @@ export type SessionId = { export type LastChallengeResponse = { attemptsLeft: number; - nextSend: string; + nextSend: AbsoluteTime; transmitted: boolean; }; @@ -52,7 +54,7 @@ export type SessionState = SessionId & { export const codecForLastChallengeResponse = (): Codec<LastChallengeResponse> => buildCodecForObject<LastChallengeResponse>() .property("attemptsLeft", codecForNumber()) - .property("nextSend", codecForString()) + .property("nextSend", codecForAbsoluteTime) .property("transmitted", codecForBoolean()) .build("LastChallengeResponse"); diff --git a/packages/challenger-ui/src/pages/AnswerChallenge.tsx b/packages/challenger-ui/src/pages/AnswerChallenge.tsx index 5fe2d9743..b5b3b74b0 100644 --- a/packages/challenger-ui/src/pages/AnswerChallenge.tsx +++ b/packages/challenger-ui/src/pages/AnswerChallenge.tsx @@ -14,6 +14,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ import { + AbsoluteTime, ChallengerApi, HttpStatusCode, assertUnreachable, @@ -80,7 +81,9 @@ export function AnswerChallenge({ } else { accepted({ attemptsLeft: ok.body.attempts_left, - nextSend: ok.body.next_tx_time, + nextSend: AbsoluteTime.fromProtocolTimestamp( + ok.body.next_tx_time, + ), transmitted: ok.body.transmitted, }); } diff --git a/packages/challenger-ui/src/pages/AskChallenge.tsx b/packages/challenger-ui/src/pages/AskChallenge.tsx index 829cdaccc..cb0223d90 100644 --- a/packages/challenger-ui/src/pages/AskChallenge.tsx +++ b/packages/challenger-ui/src/pages/AskChallenge.tsx @@ -13,7 +13,7 @@ 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 { HttpStatusCode } from "@gnu-taler/taler-util"; +import { AbsoluteTime, HttpStatusCode } from "@gnu-taler/taler-util"; import { Attention, Button, @@ -92,7 +92,7 @@ export function AskChallenge({ } else { accepted({ attemptsLeft: ok.body.attempts_left, - nextSend: ok.body.next_tx_time, + nextSend: AbsoluteTime.fromProtocolTimestamp( ok.body.next_tx_time), transmitted: ok.body.transmitted, }); } diff --git a/packages/challenger-ui/src/pages/Frame.tsx b/packages/challenger-ui/src/pages/Frame.tsx index 612eced0b..dd2a13d8c 100644 --- a/packages/challenger-ui/src/pages/Frame.tsx +++ b/packages/challenger-ui/src/pages/Frame.tsx @@ -14,56 +14,121 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { + Footer, + Header, + ToastBanner, + notifyError, + notifyException, + useTranslationContext, +} from "@gnu-taler/web-util/browser"; import { ComponentChildren, Fragment, h, VNode } from "preact"; +import { useSettingsContext } from "../context/settings.js"; +import { useEffect, useErrorBoundary } from "preact/hooks"; +import { TranslatedString } from "@gnu-taler/taler-util"; +import { + getAllBooleanPreferences, + getLabelForPreferences, + usePreferences, +} from "../context/preferences.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 ( - <Fragment> - <header class="bg-indigo-600 w-full mx-auto px-2 border-b border-opacity-25 border-indigo-400"> - <div class="flex flex-row h-16 items-center "> - <div class="flex px-2 justify-start"> - <div class="flex-shrink-0 bg-white rounded-lg"> - <a href="#"> - <img - class="h-8 w-auto" - src='data:image/svg+xml,<?xml version="1.0" encoding="UTF-8" standalone="no"?>%0A<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 201 90">%0A <g fill="%230042b3" fill-rule="evenodd" stroke-width=".3">%0A <path d="M86.7 1.1c15.6 0 29 9.4 36 23.2h-5.9A35.1 35.1 0 0086.7 6.5C67 6.5 51 23.6 51 44.7c0 10.4 3.8 19.7 10 26.6a31.4 31.4 0 01-4.2 3A45.2 45.2 0 0146 44.7c0-24 18.2-43.6 40.7-43.6zm35.8 64.3a40.4 40.4 0 01-39 22.8c3-1.5 6-3.5 8.6-5.7a35.6 35.6 0 0024.6-17.1z" />%0A <path d="M64.2 1.1l3.1.1c-3 1.6-5.9 3.5-8.5 5.8a37.5 37.5 0 00-30.2 37.7c0 14.3 7.3 26.7 18 33.3a29.6 29.6 0 01-8.5.2c-9-8-14.6-20-14.6-33.5 0-24 18.2-43.6 40.7-43.6zm5.4 81.4a35.6 35.6 0 0024.6-17.1h5.9a40.4 40.4 0 01-39 22.8c3-1.5 5.9-3.5 8.5-5.7zm24.8-58.2a37 37 0 00-12.6-12.8 29.6 29.6 0 018.5-.2c4 3.6 7.4 8 9.9 13z" />%0A <path d="M41.8 1.1c1 0 2 0 3.1.2-3 1.5-5.9 3.4-8.5 5.6A37.5 37.5 0 006.1 44.7c0 21.1 16 38.3 35.7 38.3 12.6 0 23.6-7 30-17.6h5.8a40.4 40.4 0 01-35.8 23C19.3 88.4 1 68.8 1 44.7c0-24 18.2-43.6 40.7-43.6zm30.1 23.2a38.1 38.1 0 00-4.5-6.1c1.3-1.2 2.7-2.2 4.3-3 2.3 2.7 4.4 5.8 6 9.1z" />%0A </g>%0A <path d="M76.1 34.4h9.2v-5H61.9v5H71v26h5.1zM92.6 52.9h13.7l3 7.4h5.3l-12.7-31.2h-4.7L84.5 60.3h5.2zm11.8-4.9h-9.9l5-12.4zM123.8 29.4h-4.6v31h20.6v-5h-16zM166.5 29.4H145v31h21.6v-5H150v-8.3h14.5v-4.9h-14.5v-8h16.4zM191.2 39.5c0 1.6-.5 2.8-1.6 3.8s-2.6 1.4-4.4 1.4h-7.4V34.3h7.4c1.9 0 3.4.4 4.4 1.3 1 .9 1.6 2.2 1.6 3.9zm6 20.8l-7.7-11.7c1-.3 1.9-.7 2.7-1.3a8.8 8.8 0 003.6-4.6c.4-1 .5-2.2.5-3.5 0-1.5-.2-2.9-.7-4.1a8.4 8.4 0 00-2.1-3.1c-1-.8-2-1.5-3.4-2-1.3-.4-2.8-.6-4.5-.6h-12.9v31h5V49.4h6.5l7 10.8z" />%0A</svg>' - alt="GNU Taler" - style="height: 1.5rem; margin: 0.5rem;" - /> - </a> - </div> - <span class="flex items-center text-white text-lg font-bold ml-4"> - Challenger - </span> + <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> - <div class="block flex-1 ml-6 "></div> - <div class="flex justify-end"></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> - </header> + </div> <main class="flex-1">{children}</main> - - <footer class="bottom-4 mb-4"> - <div class="mt-8 mx-8 md:order-1 md:mt-0"> - <div> - <p class="text-xs leading-5 text-gray-400"> - Learn more about{" "} - <a - target="_blank" - rel="noreferrer noopener" - class="font-semibold text-gray-500 hover:text-gray-400" - href="https://taler.net" - > - GNU Taler - </a> - </p> - </div> - <div style="flex-grow: 1;"></div> - <p class="text-xs leading-5 text-gray-400"> - Copyright © 2014—2023 Taler Systems SA.{" "} - </p> - </div> - </footer> - </Fragment> + + <Footer + testingUrlKey="challenger-base-url" + GIT_HASH={GIT_HASH} + VERSION={VERSION} + /> + </div> ); } |