aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-01-09 20:20:09 -0300
committerSebastian <sebasjm@gmail.com>2023-01-09 20:20:09 -0300
commit4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7 (patch)
tree5c16976f99eb973ff62d78ed64107ca01df57b99 /packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts
parent8a70edb2f8e235c3462127b0aa4e1b65aa1aee0b (diff)
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
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts')
-rw-r--r--packages/taler-wallet-webextension/src/hooks/useAutoOpenPermissions.ts69
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;
-}