From a1c5917e626856f2abd9dbe6ddaa71c1458334c6 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Fri, 26 Apr 2024 14:31:48 -0300
Subject: update code to match others

---
 packages/aml-backoffice-ui/src/App.tsx | 97 +++++++++++++++++++++++++---------
 1 file changed, 71 insertions(+), 26 deletions(-)

(limited to 'packages/aml-backoffice-ui/src/App.tsx')

diff --git a/packages/aml-backoffice-ui/src/App.tsx b/packages/aml-backoffice-ui/src/App.tsx
index 55f03322d..d55de776b 100644
--- a/packages/aml-backoffice-ui/src/App.tsx
+++ b/packages/aml-backoffice-ui/src/App.tsx
@@ -1,6 +1,6 @@
 /*
  This file is part of GNU Taler
- (C) 2022 Taler Systems S.A.
+ (C) 2022-2024 Taler Systems S.A.
 
  GNU Taler is free software; you can redistribute it and/or modify it under the
  terms of the GNU General Public License as published by the Free Software
@@ -13,26 +13,46 @@
  You should have received a copy of the GNU General Public License along with
  GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
  */
-import { TranslationProvider } from "@gnu-taler/web-util/browser";
-import { h, VNode } from "preact";
+import { canonicalizeBaseUrl } from "@gnu-taler/taler-util";
+import {
+  BrowserHashNavigationProvider,
+  ExchangeApiProvider,
+  Loading,
+  TranslationProvider,
+} from "@gnu-taler/web-util/browser";
+import { VNode, h } from "preact";
+import { useEffect, useState } from "preact/hooks";
 import { SWRConfig } from "swr";
-import { ExchangeApiProvider } from "./context/config.js";
-import { ExchangeAmlFrame } from "./Dashboard.js";
-import { getInitialBackendBaseURL } from "./hooks/useBackend.js";
-import { Pages } from "./pages.js";
-import { HashPathProvider, Router } from "./route.js";
+import { ExchangeAmlFrame } from "./ExchangeAmlFrame.js";
+import { Routing } from "./Routing.js";
+import { SettingsProvider } from "./context/settings.js";
+import { strings } from "./i18n/strings.js";
 import "./scss/main.css";
+import { UiSettings, fetchSettings } from "./settings.js";
 
 const WITH_LOCAL_STORAGE_CACHE = false;
 
-const pageList = Object.values(Pages);
-
 export function App(): VNode {
-  const baseUrl = getInitialBackendBaseURL();
+  const [settings, setSettings] = useState<UiSettings>();
+  useEffect(() => {
+    fetchSettings(setSettings);
+  }, []);
+  if (!settings) return <Loading />;
+
+  const baseUrl = getInitialBackendBaseURL(settings.backendBaseURL);
   return (
-    <TranslationProvider source={{}}>
-      <ExchangeApiProvider baseUrl={baseUrl} frameOnError={ExchangeAmlFrame}>
-        <HashPathProvider>
+    <SettingsProvider value={settings}>
+      <TranslationProvider
+        source={strings}
+        completeness={{
+          es: strings["es"].completeness,
+          de: strings["de"].completeness,
+        }}
+      >
+        <ExchangeApiProvider
+          baseUrl={new URL("/", baseUrl)}
+          frameOnError={ExchangeAmlFrame}
+        >
           <SWRConfig
             value={{
               provider: WITH_LOCAL_STORAGE_CACHE
@@ -60,19 +80,13 @@ export function App(): VNode {
               keepPreviousData: true,
             }}
           >
-            <ExchangeAmlFrame>
-              <Router
-                pageList={pageList}
-                onNotFound={() => {
-                  window.location.href = Pages.cases.url;
-                  return <div>not found</div>;
-                }}
-              />
-            </ExchangeAmlFrame>
+            <BrowserHashNavigationProvider>
+              <Routing />
+            </BrowserHashNavigationProvider>
           </SWRConfig>
-        </HashPathProvider>
-      </ExchangeApiProvider>
-    </TranslationProvider>
+        </ExchangeApiProvider>
+      </TranslationProvider>
+    </SettingsProvider>
   );
 }
 
@@ -85,3 +99,34 @@ function localStorageProvider(): Map<unknown, unknown> {
   });
   return map;
 }
+
+function getInitialBackendBaseURL(
+  backendFromSettings: string | undefined,
+): string {
+  const overrideUrl =
+    typeof localStorage !== "undefined"
+      ? localStorage.getItem("exchange-base-url")
+      : undefined;
+  let result: string;
+
+  if (!overrideUrl) {
+    // normal path
+    if (!backendFromSettings) {
+      console.error(
+        "ERROR: backendBaseURL was overridden by a setting file and missing. Setting value to 'window.origin'",
+      );
+      result = window.origin;
+    } else {
+      result = backendFromSettings;
+    }
+  } else {
+    // testing/development path
+    result = overrideUrl;
+  }
+  try {
+    return canonicalizeBaseUrl(result);
+  } catch (e) {
+    // fall back
+    return canonicalizeBaseUrl(window.origin);
+  }
+}
-- 
cgit v1.2.3

