2022-02-28 00:08:20 +01:00
|
|
|
import { IconPatch } from "components/buttons/IconPatch";
|
|
|
|
import { Icons } from "components/Icon";
|
|
|
|
import { Navigation } from "components/layout/Navigation";
|
|
|
|
import { Paper } from "components/layout/Paper";
|
2022-03-13 17:26:46 +01:00
|
|
|
import { LoadingSeasons, Seasons } from "components/layout/Seasons";
|
2022-02-28 00:08:20 +01:00
|
|
|
import { SkeletonVideoPlayer, VideoPlayer } from "components/media/VideoPlayer";
|
|
|
|
import { ArrowLink } from "components/text/ArrowLink";
|
|
|
|
import { DotList } from "components/text/DotList";
|
|
|
|
import { Title } from "components/text/Title";
|
|
|
|
import { useLoading } from "hooks/useLoading";
|
|
|
|
import { usePortableMedia } from "hooks/usePortableMedia";
|
|
|
|
import {
|
|
|
|
MWPortableMedia,
|
|
|
|
getStream,
|
|
|
|
MWMediaStream,
|
|
|
|
MWMedia,
|
|
|
|
convertPortableToMedia,
|
|
|
|
getProviderFromId,
|
|
|
|
MWMediaProvider,
|
2022-03-06 18:45:34 +01:00
|
|
|
MWMediaType,
|
2022-02-28 00:08:20 +01:00
|
|
|
} from "providers";
|
2022-03-06 14:41:51 +01:00
|
|
|
import { ReactElement, useEffect, useState } from "react";
|
2022-03-06 12:56:22 +01:00
|
|
|
import { useHistory } from "react-router-dom";
|
2022-02-28 00:08:20 +01:00
|
|
|
import {
|
|
|
|
getIfBookmarkedFromPortable,
|
|
|
|
useBookmarkContext,
|
|
|
|
} from "state/bookmark";
|
|
|
|
import { getWatchedFromPortable, useWatchedContext } from "state/watched";
|
2022-03-06 12:56:22 +01:00
|
|
|
import { NotFoundChecks } from "./notfound/NotFoundChecks";
|
2022-02-28 00:08:20 +01:00
|
|
|
|
|
|
|
interface StyledMediaViewProps {
|
|
|
|
media: MWMedia;
|
|
|
|
stream: MWMediaStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
function StyledMediaView(props: StyledMediaViewProps) {
|
2022-03-06 12:56:22 +01:00
|
|
|
const watchedStore = useWatchedContext();
|
2022-02-28 00:08:20 +01:00
|
|
|
const startAtTime: number | undefined = getWatchedFromPortable(
|
2022-03-06 12:56:22 +01:00
|
|
|
watchedStore.watched.items,
|
2022-02-28 00:08:20 +01:00
|
|
|
props.media
|
|
|
|
)?.progress;
|
|
|
|
|
|
|
|
function updateProgress(e: Event) {
|
|
|
|
if (!props.media) return;
|
|
|
|
const el: HTMLVideoElement = e.currentTarget as HTMLVideoElement;
|
|
|
|
if (el.currentTime <= 30) {
|
|
|
|
return; // Don't update stored progress if less than 30s into the video
|
|
|
|
}
|
2022-03-06 12:56:22 +01:00
|
|
|
watchedStore.updateProgress(props.media, el.currentTime, el.duration);
|
2022-02-28 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-06 18:31:22 +01:00
|
|
|
<VideoPlayer
|
|
|
|
source={props.stream}
|
2022-03-13 17:46:56 +01:00
|
|
|
captions={props.stream.captions}
|
2022-03-06 18:31:22 +01:00
|
|
|
onProgress={(e) => updateProgress(e)}
|
|
|
|
startAt={startAtTime}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StyledMediaFooterProps {
|
|
|
|
media: MWMedia;
|
|
|
|
provider: MWMediaProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
function StyledMediaFooter(props: StyledMediaFooterProps) {
|
|
|
|
const { setItemBookmark, getFilteredBookmarks } = useBookmarkContext();
|
|
|
|
const isBookmarked = getIfBookmarkedFromPortable(
|
|
|
|
getFilteredBookmarks(),
|
|
|
|
props.media
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Paper className="mt-5">
|
|
|
|
<div className="flex">
|
|
|
|
<div className="flex-1">
|
|
|
|
<Title>{props.media.title}</Title>
|
|
|
|
<DotList
|
|
|
|
className="mt-3 text-sm"
|
|
|
|
content={[
|
|
|
|
props.provider.displayName,
|
|
|
|
props.media.mediaType,
|
|
|
|
props.media.year,
|
|
|
|
]}
|
|
|
|
/>
|
2022-02-28 00:08:20 +01:00
|
|
|
</div>
|
2022-03-06 18:31:22 +01:00
|
|
|
<div>
|
|
|
|
<IconPatch
|
|
|
|
icon={Icons.BOOKMARK}
|
|
|
|
active={isBookmarked}
|
|
|
|
onClick={() => setItemBookmark(props.media, !isBookmarked)}
|
|
|
|
clickable
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-03-06 18:45:34 +01:00
|
|
|
{props.media.mediaType !== MWMediaType.MOVIE ? (
|
|
|
|
<Seasons media={props.media} />
|
|
|
|
) : null}
|
2022-03-06 18:31:22 +01:00
|
|
|
</Paper>
|
2022-02-28 00:08:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-06 18:31:22 +01:00
|
|
|
function LoadingMediaFooter(props: { error?: boolean }) {
|
2022-02-28 00:08:20 +01:00
|
|
|
return (
|
2022-03-06 18:31:22 +01:00
|
|
|
<Paper className="mt-5">
|
|
|
|
<div className="flex">
|
|
|
|
<div className="flex-1">
|
|
|
|
<div className="bg-denim-500 mb-2 h-4 w-48 rounded-full" />
|
|
|
|
<div>
|
|
|
|
<span className="bg-denim-400 mr-4 inline-block h-2 w-12 rounded-full" />
|
|
|
|
<span className="bg-denim-400 mr-4 inline-block h-2 w-12 rounded-full" />
|
2022-02-28 00:08:20 +01:00
|
|
|
</div>
|
2022-03-13 17:26:46 +01:00
|
|
|
{props.error ? (
|
|
|
|
<div className="flex items-center space-x-3">
|
|
|
|
<IconPatch icon={Icons.WARNING} className="text-red-400" />
|
|
|
|
<p>Your url may be invalid</p>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<LoadingSeasons />
|
|
|
|
)}
|
2022-02-28 00:08:20 +01:00
|
|
|
</div>
|
2022-03-06 18:31:22 +01:00
|
|
|
</div>
|
|
|
|
</Paper>
|
2022-02-28 00:08:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-06 12:56:22 +01:00
|
|
|
function MediaViewContent(props: { portable: MWPortableMedia }) {
|
|
|
|
const mediaPortable = props.portable;
|
2022-02-28 00:08:20 +01:00
|
|
|
const [streamUrl, setStreamUrl] = useState<MWMediaStream | undefined>();
|
|
|
|
const [media, setMedia] = useState<MWMedia | undefined>();
|
2022-03-06 18:31:22 +01:00
|
|
|
const [fetchMedia, loadingPortable, errorPortable] = useLoading(
|
|
|
|
(portable: MWPortableMedia) => convertPortableToMedia(portable)
|
|
|
|
);
|
|
|
|
const [fetchStream, loadingStream, errorStream] = useLoading(
|
|
|
|
(portable: MWPortableMedia) => getStream(portable)
|
2022-03-06 14:41:51 +01:00
|
|
|
);
|
2022-02-28 00:08:20 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
if (mediaPortable) {
|
2022-03-06 18:31:22 +01:00
|
|
|
setMedia(await fetchMedia(mediaPortable));
|
2022-02-28 00:08:20 +01:00
|
|
|
}
|
|
|
|
})();
|
2022-03-06 18:31:22 +01:00
|
|
|
}, [mediaPortable, setMedia, fetchMedia]);
|
2022-02-28 00:08:20 +01:00
|
|
|
|
2022-03-06 18:31:22 +01:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
if (mediaPortable) {
|
|
|
|
setStreamUrl(await fetchStream(mediaPortable));
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, [mediaPortable, setStreamUrl, fetchStream]);
|
|
|
|
|
|
|
|
let playerContent: ReactElement | null = null;
|
|
|
|
if (loadingStream) playerContent = <SkeletonVideoPlayer />;
|
|
|
|
else if (errorStream) playerContent = <SkeletonVideoPlayer error />;
|
|
|
|
else if (media && streamUrl)
|
|
|
|
playerContent = <StyledMediaView media={media} stream={streamUrl} />;
|
|
|
|
|
|
|
|
let footerContent: ReactElement | null = null;
|
|
|
|
if (loadingPortable) footerContent = <LoadingMediaFooter />;
|
|
|
|
else if (errorPortable) footerContent = <LoadingMediaFooter error />;
|
2022-03-06 18:45:34 +01:00
|
|
|
else if (mediaPortable && media)
|
2022-03-06 18:31:22 +01:00
|
|
|
footerContent = (
|
|
|
|
<StyledMediaFooter
|
2022-02-28 00:08:20 +01:00
|
|
|
provider={
|
|
|
|
getProviderFromId(mediaPortable.providerId) as MWMediaProvider
|
|
|
|
}
|
|
|
|
media={media}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-03-06 18:31:22 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{playerContent}
|
|
|
|
{footerContent}
|
|
|
|
</>
|
|
|
|
);
|
2022-03-06 12:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function MediaView() {
|
|
|
|
const mediaPortable: MWPortableMedia | undefined = usePortableMedia();
|
|
|
|
const reactHistory = useHistory();
|
|
|
|
|
2022-02-28 00:08:20 +01:00
|
|
|
return (
|
2022-03-06 12:56:22 +01:00
|
|
|
<div className="flex min-h-screen w-full">
|
2022-02-28 00:08:20 +01:00
|
|
|
<Navigation>
|
|
|
|
<ArrowLink
|
2022-03-06 14:41:51 +01:00
|
|
|
onClick={() =>
|
2022-03-06 12:56:22 +01:00
|
|
|
reactHistory.action !== "POP"
|
|
|
|
? reactHistory.goBack()
|
2022-03-06 14:41:51 +01:00
|
|
|
: reactHistory.push("/")
|
|
|
|
}
|
2022-02-28 00:08:20 +01:00
|
|
|
direction="left"
|
|
|
|
linkText="Go back"
|
|
|
|
/>
|
|
|
|
</Navigation>
|
2022-03-06 12:56:22 +01:00
|
|
|
<NotFoundChecks portable={mediaPortable}>
|
|
|
|
<div className="container mx-auto mt-40 mb-16 max-w-[1100px]">
|
|
|
|
<MediaViewContent portable={mediaPortable as MWPortableMedia} />
|
|
|
|
</div>
|
|
|
|
</NotFoundChecks>
|
2022-02-28 00:08:20 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|