movie-web/src/stores/player/slices/source.ts

93 lines
1.9 KiB
TypeScript
Raw Normal View History

import { ScrapeMedia } from "@movie-web/providers";
2023-07-23 16:30:22 +02:00
import { MWStreamType } from "@/backend/helpers/streams";
import { MakeSlice } from "@/stores/player/slices/types";
import { ValuesOf } from "@/utils/typeguard";
export const playerStatus = {
IDLE: "idle",
SCRAPING: "scraping",
PLAYING: "playing",
} as const;
export type PlayerStatus = ValuesOf<typeof playerStatus>;
2023-08-15 19:55:48 +02:00
export interface SourceSliceSource {
url: string;
type: MWStreamType;
}
export interface PlayerMeta {
type: "movie" | "show";
title: string;
tmdbId: string;
imdbId?: string;
releaseYear: number;
episode?: {
number: number;
tmdbId: string;
title: string;
};
season?: {
number: number;
tmdbId: string;
title: string;
};
}
2023-07-23 16:30:22 +02:00
export interface SourceSlice {
status: PlayerStatus;
2023-08-15 19:55:48 +02:00
source: SourceSliceSource | null;
meta: PlayerMeta | null;
2023-07-23 16:30:22 +02:00
setStatus(status: PlayerStatus): void;
setSource(url: string, type: MWStreamType): void;
setMeta(meta: PlayerMeta): void;
}
export function metaToScrapeMedia(meta: PlayerMeta): ScrapeMedia {
if (meta.type === "show") {
if (!meta.episode || !meta.season) throw new Error("missing show data");
return {
title: meta.title,
releaseYear: meta.releaseYear,
tmdbId: meta.tmdbId,
type: "show",
imdbId: meta.imdbId,
episode: meta.episode,
season: meta.season,
};
}
return {
title: meta.title,
releaseYear: meta.releaseYear,
tmdbId: meta.tmdbId,
type: "movie",
imdbId: meta.imdbId,
};
2023-07-23 16:30:22 +02:00
}
export const createSourceSlice: MakeSlice<SourceSlice> = (set) => ({
2023-07-23 16:30:22 +02:00
source: null,
status: playerStatus.IDLE,
meta: null,
2023-07-23 16:30:22 +02:00
setStatus(status: PlayerStatus) {
set((s) => {
s.status = status;
});
},
setMeta(meta) {
set((s) => {
s.meta = meta;
});
},
2023-07-23 16:30:22 +02:00
setSource(url: string, type: MWStreamType) {
set((s) => {
s.source = {
type,
url,
};
});
},
});