File size: 2,371 Bytes
6b3405c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Route, Switch } from "wouter";
import { MantineProvider } from "@mantine/core";
import "@mantine/core/styles.css";
import { lazy, useEffect, useState } from "react";
import { usePubSub } from "create-pubsub/react";
import { settingsPubSub } from "../../modules/pubSub";
import { defaultSettings } from "../../modules/settings";
import { addLogEntry } from "../../modules/logEntries";
import { Notifications } from "@mantine/notifications";
import "@mantine/notifications/styles.css";
import { match } from "ts-pattern";
import { verifyStoredAccessKey } from "../../modules/accessKey";

const MainPage = lazy(() => import("../Pages/Main/MainPage"));
const AccessPage = lazy(() => import("../Pages/AccessPage"));

export function App() {
  useInitializeSettings();

  const [hasValidatedAccessKey, setValidatedAccessKey] = useState(false);
  const [isCheckingStoredKey, setCheckingStoredKey] = useState(true);

  useEffect(() => {
    async function checkStoredAccessKey() {
      if (VITE_ACCESS_KEYS_ENABLED) {
        const isValid = await verifyStoredAccessKey();
        if (isValid) setValidatedAccessKey(true);
      }
      setCheckingStoredKey(false);
    }

    checkStoredAccessKey();
  }, []);

  if (isCheckingStoredKey) return null;

  return (
    <MantineProvider defaultColorScheme="dark">
      <Notifications />
      <Switch>
        <Route path="/">
          {match([VITE_ACCESS_KEYS_ENABLED, hasValidatedAccessKey])
            .with([true, false], () => (
              <AccessPage
                onAccessKeyValid={() => setValidatedAccessKey(true)}
              />
            ))
            .otherwise(() => (
              <MainPage />
            ))}
        </Route>
      </Switch>
    </MantineProvider>
  );
}

/**
 * A custom React hook that initializes the application settings.
 *
 * @returns The initialized settings object.
 *
 * @remarks
 * This hook uses the `usePubSub` hook to access and update the settings state.
 * It initializes the settings by merging the default settings with any existing settings.
 * The initialization is performed once when the component mounts.
 */
function useInitializeSettings() {
  const [settings, setSettings] = usePubSub(settingsPubSub);

  useEffect(() => {
    setSettings({ ...defaultSettings, ...settings });
    addLogEntry("Settings initialized");
  }, []);

  return settings;
}