import { LibtoolVersion } from "@gnu-taler/taler-util"; import { ErrorType, HttpError, HttpResponseServerError, RequestError, useApiContext } from "@gnu-taler/web-util/browser"; import { useEffect, useState } from "preact/hooks"; import { getInitialBackendBaseURL } from "./backend.js"; /** * Protocol version spoken with the bank. * * Uses libtool's current:revision:age versioning. */ export const BANK_INTEGRATION_PROTOCOL_VERSION = "0:0:0"; async function getConfigState( request: ReturnType["request"], ): Promise { const url = getInitialBackendBaseURL(); const result = await request(url, `config`); return result.data; } type Result = undefined | { type: "ok", result: SandboxBackend.Config } | { type: "wrong", result: SandboxBackend.Config } | { type: "error", result: HttpError } export function useConfigState(): Result { const [checked, setChecked] = useState() const { request } = useApiContext(); useEffect(() => { getConfigState(request) .then((result) => { const r = LibtoolVersion.compare(BANK_INTEGRATION_PROTOCOL_VERSION, result.version) if (r?.compatible) { setChecked({ type: "ok",result }); } else { setChecked({ type: "wrong",result }) } }) .catch((error: unknown) => { if (error instanceof RequestError) { const result = error.cause setChecked({ type:"error", result }); } }); }, []); return checked; }