Fix routing

This commit is contained in:
mrjvs 2023-07-23 12:34:59 +02:00
parent 7d6d41fb48
commit 8b84ff114d
6 changed files with 34 additions and 33 deletions

View File

@ -5,7 +5,7 @@ import { MWMediaMeta, MWQuery } from "./types/mw";
const cache = new SimpleCache<MWQuery, MWMediaMeta[]>(); const cache = new SimpleCache<MWQuery, MWMediaMeta[]>();
cache.setCompare((a, b) => { cache.setCompare((a, b) => {
return a.type === b.type && a.searchQuery.trim() === b.searchQuery.trim(); return a.searchQuery.trim() === b.searchQuery.trim();
}); });
cache.initialize(); cache.initialize();

View File

@ -43,7 +43,6 @@ export type MWMediaMeta = MWMediaMetaBase & MWMediaMetaSpecific;
export interface MWQuery { export interface MWQuery {
searchQuery: string; searchQuery: string;
type: MWMediaType;
} }
export interface DetailedMeta { export interface DetailedMeta {

View File

@ -1,14 +1,16 @@
import { useState } from "react"; import { useState } from "react";
import { generatePath, useHistory, useRouteMatch } from "react-router-dom"; import { generatePath, useHistory, useParams } from "react-router-dom";
import { MWMediaType, MWQuery } from "@/backend/metadata/types/mw"; import { MWQuery } from "@/backend/metadata/types/mw";
import { useQueryParams } from "@/hooks/useQueryParams";
function getInitialValue(params: { type: string; query: string }) { function getInitialValue(
const type = query: Record<string, string>,
Object.values(MWMediaType).find((v) => params.type === v) || params: Record<string, string>
MWMediaType.MOVIE; ) {
const searchQuery = decodeURIComponent(params.query || ""); let searchQuery = decodeURIComponent(params.query || "");
return { type, searchQuery }; if (query.q) searchQuery = query.q;
return { searchQuery };
} }
export function useSearchQuery(): [ export function useSearchQuery(): [
@ -17,28 +19,34 @@ export function useSearchQuery(): [
() => void () => void
] { ] {
const history = useHistory(); const history = useHistory();
const { path, params } = useRouteMatch<{ type: string; query: string }>(); const query = useQueryParams();
const [search, setSearch] = useState<MWQuery>(getInitialValue(params)); const params = useParams<{ query: string }>();
const [search, setSearch] = useState<MWQuery>(getInitialValue(query, params));
const updateParams = (inp: Partial<MWQuery>, force: boolean) => { const updateParams = (inp: Partial<MWQuery>, force: boolean) => {
const copySearch: MWQuery = { ...search }; const copySearch = { ...search };
Object.assign(copySearch, inp); Object.assign(copySearch, inp);
setSearch(copySearch); setSearch(copySearch);
if (!force) return; if (!force) return;
if (copySearch.searchQuery.length === 0) {
history.replace("/");
return;
}
history.replace( history.replace(
generatePath(path, { generatePath("/browse/:query", {
query: query: copySearch.searchQuery,
copySearch.searchQuery.length === 0 ? undefined : inp.searchQuery,
type: copySearch.type,
}) })
); );
}; };
const onUnFocus = () => { const onUnFocus = () => {
if (search.searchQuery.length === 0) {
history.replace("/");
return;
}
history.replace( history.replace(
generatePath(path, { generatePath("/browse/:query", {
query: search.searchQuery.length === 0 ? undefined : search.searchQuery, query: search.searchQuery,
type: search.type,
}) })
); );
}; };

View File

@ -84,8 +84,12 @@ function App() {
<Route exact path="/search/:type"> <Route exact path="/search/:type">
<Redirect to="/browse" /> <Redirect to="/browse" />
</Route> </Route>
<Route exact path="/browse/:query" component={SearchView} /> <Route
<Route exact path="/" component={SearchView} /> exact
path={["/browse/:query?", "/"]}
component={SearchView}
/>
{/* other */} {/* other */}
<Route <Route
exact exact

View File

@ -48,7 +48,6 @@ async function getMetas(
const year = Number(item.year.toString().split("-")[0]); const year = Number(item.year.toString().split("-")[0]);
const data = await searchForMedia({ const data = await searchForMedia({
searchQuery: `${item.title} ${year}`, searchQuery: `${item.title} ${year}`,
type: item.mediaType,
}); });
const relevantItem = data.find( const relevantItem = data.find(
(res) => (res) =>

View File

@ -1,19 +1,10 @@
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Loading } from "@/components/layout/Loading"; import { Loading } from "@/components/layout/Loading";
import { useSearchQuery } from "@/hooks/useSearchQuery";
export function SearchLoadingView() { export function SearchLoadingView() {
const { t } = useTranslation(); const { t } = useTranslation();
const [query] = useSearchQuery();
return ( return (
<Loading <Loading className="mb-24 mt-40 " text={t("search.loading") || "..."} />
className="mb-24 mt-40 "
text={
t(`search.loading_${query.type}`) ||
t("search.loading") ||
"Fetching your favourite shows..."
}
/>
); );
} }