From d99f332523c1e70df8ed1f0d024cc6dd77607433 Mon Sep 17 00:00:00 2001
From: AAGaming <aa@mail.catvibers.me>
Date: Fri, 12 May 2023 02:02:04 +0000
Subject: Initial implementation of global DFL instance (#451)

---
 frontend/src/start.tsx | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 frontend/src/start.tsx

(limited to 'frontend/src/start.tsx')

diff --git a/frontend/src/start.tsx b/frontend/src/start.tsx
new file mode 100644
index 00000000..94b22ffe
--- /dev/null
+++ b/frontend/src/start.tsx
@@ -0,0 +1,77 @@
+import i18n from 'i18next';
+import Backend from 'i18next-http-backend';
+import { initReactI18next } from 'react-i18next';
+
+import PluginLoader from './plugin-loader';
+import { DeckyUpdater } from './updater';
+
+declare global {
+  interface Window {
+    DeckyPluginLoader: PluginLoader;
+    DeckyUpdater?: DeckyUpdater;
+    importDeckyPlugin: Function;
+    syncDeckyPlugins: Function;
+    deckyHasLoaded: boolean;
+    deckyHasConnectedRDT?: boolean;
+    deckyAuthToken: string;
+    DFL?: any;
+  }
+}
+
+(async () => {
+  window.deckyAuthToken = await fetch('http://127.0.0.1:1337/auth/token').then((r) => r.text());
+
+  i18n
+    .use(Backend)
+    .use(initReactI18next)
+    .init({
+      load: 'currentOnly',
+      detection: {
+        order: ['querystring', 'navigator'],
+        lookupQuerystring: 'lng',
+      },
+      //debug: true,
+      lng: navigator.language,
+      fallbackLng: 'en-US',
+      interpolation: {
+        escapeValue: true,
+      },
+      returnEmptyString: false,
+      backend: {
+        loadPath: 'http://127.0.0.1:1337/locales/{{lng}}.json',
+        customHeaders: {
+          Authentication: window.deckyAuthToken,
+        },
+        requestOptions: {
+          credentials: 'include',
+        },
+      },
+    });
+
+  window.DeckyPluginLoader?.dismountAll();
+  window.DeckyPluginLoader?.deinit();
+  window.DeckyPluginLoader = new PluginLoader();
+  window.DeckyPluginLoader.init();
+  window.importDeckyPlugin = function (name: string, version: string) {
+    window.DeckyPluginLoader?.importPlugin(name, version);
+  };
+
+  window.syncDeckyPlugins = async function () {
+    const plugins = await (
+      await fetch('http://127.0.0.1:1337/plugins', {
+        credentials: 'include',
+        headers: { Authentication: window.deckyAuthToken },
+      })
+    ).json();
+    for (const plugin of plugins) {
+      if (!window.DeckyPluginLoader.hasPlugin(plugin.name))
+        window.DeckyPluginLoader?.importPlugin(plugin.name, plugin.version);
+    }
+
+    window.DeckyPluginLoader.checkPluginUpdates();
+  };
+
+  setTimeout(() => window.syncDeckyPlugins(), 5000);
+})();
+
+export default i18n;
-- 
cgit v1.2.3

