1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2025-01-02 16:37:39 +01:00
smov/src/setup/App.tsx

146 lines
4.5 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";
import { BannerContextProvider } from "@/hooks/useBanner";
2023-09-06 20:27:17 +02:00
import { AboutPage } from "@/pages/About";
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 { MediaView } from "@/pages/media/MediaView";
import { Layout } from "@/setup/Layout";
import { BookmarkContextProvider } from "@/state/bookmark";
import { SettingsProvider } from "@/state/settings";
import { WatchedContextProvider } from "@/state/watched";
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() {
2022-02-06 20:56:48 +01:00
return (
2023-03-09 18:08:13 +01:00
<SettingsProvider>
<WatchedContextProvider>
<BookmarkContextProvider>
<BannerContextProvider>
<Layout>
2023-06-21 21:35:25 +02:00
<Switch>
{/* functional routes */}
2023-06-23 11:05:01 +02:00
<Route exact path="/s/:query">
<QuickSearch />
</Route>
2023-02-22 19:02:23 +01:00
2023-06-21 21:35:25 +02:00
{/* pages */}
<Route exact path="/media/:media">
<LegacyUrlView>
<MediaView />
</LegacyUrlView>
</Route>
<Route exact path="/media/:media/:season/:episode">
<LegacyUrlView>
<MediaView />
</LegacyUrlView>
</Route>
2023-06-30 11:43:11 +02:00
<Route exact path="/search/:type/:query?">
<Redirect to="/browse/:query" />
</Route>
<Route exact path="/search/:type">
<Redirect to="/browse" />
</Route>
2023-07-23 12:34:59 +02:00
<Route
exact
path={["/browse/:query?", "/"]}
2023-08-20 18:45:07 +02:00
component={HomePage}
2023-07-23 12:34:59 +02:00
/>
2023-09-06 20:27:17 +02:00
<Route exact path="/faq" component={AboutPage} />
<Route exact path="/dmca" component={DmcaPage} />
2023-07-23 12:34:59 +02:00
2023-06-21 21:35:25 +02:00
{/* other */}
<Route
exact
path="/dev"
2023-08-20 18:46:13 +02:00
component={lazy(() => import("@/pages/DeveloperPage"))}
2023-06-21 21:35:25 +02:00
/>
<Route
exact
path="/dev/video"
component={lazy(
2023-08-20 18:46:13 +02:00
() => import("@/pages/developer/VideoTesterView")
2023-06-21 21:35:25 +02:00
)}
/>
{/* developer routes that can abuse workers are disabled in production */}
{process.env.NODE_ENV === "development" ? (
<>
<Route
exact
path="/dev/test"
component={lazy(
2023-08-20 18:46:13 +02:00
() => import("@/pages/developer/TestView")
2023-06-21 21:35:25 +02:00
)}
/>
2023-04-25 17:35:09 +02:00
2023-06-21 21:35:25 +02:00
<Route
exact
path="/dev/providers"
component={lazy(
2023-08-20 18:46:13 +02:00
() => import("@/pages/developer/ProviderTesterView")
2023-06-21 21:35:25 +02:00
)}
/>
<Route
exact
path="/dev/embeds"
component={lazy(
2023-08-20 18:46:13 +02:00
() => import("@/pages/developer/EmbedTesterView")
2023-06-21 21:35:25 +02:00
)}
/>
</>
) : null}
<Route path="*" component={NotFoundPage} />
</Switch>
2023-03-09 18:08:13 +01:00
</Layout>
</BannerContextProvider>
</BookmarkContextProvider>
</WatchedContextProvider>
</SettingsProvider>
2022-02-06 20:56:48 +01:00
);
}
export default App;