refactor url prefix

This commit is contained in:
castdrian 2023-06-13 11:01:07 +02:00 committed by Adrian Castro
parent 3af98373fb
commit 70f8355386
4 changed files with 37 additions and 9 deletions

View File

@ -2,7 +2,12 @@ import { FetchError } from "ofetch";
import { formatJWMeta, mediaTypeToJW } from "./justwatch"; import { formatJWMeta, mediaTypeToJW } from "./justwatch";
import { Tmdb } from "./tmdb"; import { Tmdb } from "./tmdb";
import { Trakt, formatTTVMeta } from "./trakttv"; import {
TTVMediaToMediaType,
Trakt,
formatTTVMeta,
mediaTypeToTTV,
} from "./trakttv";
import { import {
JWMediaResult, JWMediaResult,
JWSeasonMetaResult, JWSeasonMetaResult,
@ -137,3 +142,24 @@ export async function getLegacyMetaFromId(
tmdbId, tmdbId,
}; };
} }
export function MWMediaToId(media: MWMediaMeta): string {
return ["MW", mediaTypeToTTV(media.type), media.id].join("-");
}
export function decodeMWId(
paramId: string
): { id: string; type: MWMediaType } | null {
const [prefix, type, id] = paramId.split("-", 3);
if (prefix !== "MW") return null;
let mediaType;
try {
mediaType = TTVMediaToMediaType(type);
} catch {
return null;
}
return {
type: mediaType,
id,
};
}

View File

@ -1,7 +1,7 @@
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { JWMediaToId } from "@/backend/metadata/justwatch"; import { MWMediaToId } from "@/backend/metadata/getmeta";
import { MWMediaMeta } from "@/backend/metadata/types"; import { MWMediaMeta } from "@/backend/metadata/types";
import { DotList } from "@/components/text/DotList"; import { DotList } from "@/components/text/DotList";
@ -132,7 +132,7 @@ export function MediaCard(props: MediaCardProps) {
const canLink = props.linkable && !props.closable; const canLink = props.linkable && !props.closable;
let link = canLink let link = canLink
? `/media/${encodeURIComponent(JWMediaToId(props.media))}` ? `/media/${encodeURIComponent(MWMediaToId(props.media))}`
: "#"; : "#";
if (canLink && props.series) if (canLink && props.series)
link += `/${encodeURIComponent(props.series.seasonId)}/${encodeURIComponent( link += `/${encodeURIComponent(props.series.seasonId)}/${encodeURIComponent(

View File

@ -2,8 +2,7 @@ import { useCallback, useMemo, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { getMetaFromId } from "@/backend/metadata/getmeta"; import { decodeMWId, getMetaFromId } from "@/backend/metadata/getmeta";
import { decodeJWId } from "@/backend/metadata/justwatch";
import { MWMediaType, MWSeasonWithEpisodeMeta } from "@/backend/metadata/types"; import { MWMediaType, MWSeasonWithEpisodeMeta } from "@/backend/metadata/types";
import { IconPatch } from "@/components/buttons/IconPatch"; import { IconPatch } from "@/components/buttons/IconPatch";
import { Icon, Icons } from "@/components/Icon"; import { Icon, Icons } from "@/components/Icon";
@ -45,7 +44,7 @@ export function EpisodeSelectionPopout() {
seasonId: sId, seasonId: sId,
season: undefined, season: undefined,
}); });
reqSeasonMeta(decodeJWId(params.media)?.id as string, sId).then((v) => { reqSeasonMeta(decodeMWId(params.media)?.id as string, sId).then((v) => {
if (v?.meta.type !== MWMediaType.SERIES) return; if (v?.meta.type !== MWMediaType.SERIES) return;
setCurrentVisibleSeason({ setCurrentVisibleSeason({
seasonId: sId, seasonId: sId,

View File

@ -4,8 +4,11 @@ import { useTranslation } from "react-i18next";
import { useHistory, useParams } from "react-router-dom"; import { useHistory, useParams } from "react-router-dom";
import { MWStream } from "@/backend/helpers/streams"; import { MWStream } from "@/backend/helpers/streams";
import { DetailedMeta, getMetaFromId } from "@/backend/metadata/getmeta"; import {
import { decodeJWId } from "@/backend/metadata/justwatch"; DetailedMeta,
decodeMWId,
getMetaFromId,
} from "@/backend/metadata/getmeta";
import { MWMediaType, MWSeasonWithEpisodeMeta } from "@/backend/metadata/types"; import { MWMediaType, MWSeasonWithEpisodeMeta } from "@/backend/metadata/types";
import { IconPatch } from "@/components/buttons/IconPatch"; import { IconPatch } from "@/components/buttons/IconPatch";
import { Icons } from "@/components/Icon"; import { Icons } from "@/components/Icon";
@ -181,7 +184,7 @@ export function MediaView() {
const [selected, setSelected] = useState<SelectedMediaData | null>(null); const [selected, setSelected] = useState<SelectedMediaData | null>(null);
const [exec, loading, error] = useLoading( const [exec, loading, error] = useLoading(
async (mediaParams: string, seasonId?: string) => { async (mediaParams: string, seasonId?: string) => {
const data = decodeJWId(mediaParams); const data = decodeMWId(mediaParams);
if (!data) return null; if (!data) return null;
return getMetaFromId(data.type, data.id, seasonId); return getMetaFromId(data.type, data.id, seasonId);
} }