From a8861c9deb9521194f513688af6115cf66e511d7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 28 Jun 2024 13:12:28 -0300 Subject: better header and footer --- packages/challenger-ui/src/Routing.tsx | 2 +- packages/challenger-ui/src/context/preferences.ts | 87 ++++++++++++ packages/challenger-ui/src/declaration.d.ts | 35 +++++ packages/challenger-ui/src/hooks/session.ts | 6 +- .../challenger-ui/src/pages/AnswerChallenge.tsx | 5 +- packages/challenger-ui/src/pages/AskChallenge.tsx | 4 +- packages/challenger-ui/src/pages/Frame.tsx | 151 +++++++++++++++------ 7 files changed, 241 insertions(+), 49 deletions(-) create mode 100644 packages/challenger-ui/src/context/preferences.ts create mode 100644 packages/challenger-ui/src/declaration.d.ts 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 + */ + +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 => + buildCodecForObject() + .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, + (key: T, value: Preferences[T]) => void, +] { + const { value, update } = useLocalStorage( + PREFERENCES_KEY, + defaultPreferences, + ); + + function updateField(k: T, v: Preferences[T]) { + const newValue = { ...value, [k]: v }; + update(newValue); + } + return [value, updateField]; +} + +export function getAllBooleanPreferences(): Array { + return [ + "showChallangeSetup", + "showDebugInfo", + ]; +} + +export function getLabelForPreferences( + k: keyof Preferences, + i18n: ReturnType["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 + */ + +declare module "*.css" { + const mapping: Record; + 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 => buildCodecForObject() .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 */ 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 */ -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 */ +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 ( - -
-
-
-
- - GNU Taler - -
- - Challenger - +
+
+
  • +
    + Preferences
    -
    -
    +
      + {getAllBooleanPreferences().map((set) => { + const isOn: boolean = !!preferences[set]; + return ( +
    • +
      + + + {getLabelForPreferences(set, i18n)} + + + +
      +
    • + ); + })} +
    +
  • +
    + +
    +
    +
    -
    +
    {children}
    - -
    -
    -
    -

    - Learn more about{" "} - - GNU Taler - -

    -
    -
    -

    - Copyright © 2014—2023 Taler Systems SA.{" "} -

    -
    -
    -
    + +