2023-10-12 20:31:40 +02:00
|
|
|
import { useCallback, useMemo, useState } from "react";
|
|
|
|
|
|
|
|
import { DetailedMeta } from "@/backend/metadata/getmeta";
|
|
|
|
import { MWMediaType } from "@/backend/metadata/types/mw";
|
|
|
|
import { usePlayer } from "@/components/player/hooks/usePlayer";
|
|
|
|
import { PlayerMeta, metaToScrapeMedia } from "@/stores/player/slices/source";
|
|
|
|
|
|
|
|
export function usePlayerMeta() {
|
|
|
|
const { setMeta, setScrapeStatus } = usePlayer();
|
|
|
|
const [meta, _setPlayerMeta] = useState<PlayerMeta | null>(null);
|
|
|
|
const scrapeMedia = useMemo(
|
|
|
|
() => (meta ? metaToScrapeMedia(meta) : null),
|
|
|
|
[meta]
|
|
|
|
);
|
|
|
|
|
2023-10-20 17:29:10 +02:00
|
|
|
const setDirectMeta = useCallback(
|
|
|
|
(m: PlayerMeta) => {
|
|
|
|
_setPlayerMeta(m);
|
|
|
|
setScrapeStatus();
|
2023-10-21 16:13:16 +02:00
|
|
|
setMeta(m);
|
2023-10-20 17:29:10 +02:00
|
|
|
},
|
|
|
|
[_setPlayerMeta, setMeta, setScrapeStatus]
|
|
|
|
);
|
|
|
|
|
2023-10-12 20:31:40 +02:00
|
|
|
const setPlayerMeta = useCallback(
|
|
|
|
(m: DetailedMeta, episodeId?: string) => {
|
|
|
|
let playerMeta: PlayerMeta;
|
|
|
|
if (m.meta.type === MWMediaType.SERIES) {
|
|
|
|
const ep = m.meta.seasonData.episodes.find((v) => v.id === episodeId);
|
2023-10-14 22:03:13 +02:00
|
|
|
if (!ep) return null;
|
2023-10-12 20:31:40 +02:00
|
|
|
playerMeta = {
|
|
|
|
type: "show",
|
|
|
|
releaseYear: +(m.meta.year ?? 0),
|
|
|
|
title: m.meta.title,
|
2023-10-17 16:01:26 +02:00
|
|
|
poster: m.meta.poster,
|
2023-10-12 20:31:40 +02:00
|
|
|
tmdbId: m.tmdbId ?? "",
|
|
|
|
imdbId: m.imdbId,
|
2023-10-20 17:29:10 +02:00
|
|
|
episodes: m.meta.seasonData.episodes.map((v) => ({
|
|
|
|
number: v.number,
|
|
|
|
title: v.title,
|
|
|
|
tmdbId: v.id,
|
|
|
|
})),
|
2023-10-12 20:31:40 +02:00
|
|
|
episode: {
|
|
|
|
number: ep.number,
|
|
|
|
title: ep.title,
|
|
|
|
tmdbId: ep.id,
|
|
|
|
},
|
|
|
|
season: {
|
|
|
|
number: m.meta.seasonData.number,
|
|
|
|
title: m.meta.seasonData.title,
|
|
|
|
tmdbId: m.meta.seasonData.id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
playerMeta = {
|
|
|
|
type: "movie",
|
|
|
|
releaseYear: +(m.meta.year ?? 0),
|
|
|
|
title: m.meta.title,
|
2023-10-17 16:01:26 +02:00
|
|
|
poster: m.meta.poster,
|
2023-10-12 20:31:40 +02:00
|
|
|
tmdbId: m.tmdbId ?? "",
|
|
|
|
imdbId: m.imdbId,
|
|
|
|
};
|
|
|
|
}
|
2023-10-20 17:29:10 +02:00
|
|
|
setDirectMeta(playerMeta);
|
2023-10-14 22:03:13 +02:00
|
|
|
return playerMeta;
|
2023-10-12 20:31:40 +02:00
|
|
|
},
|
2023-10-20 17:29:10 +02:00
|
|
|
[setDirectMeta]
|
2023-10-12 20:31:40 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
playerMeta: meta,
|
|
|
|
setPlayerMeta,
|
2023-10-20 17:29:10 +02:00
|
|
|
setDirectMeta,
|
2023-10-12 20:31:40 +02:00
|
|
|
scrapeMedia,
|
|
|
|
};
|
|
|
|
}
|