mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-11 02:25:08 +01:00
Fix routing
This commit is contained in:
parent
7d6d41fb48
commit
8b84ff114d
@ -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();
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
|
@ -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,
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -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
|
||||||
|
@ -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) =>
|
||||||
|
@ -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..."
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user