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; } export function useConfigState(): undefined | true | string | HttpError { const [checked, setChecked] = useState>() const { request } = useApiContext(); useEffect(() => { getConfigState(request) .then((s) => { const r = LibtoolVersion.compare(BANK_INTEGRATION_PROTOCOL_VERSION, s.version) if (r?.compatible) { setChecked(true); } else { setChecked(s.version) } }) .catch((error: unknown) => { if (error instanceof RequestError) { setChecked(error.cause); } }); }, []); return checked; }