summaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks/config.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/demobank-ui/src/hooks/config.ts')
-rw-r--r--packages/demobank-ui/src/hooks/config.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/demobank-ui/src/hooks/config.ts b/packages/demobank-ui/src/hooks/config.ts
new file mode 100644
index 000000000..4b22e8ad3
--- /dev/null
+++ b/packages/demobank-ui/src/hooks/config.ts
@@ -0,0 +1,51 @@
+import { LibtoolVersion } from "@gnu-taler/taler-util";
+import { 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<typeof useApiContext>["request"],
+): Promise<SandboxBackend.Config | undefined> {
+ try {
+ const url = getInitialBackendBaseURL();
+ const result = await request<SandboxBackend.Config>(
+ url,
+ `config`,
+ );
+ return result.data;
+ } catch (error) {
+ return undefined;
+ }
+}
+
+export function useConfigState(): boolean | undefined {
+ const [checked, setChecked] = useState<boolean>()
+ const { request } = useApiContext();
+
+ useEffect(() => {
+
+ getConfigState(request)
+ .then((result) => {
+ if (!result) {
+ setChecked(false)
+ } else {
+ const r = LibtoolVersion.compare(BANK_INTEGRATION_PROTOCOL_VERSION, result.version)
+ setChecked(r?.compatible);
+ }
+ })
+ .catch((error) => {
+ setChecked(false);
+ });
+ });
+
+ return checked;
+}
+
+