From 4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 9 Jan 2023 20:20:09 -0300 Subject: fix #7153: more error handling if handler do not trap error then fail at compile time, all safe handlers push alert on error errors are typed so they render good information --- .../src/hooks/useAutoOpenPermissions.ts | 69 +++++++++------------- 1 file changed, 29 insertions(+), 40 deletions(-) (limited to 'packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts') diff --git a/packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts b/packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts index cf2fd880e..e0a34f690 100644 --- a/packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts +++ b/packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts @@ -14,23 +14,41 @@ GNU Taler; see the file COPYING. If not, see */ -import { TalerError } from "@gnu-taler/taler-wallet-core"; import { useEffect, useState } from "preact/hooks"; +import { useAlertContext } from "../context/alert.js"; import { useBackendContext } from "../context/backend.js"; import { ToggleHandler } from "../mui/handlers.js"; import { platform } from "../platform/foreground.js"; export function useAutoOpenPermissions(): ToggleHandler { const api = useBackendContext(); + const { pushAlertOnError } = useAlertContext(); const [enabled, setEnabled] = useState(false); - const [error, setError] = useState(); - const toggle = async (): Promise => { - return handleAutoOpenPerm(enabled, setEnabled, api.background).catch( - (e) => { - setError(TalerError.fromException(e)); - }, - ); - }; + + async function handleAutoOpenPerm(): Promise { + if (!enabled) { + // We set permissions here, since apparently FF wants this to be done + // as the result of an input event ... + let granted: boolean; + try { + granted = await platform.getPermissionsApi().requestHostPermissions(); + } catch (lastError) { + setEnabled(false); + throw lastError; + } + const res = await api.background.call("toggleHeaderListener", granted); + setEnabled(res.newValue); + } else { + try { + await api.background + .call("toggleHeaderListener", false) + .then((r) => setEnabled(r.newValue)); + } catch (e) { + console.log(e); + } + } + return; + } useEffect(() => { async function getValue(): Promise { @@ -42,40 +60,11 @@ export function useAutoOpenPermissions(): ToggleHandler { } getValue(); }, []); + return { value: enabled, button: { - onClick: toggle, - error, + onClick: pushAlertOnError(handleAutoOpenPerm), }, }; } - -async function handleAutoOpenPerm( - isEnabled: boolean, - onChange: (value: boolean) => void, - background: ReturnType["background"], -): Promise { - if (!isEnabled) { - // We set permissions here, since apparently FF wants this to be done - // as the result of an input event ... - let granted: boolean; - try { - granted = await platform.getPermissionsApi().requestHostPermissions(); - } catch (lastError) { - onChange(false); - throw lastError; - } - const res = await background.call("toggleHeaderListener", granted); - onChange(res.newValue); - } else { - try { - await background - .call("toggleHeaderListener", false) - .then((r) => onChange(r.newValue)); - } catch (e) { - console.log(e); - } - } - return; -} -- cgit v1.2.3