2023-10-08 17:48:48 +02:00
|
|
|
import { useInitializePlayer } from "@/components/player/hooks/useInitializePlayer";
|
2023-10-08 18:16:30 +02:00
|
|
|
import { PlayerMeta, playerStatus } from "@/stores/player/slices/source";
|
2023-07-23 16:30:22 +02:00
|
|
|
import { usePlayerStore } from "@/stores/player/store";
|
2023-10-14 16:06:25 +02:00
|
|
|
import { SourceSliceSource } from "@/stores/player/utils/qualities";
|
2023-10-15 20:25:14 +02:00
|
|
|
import { ProgressMediaItem, useProgressStore } from "@/stores/progress";
|
2023-07-23 16:30:22 +02:00
|
|
|
|
|
|
|
export interface Source {
|
|
|
|
url: string;
|
2023-10-17 19:11:53 +02:00
|
|
|
type: "hls" | "mp4";
|
2023-07-23 16:30:22 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 20:25:14 +02:00
|
|
|
function getProgress(
|
|
|
|
items: Record<string, ProgressMediaItem>,
|
|
|
|
meta: PlayerMeta | null
|
|
|
|
): number {
|
|
|
|
const item = items[meta?.tmdbId ?? ""];
|
|
|
|
if (!item || !meta) return 0;
|
|
|
|
if (meta.type === "movie") {
|
|
|
|
if (!item.progress) return 0;
|
|
|
|
return item.progress.watched;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ep = item.episodes[meta.episode?.tmdbId ?? ""];
|
|
|
|
if (!ep) return 0;
|
|
|
|
return ep.progress.watched;
|
|
|
|
}
|
|
|
|
|
2023-07-23 16:30:22 +02:00
|
|
|
export function usePlayer() {
|
|
|
|
const setStatus = usePlayerStore((s) => s.setStatus);
|
2023-10-08 18:16:30 +02:00
|
|
|
const setMeta = usePlayerStore((s) => s.setMeta);
|
2023-10-14 16:06:25 +02:00
|
|
|
const setSource = usePlayerStore((s) => s.setSource);
|
2023-09-30 20:57:00 +02:00
|
|
|
const status = usePlayerStore((s) => s.status);
|
2023-10-12 23:05:47 +02:00
|
|
|
const reset = usePlayerStore((s) => s.reset);
|
2023-10-15 20:25:14 +02:00
|
|
|
const meta = usePlayerStore((s) => s.meta);
|
2023-10-08 17:48:48 +02:00
|
|
|
const { init } = useInitializePlayer();
|
2023-10-15 20:25:14 +02:00
|
|
|
const progressStore = useProgressStore();
|
2023-07-23 16:30:22 +02:00
|
|
|
|
|
|
|
return {
|
2023-10-12 23:05:47 +02:00
|
|
|
reset,
|
2023-09-30 20:57:00 +02:00
|
|
|
status,
|
2023-10-14 22:03:13 +02:00
|
|
|
setMeta(m: PlayerMeta) {
|
|
|
|
setMeta(m);
|
2023-10-08 18:16:30 +02:00
|
|
|
},
|
2023-10-14 16:06:25 +02:00
|
|
|
playMedia(source: SourceSliceSource) {
|
2023-10-15 20:25:14 +02:00
|
|
|
setSource(source, getProgress(progressStore.items, meta));
|
2023-07-23 16:30:22 +02:00
|
|
|
setStatus(playerStatus.PLAYING);
|
2023-10-08 17:48:48 +02:00
|
|
|
init();
|
2023-07-23 16:30:22 +02:00
|
|
|
},
|
2023-10-01 17:34:37 +02:00
|
|
|
setScrapeStatus() {
|
|
|
|
setStatus(playerStatus.SCRAPING);
|
|
|
|
},
|
2023-07-23 16:30:22 +02:00
|
|
|
};
|
|
|
|
}
|