diff options
author | Sebastian <sebasjm@gmail.com> | 2021-11-15 11:18:58 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-11-15 11:18:58 -0300 |
commit | 1d4815c66c395f4fcc86c30e20f3d005e3cb9ff5 (patch) | |
tree | 99e8241a5eb5af4d752be93a460004bc0c6255aa /packages/taler-wallet-webextension/src/hooks | |
parent | 9692f589c687a2ba39a705ca4238cf123f444c61 (diff) | |
download | wallet-core-1d4815c66c395f4fcc86c30e20f3d005e3cb9ff5.tar.gz wallet-core-1d4815c66c395f4fcc86c30e20f3d005e3cb9ff5.tar.bz2 wallet-core-1d4815c66c395f4fcc86c30e20f3d005e3cb9ff5.zip |
prettier
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks')
10 files changed, 97 insertions, 70 deletions
diff --git a/packages/taler-wallet-webextension/src/hooks/useAsyncAsHook.ts b/packages/taler-wallet-webextension/src/hooks/useAsyncAsHook.ts index 2131d45cb..aa6695c3e 100644 --- a/packages/taler-wallet-webextension/src/hooks/useAsyncAsHook.ts +++ b/packages/taler-wallet-webextension/src/hooks/useAsyncAsHook.ts @@ -29,7 +29,7 @@ interface HookError { export type HookResponse<T> = HookOk<T> | HookError | undefined; -export function useAsyncAsHook<T> (fn: (() => Promise<T>)): HookResponse<T> { +export function useAsyncAsHook<T>(fn: () => Promise<T>): HookResponse<T> { const [result, setHookResponse] = useState<HookResponse<T>>(undefined); useEffect(() => { async function doAsync() { @@ -42,7 +42,7 @@ export function useAsyncAsHook<T> (fn: (() => Promise<T>)): HookResponse<T> { } } } - doAsync() + doAsync(); }, []); return result; } diff --git a/packages/taler-wallet-webextension/src/hooks/useBackupDeviceName.ts b/packages/taler-wallet-webextension/src/hooks/useBackupDeviceName.ts index f3b1b3b5f..1aa711a90 100644 --- a/packages/taler-wallet-webextension/src/hooks/useBackupDeviceName.ts +++ b/packages/taler-wallet-webextension/src/hooks/useBackupDeviceName.ts @@ -17,34 +17,31 @@ import { useEffect, useState } from "preact/hooks"; import * as wxApi from "../wxApi"; - export interface BackupDeviceName { name: string; - update: (s:string) => Promise<void> + update: (s: string) => Promise<void>; } - export function useBackupDeviceName(): BackupDeviceName { const [status, setStatus] = useState<BackupDeviceName>({ - name: '', - update: () => Promise.resolve() - }) + name: "", + update: () => Promise.resolve(), + }); useEffect(() => { async function run() { //create a first list of backup info by currency - const status = await wxApi.getBackupInfo() + const status = await wxApi.getBackupInfo(); async function update(newName: string) { - await wxApi.setWalletDeviceId(newName) - setStatus(old => ({ ...old, name: newName })) + await wxApi.setWalletDeviceId(newName); + setStatus((old) => ({ ...old, name: newName })); } - setStatus({ name: status.deviceId, update }) + setStatus({ name: status.deviceId, update }); } - run() - }, []) + run(); + }, []); - return status + return status; } - diff --git a/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts b/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts index c46ab6a5f..8a8fd6f2f 100644 --- a/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts +++ b/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts @@ -14,11 +14,15 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { ProviderInfo, ProviderPaymentPaid, ProviderPaymentStatus, ProviderPaymentType } from "@gnu-taler/taler-wallet-core"; +import { + ProviderInfo, + ProviderPaymentPaid, + ProviderPaymentStatus, + ProviderPaymentType, +} from "@gnu-taler/taler-wallet-core"; import { useEffect, useState } from "preact/hooks"; import * as wxApi from "../wxApi"; - export interface BackupStatus { deviceName: string; providers: ProviderInfo[]; @@ -32,40 +36,46 @@ function getStatusTypeOrder(t: ProviderPaymentStatus) { ProviderPaymentType.Unpaid, ProviderPaymentType.Paid, ProviderPaymentType.Pending, - ].indexOf(t.type) + ].indexOf(t.type); } function getStatusPaidOrder(a: ProviderPaymentPaid, b: ProviderPaymentPaid) { - return a.paidUntil.t_ms === 'never' ? -1 : - b.paidUntil.t_ms === 'never' ? 1 : - a.paidUntil.t_ms - b.paidUntil.t_ms + return a.paidUntil.t_ms === "never" + ? -1 + : b.paidUntil.t_ms === "never" + ? 1 + : a.paidUntil.t_ms - b.paidUntil.t_ms; } export function useBackupStatus(): BackupStatus | undefined { - const [status, setStatus] = useState<BackupStatus | undefined>(undefined) + const [status, setStatus] = useState<BackupStatus | undefined>(undefined); useEffect(() => { async function run() { //create a first list of backup info by currency - const status = await wxApi.getBackupInfo() + const status = await wxApi.getBackupInfo(); const providers = status.providers.sort((a, b) => { - if (a.paymentStatus.type === ProviderPaymentType.Paid && b.paymentStatus.type === ProviderPaymentType.Paid) { - return getStatusPaidOrder(a.paymentStatus, b.paymentStatus) + if ( + a.paymentStatus.type === ProviderPaymentType.Paid && + b.paymentStatus.type === ProviderPaymentType.Paid + ) { + return getStatusPaidOrder(a.paymentStatus, b.paymentStatus); } - return getStatusTypeOrder(a.paymentStatus) - getStatusTypeOrder(b.paymentStatus) - }) + return ( + getStatusTypeOrder(a.paymentStatus) - + getStatusTypeOrder(b.paymentStatus) + ); + }); async function sync() { - await wxApi.syncAllProviders() + await wxApi.syncAllProviders(); } - - setStatus({ deviceName: status.deviceId, providers, sync }) + + setStatus({ deviceName: status.deviceId, providers, sync }); } - run() - }, []) + run(); + }, []); - return status + return status; } - - diff --git a/packages/taler-wallet-webextension/src/hooks/useBalances.ts b/packages/taler-wallet-webextension/src/hooks/useBalances.ts index 37424fb05..403ce7b87 100644 --- a/packages/taler-wallet-webextension/src/hooks/useBalances.ts +++ b/packages/taler-wallet-webextension/src/hooks/useBalances.ts @@ -18,7 +18,6 @@ import { BalancesResponse } from "@gnu-taler/taler-util"; import { useEffect, useState } from "preact/hooks"; import * as wxApi from "../wxApi"; - interface BalancesHookOk { hasError: false; response: BalancesResponse; @@ -46,7 +45,7 @@ export function useBalances(): BalancesHook { } } } - checkBalance() + checkBalance(); return wxApi.onUpdateNotification(checkBalance); }, []); diff --git a/packages/taler-wallet-webextension/src/hooks/useDiagnostics.ts b/packages/taler-wallet-webextension/src/hooks/useDiagnostics.ts index 888d4d5f1..48aff2602 100644 --- a/packages/taler-wallet-webextension/src/hooks/useDiagnostics.ts +++ b/packages/taler-wallet-webextension/src/hooks/useDiagnostics.ts @@ -21,7 +21,7 @@ import * as wxApi from "../wxApi"; export function useDiagnostics(): [WalletDiagnostics | undefined, boolean] { const [timedOut, setTimedOut] = useState(false); const [diagnostics, setDiagnostics] = useState<WalletDiagnostics | undefined>( - undefined + undefined, ); useEffect(() => { @@ -41,5 +41,5 @@ export function useDiagnostics(): [WalletDiagnostics | undefined, boolean] { console.log("fetching diagnostics"); doFetch(); }, []); - return [diagnostics, timedOut] -}
\ No newline at end of file + return [diagnostics, timedOut]; +} diff --git a/packages/taler-wallet-webextension/src/hooks/useExtendedPermissions.ts b/packages/taler-wallet-webextension/src/hooks/useExtendedPermissions.ts index a92425760..aaab0aa43 100644 --- a/packages/taler-wallet-webextension/src/hooks/useExtendedPermissions.ts +++ b/packages/taler-wallet-webextension/src/hooks/useExtendedPermissions.ts @@ -19,13 +19,12 @@ import * as wxApi from "../wxApi"; import { getPermissionsApi } from "../compat"; import { extendedPermissions } from "../permissions"; - export function useExtendedPermissions(): [boolean, () => void] { const [enabled, setEnabled] = useState(false); const toggle = () => { - setEnabled(v => !v); - handleExtendedPerm(enabled).then(result => { + setEnabled((v) => !v); + handleExtendedPerm(enabled).then((result) => { setEnabled(result); }); }; @@ -65,5 +64,5 @@ async function handleExtendedPerm(isEnabled: boolean): Promise<boolean> { nextVal = res.newValue; } console.log("new permissions applied:", nextVal ?? false); - return nextVal ?? false -}
\ No newline at end of file + return nextVal ?? false; +} diff --git a/packages/taler-wallet-webextension/src/hooks/useLang.ts b/packages/taler-wallet-webextension/src/hooks/useLang.ts index 70b9614f6..cc4ff3fc8 100644 --- a/packages/taler-wallet-webextension/src/hooks/useLang.ts +++ b/packages/taler-wallet-webextension/src/hooks/useLang.ts @@ -14,10 +14,13 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { useNotNullLocalStorage } from './useLocalStorage'; +import { useNotNullLocalStorage } from "./useLocalStorage"; -export function useLang(initial?: string): [string, (s:string) => void] { - const browserLang: string | undefined = typeof window !== "undefined" ? navigator.language || (navigator as any).userLanguage : undefined; - const defaultLang = (browserLang || initial || 'en').substring(0, 2) - return useNotNullLocalStorage('lang-preference', defaultLang) +export function useLang(initial?: string): [string, (s: string) => void] { + const browserLang: string | undefined = + typeof window !== "undefined" + ? navigator.language || (navigator as any).userLanguage + : undefined; + const defaultLang = (browserLang || initial || "en").substring(0, 2); + return useNotNullLocalStorage("lang-preference", defaultLang); } diff --git a/packages/taler-wallet-webextension/src/hooks/useLocalStorage.ts b/packages/taler-wallet-webextension/src/hooks/useLocalStorage.ts index 78a8b65d5..3883aff04 100644 --- a/packages/taler-wallet-webextension/src/hooks/useLocalStorage.ts +++ b/packages/taler-wallet-webextension/src/hooks/useLocalStorage.ts @@ -15,38 +15,52 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ import { StateUpdater, useState } from "preact/hooks"; -export function useLocalStorage(key: string, initialValue?: string): [string | undefined, StateUpdater<string | undefined>] { - const [storedValue, setStoredValue] = useState<string | undefined>((): string | undefined => { - return typeof window !== "undefined" ? window.localStorage.getItem(key) || initialValue : initialValue; +export function useLocalStorage( + key: string, + initialValue?: string, +): [string | undefined, StateUpdater<string | undefined>] { + const [storedValue, setStoredValue] = useState<string | undefined>((): + | string + | undefined => { + return typeof window !== "undefined" + ? window.localStorage.getItem(key) || initialValue + : initialValue; }); - const setValue = (value?: string | ((val?: string) => string | undefined)) => { - setStoredValue(p => { - const toStore = value instanceof Function ? value(p) : value + const setValue = ( + value?: string | ((val?: string) => string | undefined), + ) => { + setStoredValue((p) => { + const toStore = value instanceof Function ? value(p) : value; if (typeof window !== "undefined") { if (!toStore) { - window.localStorage.removeItem(key) + window.localStorage.removeItem(key); } else { window.localStorage.setItem(key, toStore); } } - return toStore - }) + return toStore; + }); }; return [storedValue, setValue]; } //TODO: merge with the above function -export function useNotNullLocalStorage(key: string, initialValue: string): [string, StateUpdater<string>] { +export function useNotNullLocalStorage( + key: string, + initialValue: string, +): [string, StateUpdater<string>] { const [storedValue, setStoredValue] = useState<string>((): string => { - return typeof window !== "undefined" ? window.localStorage.getItem(key) || initialValue : initialValue; + return typeof window !== "undefined" + ? window.localStorage.getItem(key) || initialValue + : initialValue; }); const setValue = (value: string | ((val: string) => string)) => { @@ -54,7 +68,7 @@ export function useNotNullLocalStorage(key: string, initialValue: string): [stri setStoredValue(valueToStore); if (typeof window !== "undefined") { if (!valueToStore) { - window.localStorage.removeItem(key) + window.localStorage.removeItem(key); } else { window.localStorage.setItem(key, valueToStore); } diff --git a/packages/taler-wallet-webextension/src/hooks/useProviderStatus.ts b/packages/taler-wallet-webextension/src/hooks/useProviderStatus.ts index 6520848a5..ea167463e 100644 --- a/packages/taler-wallet-webextension/src/hooks/useProviderStatus.ts +++ b/packages/taler-wallet-webextension/src/hooks/useProviderStatus.ts @@ -32,7 +32,9 @@ export function useProviderStatus(url: string): ProviderStatus | undefined { //create a first list of backup info by currency const status = await wxApi.getBackupInfo(); - const providers = status.providers.filter(p => p.syncProviderBaseUrl === url); + const providers = status.providers.filter( + (p) => p.syncProviderBaseUrl === url, + ); const info = providers.length ? providers[0] : undefined; async function sync() { diff --git a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts index ff9cc029a..96a278401 100644 --- a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts +++ b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts @@ -17,15 +17,18 @@ import { classifyTalerUri, TalerUriType } from "@gnu-taler/taler-util"; import { useEffect, useState } from "preact/hooks"; -export function useTalerActionURL(): [string | undefined, (s: boolean) => void] { +export function useTalerActionURL(): [ + string | undefined, + (s: boolean) => void, +] { const [talerActionUrl, setTalerActionUrl] = useState<string | undefined>( - undefined + undefined, ); const [dismissed, setDismissed] = useState(false); useEffect(() => { async function check(): Promise<void> { const talerUri = await findTalerUriInActiveTab(); - setTalerActionUrl(talerUri) + setTalerActionUrl(talerUri); } check(); }, []); |