diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts | 69 |
1 files changed, 29 insertions, 40 deletions
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 <http://www.gnu.org/licenses/> */ -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<TalerError | undefined>(); - const toggle = async (): Promise<void> => { - return handleAutoOpenPerm(enabled, setEnabled, api.background).catch( - (e) => { - setError(TalerError.fromException(e)); - }, - ); - }; + + async function handleAutoOpenPerm(): Promise<void> { + 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<void> { @@ -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<typeof useBackendContext>["background"], -): Promise<void> { - 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; -} |