watch: added support for timestamp parameter
continuous-integration/drone/push Build is passing Details

nextui
Guus van Meerveld 8 months ago
parent 189c651941
commit a2d9a449db

@ -30,11 +30,12 @@ export const Description: Component<{ data: string }> = ({ data }) => {
[data] [data]
); );
const descriptionAlreadyShort = sanitizedDescription.length <= 200; const descriptionAlreadyShort =
sanitizedDescription.length <= shortenedDescriptionLength;
const descriptionCut = useMemo(() => { const descriptionCut = useMemo(() => {
if (descriptionAlreadyShort) return sanitizedDescription; if (descriptionAlreadyShort || expandedDescription)
else if (expandedDescription) return sanitizedDescription; return sanitizedDescription;
else else
return ( return (
sanitizedDescription.substring(0, shortenedDescriptionLength) + "..." sanitizedDescription.substring(0, shortenedDescriptionLength) + "..."

@ -30,7 +30,11 @@ import formatDuration from "@/utils/formatDuration";
import { Component } from "@/typings/component"; import { Component } from "@/typings/component";
export const Player: Component<{ streams: Stream[] }> = ({ streams }) => { export const Player: Component<{
streams: Stream[];
initialDuration: number;
initialTimestamp?: number;
}> = ({ streams, initialTimestamp, initialDuration }) => {
const stream = streams.find((stream) => stream.type === StreamType.Hls); const stream = streams.find((stream) => stream.type === StreamType.Hls);
const playerRef = useRef<ReactPlayer>(null); const playerRef = useRef<ReactPlayer>(null);
@ -61,9 +65,9 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
]; ];
}, []); }, []);
const [duration, setDuration] = useState(initialDuration);
const [progress, setProgress] = useState(0); const [progress, setProgress] = useState(0);
const [loaded, setLoaded] = useState(0); const [loaded, setLoaded] = useState(0);
const [duration, setDuration] = useState(0);
const [maximized, setMaximized] = useState(false); const [maximized, setMaximized] = useState(false);
const [volume, setVolume] = useState(40); const [volume, setVolume] = useState(40);
const [muted, setMuted] = useState(false); const [muted, setMuted] = useState(false);
@ -154,6 +158,11 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
playerRef.current?.seekTo(userSetProgress); playerRef.current?.seekTo(userSetProgress);
}, [userSetProgress]); }, [userSetProgress]);
useEffect(() => {
if (initialTimestamp && initialTimestamp <= duration)
setUserSetProgress(initialTimestamp / duration);
}, [initialTimestamp, duration]);
const updateMaximized = useCallback(() => { const updateMaximized = useCallback(() => {
setMaximized(screenfull.isFullscreen); setMaximized(screenfull.isFullscreen);
}, [setMaximized]); }, [setMaximized]);

@ -34,16 +34,33 @@ export const Watch: Component = () => {
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const videoId = searchParams.get("v") as string; const videoId = useMemo(() => {
const param = searchParams.get("v");
const videoIdIsInvalid = useMemo(() => videoId === null, [videoId]); if (param === null) return;
return param;
}, [searchParams]);
const timestamp = useMemo(() => {
const param = searchParams.get("t");
if (param === null) return;
const time = parseInt(param);
if (isNaN(time)) return;
return time;
}, [searchParams]);
const { data, isLoading, error } = useQuery({ const { data, isLoading, error } = useQuery({
queryKey: ["watch", videoId], queryKey: ["watch", videoId],
queryFn: () => { queryFn: () => {
if (!videoId) return;
return client.getWatchable(videoId); return client.getWatchable(videoId);
}, },
enabled: !videoIdIsInvalid enabled: !!videoId
}); });
const { const {
@ -54,9 +71,10 @@ export const Watch: Component = () => {
} = useQuery({ } = useQuery({
queryKey: ["comments", videoId], queryKey: ["comments", videoId],
queryFn: () => { queryFn: () => {
if (!videoId) return;
return client.getComments(videoId); return client.getComments(videoId);
}, },
enabled: !videoIdIsInvalid enabled: !!videoId
}); });
if (error) console.log(error); if (error) console.log(error);
@ -66,7 +84,11 @@ export const Watch: Component = () => {
{isLoading && <LoadingPage />} {isLoading && <LoadingPage />}
{data && !isLoading && ( {data && !isLoading && (
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
<Player streams={data.streams} /> <Player
initialTimestamp={timestamp}
initialDuration={data.video.duration / 1000}
streams={data.streams}
/>
<div className="flex flex-col xl:flex-row gap-4"> <div className="flex flex-col xl:flex-row gap-4">
<div className="flex flex-1 flex-col gap-4"> <div className="flex flex-1 flex-col gap-4">
<div className="flex flex-col"> <div className="flex flex-col">

Loading…
Cancel
Save