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

153 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-12-06 19:24:43 +01:00
import "@/setup/pwa";
import "core-js/stable";
2023-11-19 20:03:35 +01:00
import "./stores/__old/imports";
import "@/setup/ga";
import "@/assets/css/index.css";
2023-11-19 20:03:35 +01:00
import React, { Suspense, useCallback } 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 { useTranslation } from "react-i18next";
import { BrowserRouter, HashRouter } from "react-router-dom";
import { useAsync } from "react-use";
2023-01-07 21:36:18 +01:00
import { Button } from "@/components/buttons/Button";
import { Icon, Icons } from "@/components/Icon";
import { Loading } from "@/components/layout/Loading";
import { useAuthRestore } from "@/hooks/auth/useAuthRestore";
import { useBackendUrl } from "@/hooks/auth/useBackendUrl";
import { ErrorBoundary } from "@/pages/errors/ErrorBoundary";
import { MigrationPart } from "@/pages/parts/migrations/MigrationPart";
import { LargeTextPart } from "@/pages/parts/util/LargeTextPart";
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";
import { useAuthStore } from "@/stores/auth";
2023-11-19 20:03:35 +01:00
import { BookmarkSyncer } from "@/stores/bookmarks/BookmarkSyncer";
import { useLanguageStore } from "@/stores/language";
import { ProgressSyncer } from "@/stores/progress/ProgressSyncer";
import { SettingsSyncer } from "@/stores/subtitles/SettingsSyncer";
import { useThemeStore } from "@/stores/theme";
2023-01-25 21:44:15 +01:00
import { initializeChromecast } from "./setup/chromecast";
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();
2021-07-14 00:31:37 +02:00
function LoadingScreen(props: { type: "user" | "lazy" }) {
const mapping = {
user: "screens.loadingUser",
lazy: "screens.loadingApp",
};
const { t } = useTranslation();
return (
<LargeTextPart iconSlot={<Loading />}>
{t(mapping[props.type] ?? "unknown.translation")}
</LargeTextPart>
);
}
function ErrorScreen(props: {
children: ReactNode;
showResetButton?: boolean;
}) {
const { t } = useTranslation();
const setBackendUrl = useAuthStore((s) => s.setBackendUrl);
const resetBackend = useCallback(() => {
setBackendUrl(null);
// eslint-disable-next-line no-restricted-globals
location.reload();
}, [setBackendUrl]);
return (
<LargeTextPart
iconSlot={
<Icon className="text-type-danger text-2xl" icon={Icons.WARNING} />
}
>
{props.children}
{props.showResetButton ? (
<div className="mt-6">
<Button theme="secondary" onClick={resetBackend}>
{t("screens.loadingUserError.reset")}
</Button>
</div>
) : null}
</LargeTextPart>
);
}
function AuthWrapper() {
const status = useAuthRestore();
const backendUrl = conf().BACKEND_URL;
const userBackendUrl = useBackendUrl();
const { t } = useTranslation();
if (status.loading) return <LoadingScreen type="user" />;
if (status.error)
return (
<ErrorScreen showResetButton={backendUrl !== userBackendUrl}>
{t(
backendUrl !== userBackendUrl
? "screens.loadingUserError.textWithReset"
: "screens.loadingUserError.text"
)}
</ErrorScreen>
);
return <App />;
}
function MigrationRunner() {
const status = useAsync(async () => {
i18n.changeLanguage(useLanguageStore.getState().language);
await initializeOldStores();
}, []);
const { t } = useTranslation();
if (status.loading) return <MigrationPart />;
if (status.error)
return <ErrorScreen>{t("screens.migration.failed")}</ErrorScreen>;
return <AuthWrapper />;
}
function TheRouter(props: { children: ReactNode }) {
const normalRouter = conf().NORMAL_ROUTER;
if (normalRouter) return <BrowserRouter>{props.children}</BrowserRouter>;
return <HashRouter>{props.children}</HashRouter>;
}
function ThemeProvider(props: { children: ReactNode }) {
const theme = useThemeStore((s) => s.theme);
const themeSelector = theme ? `theme-${theme}` : undefined;
return <div className={themeSelector}>{props.children}</div>;
}
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" />}>
<ThemeProvider>
<ProgressSyncer />
2023-11-19 20:03:35 +01:00
<BookmarkSyncer />
<SettingsSyncer />
<TheRouter>
<MigrationRunner />
</TheRouter>
</ThemeProvider>
</Suspense>
</HelmetProvider>
2022-02-16 21:30:12 +01:00
</ErrorBoundary>
</React.StrictMode>,
document.getElementById("root")
2021-07-14 00:31:37 +02:00
);