1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-22 14:57:40 +01:00
smov/src/index.tsx

80 lines
2.3 KiB
TypeScript
Raw Normal View History

import "core-js/stable";
import React, { Suspense } from "react";
2023-09-01 15:37:03 +02:00
import type { ReactNode } from "react";
2022-02-16 21:30:12 +01:00
import ReactDOM from "react-dom";
import { HelmetProvider } from "react-helmet-async";
import { BrowserRouter, HashRouter } from "react-router-dom";
import { useAsync } from "react-use";
2023-02-24 20:12:20 +01:00
import { registerSW } from "virtual:pwa-register";
2023-01-07 21:36:18 +01:00
import { useAuthRestore } from "@/hooks/auth/useAuthRestore";
import { ErrorBoundary } from "@/pages/errors/ErrorBoundary";
import { MigrationPart } from "@/pages/parts/migrations/MigrationPart";
2023-01-07 21:36:18 +01:00
import App from "@/setup/App";
2023-10-21 22:14:54 +02:00
import { conf } from "@/setup/config";
2023-06-18 14:10:26 +02:00
import i18n from "@/setup/i18n";
2023-02-21 21:45:14 +01:00
import "@/setup/ga";
2023-01-07 21:36:18 +01:00
import "@/setup/index.css";
import { useLanguageStore } from "@/stores/language";
2023-01-25 21:44:15 +01:00
import { initializeChromecast } from "./setup/chromecast";
2023-11-01 13:57:48 +01:00
import "./stores/__old/imports";
import { initializeOldStores } from "./stores/__old/migrations";
2022-12-29 18:25:57 +01:00
// initialize
const key =
(window as any)?.__CONFIG__?.VITE_KEY ?? import.meta.env.VITE_KEY ?? null;
if (key) {
2023-02-22 21:41:13 +01:00
(window as any).initMW(conf().PROXY_URLS, key);
2022-12-29 18:25:57 +01:00
}
2023-01-25 21:44:15 +01:00
initializeChromecast();
2023-02-24 20:12:20 +01:00
registerSW({
immediate: true,
2023-02-24 20:12:20 +01:00
});
2021-07-14 00:31:37 +02:00
function LoadingScreen(props: { type: "user" | "lazy" }) {
return <p>Loading: {props.type}</p>;
}
function AuthWrapper() {
const status = useAuthRestore();
// TODO what to do when failing to load user data?
if (status.loading) return <LoadingScreen type="user" />;
if (status.error) return <p>Failed to fetch user data</p>;
return <App />;
}
function MigrationRunner() {
const status = useAsync(async () => {
i18n.changeLanguage(useLanguageStore.getState().language);
await initializeOldStores();
}, []);
if (status.loading) return <MigrationPart />;
if (status.error) return <p>Failed to migrate</p>;
return <AuthWrapper />;
}
function TheRouter(props: { children: ReactNode }) {
const normalRouter = conf().NORMAL_ROUTER;
if (normalRouter) return <BrowserRouter>{props.children}</BrowserRouter>;
return <HashRouter>{props.children}</HashRouter>;
}
2021-07-14 00:31:37 +02:00
ReactDOM.render(
2022-02-16 21:30:12 +01:00
<React.StrictMode>
<ErrorBoundary>
<HelmetProvider>
<Suspense fallback={<LoadingScreen type="lazy" />}>
<TheRouter>
<MigrationRunner />
</TheRouter>
</Suspense>
</HelmetProvider>
2022-02-16 21:30:12 +01:00
</ErrorBoundary>
</React.StrictMode>,
document.getElementById("root")
2021-07-14 00:31:37 +02:00
);