movie-web/src/setup/App.tsx

140 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-06-21 21:35:25 +02:00
import { ReactElement, lazy, useEffect } from "react";
import {
Redirect,
Route,
Switch,
useHistory,
useLocation,
2023-06-23 11:05:01 +02:00
useParams,
2023-06-21 21:35:25 +02:00
} from "react-router-dom";
2023-01-07 21:36:18 +01:00
2023-06-21 21:35:25 +02:00
import { convertLegacyUrl, isLegacyUrl } from "@/backend/metadata/getmeta";
2023-06-23 11:05:01 +02:00
import { generateQuickSearchMediaUrl } from "@/backend/metadata/tmdb";
2023-10-21 21:44:08 +02:00
import { useOnlineListener } from "@/hooks/usePing";
2023-09-06 20:27:17 +02:00
import { AboutPage } from "@/pages/About";
import { AdminPage } from "@/pages/admin/AdminPage";
2023-09-06 20:27:17 +02:00
import { DmcaPage } from "@/pages/Dmca";
2023-08-20 18:46:13 +02:00
import { NotFoundPage } from "@/pages/errors/NotFoundPage";
import { HomePage } from "@/pages/HomePage";
import { PlayerView } from "@/pages/PlayerView";
import { Layout } from "@/setup/Layout";
import { BookmarkContextProvider } from "@/state/bookmark";
import { SettingsProvider } from "@/state/settings";
import { WatchedContextProvider } from "@/state/watched";
import { useHistoryListener } from "@/stores/history";
2023-02-24 19:23:00 +01:00
2023-06-21 21:35:25 +02:00
function LegacyUrlView({ children }: { children: ReactElement }) {
2023-06-13 14:06:37 +02:00
const location = useLocation();
2023-06-21 21:35:25 +02:00
const { replace } = useHistory();
2023-06-21 18:16:41 +02:00
useEffect(() => {
2023-06-21 21:35:25 +02:00
const url = location.pathname;
if (!isLegacyUrl(url)) return;
2023-06-21 18:16:41 +02:00
convertLegacyUrl(location.pathname).then((convertedUrl) => {
2023-06-21 21:35:25 +02:00
replace(convertedUrl ?? "/");
2023-06-21 18:16:41 +02:00
});
2023-06-21 21:35:25 +02:00
}, [location.pathname, replace]);
2023-06-21 18:16:41 +02:00
2023-06-21 21:35:25 +02:00
if (isLegacyUrl(location.pathname)) return null;
return children;
}
2023-06-21 18:16:41 +02:00
2023-06-23 11:05:01 +02:00
function QuickSearch() {
const { query } = useParams<{ query: string }>();
const { replace } = useHistory();
useEffect(() => {
if (query) {
generateQuickSearchMediaUrl(query).then((url) => {
replace(url ?? "/");
});
} else {
replace("/");
}
}, [query, replace]);
return null;
}
2023-06-21 18:16:41 +02:00
function App() {
useHistoryListener();
2023-10-21 21:44:08 +02:00
useOnlineListener();
2022-02-06 20:56:48 +01:00
return (
2023-03-09 20:08:13 +03:00
<SettingsProvider>
<WatchedContextProvider>
<BookmarkContextProvider>
2023-10-21 21:44:08 +02:00
<Layout>
<Switch>
{/* functional routes */}
<Route exact path="/s/:query">
<QuickSearch />
</Route>
<Route exact path="/search/:type">
<Redirect to="/browse" push={false} />
</Route>
<Route exact path="/search/:type/:query?">
{({ match }) => {
if (match?.params.query)
return (
<Redirect
to={`/browse/${match?.params.query}`}
push={false}
/>
);
return <Redirect to="/browse" push={false} />;
}}
</Route>
2023-02-22 19:02:23 +01:00
2023-10-21 21:44:08 +02:00
{/* pages */}
<Route
exact
path={["/media/:media", "/media/:media/:season/:episode"]}
>
<LegacyUrlView>
<PlayerView />
</LegacyUrlView>
</Route>
<Route
exact
path={["/browse/:query?", "/"]}
component={HomePage}
/>
<Route exact path="/faq" component={AboutPage} />
<Route exact path="/dmca" component={DmcaPage} />
2023-07-23 12:34:59 +02:00
{/* admin routes */}
<Route exact path="/admin" component={AdminPage} />
2023-10-21 21:44:08 +02:00
{/* other */}
<Route
exact
path="/dev"
component={lazy(() => import("@/pages/DeveloperPage"))}
/>
<Route
exact
path="/dev/video"
component={lazy(
() => import("@/pages/developer/VideoTesterView")
)}
/>
{/* developer routes that can abuse workers are disabled in production */}
{process.env.NODE_ENV === "development" ? (
2023-06-21 21:35:25 +02:00
<Route
exact
2023-10-21 21:44:08 +02:00
path="/dev/test"
component={lazy(() => import("@/pages/developer/TestView"))}
2023-06-21 21:35:25 +02:00
/>
2023-10-21 21:44:08 +02:00
) : null}
<Route path="*" component={NotFoundPage} />
</Switch>
</Layout>
2023-03-09 20:08:13 +03:00
</BookmarkContextProvider>
</WatchedContextProvider>
</SettingsProvider>
2022-02-06 20:56:48 +01:00
);
}
export default App;