1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-25 15:27:40 +01:00
smov/src/components/player/hooks/usePlayer.ts
2023-10-18 16:08:33 +02:00

55 lines
1.7 KiB
TypeScript

import { useInitializePlayer } from "@/components/player/hooks/useInitializePlayer";
import { PlayerMeta, playerStatus } from "@/stores/player/slices/source";
import { usePlayerStore } from "@/stores/player/store";
import { SourceSliceSource } from "@/stores/player/utils/qualities";
import { ProgressMediaItem, useProgressStore } from "@/stores/progress";
export interface Source {
url: string;
type: "hls" | "mp4";
}
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;
}
export function usePlayer() {
const setStatus = usePlayerStore((s) => s.setStatus);
const setMeta = usePlayerStore((s) => s.setMeta);
const setSource = usePlayerStore((s) => s.setSource);
const setSourceId = usePlayerStore((s) => s.setSourceId);
const status = usePlayerStore((s) => s.status);
const reset = usePlayerStore((s) => s.reset);
const meta = usePlayerStore((s) => s.meta);
const { init } = useInitializePlayer();
const progressStore = useProgressStore();
return {
reset,
status,
setMeta(m: PlayerMeta) {
setMeta(m);
},
playMedia(source: SourceSliceSource, sourceId: string | null) {
setSource(source, getProgress(progressStore.items, meta));
setSourceId(sourceId);
setStatus(playerStatus.PLAYING);
init();
},
setScrapeStatus() {
setStatus(playerStatus.SCRAPING);
},
};
}