From f2f242a684e922b4fbc2a0ee83e418aff7d10b96 Mon Sep 17 00:00:00 2001 From: ner Date: Sun, 20 Oct 2024 21:00:01 +0300 Subject: [PATCH] feat: autostart next video --- .../Torrents/components/Player/Player.tsx | 38 +++++++-- .../Player/components/PlayerMenu/index.tsx | 79 +++++++++++++------ .../Player/components/VideosMenu/index.tsx | 1 + .../Torrents/components/Player/useSource.ts | 4 +- 4 files changed, 89 insertions(+), 33 deletions(-) diff --git a/components/Preview/components/Torrents/components/Player/Player.tsx b/components/Preview/components/Torrents/components/Player/Player.tsx index fd362c6..fc87cd7 100644 --- a/components/Preview/components/Torrents/components/Player/Player.tsx +++ b/components/Preview/components/Torrents/components/Player/Player.tsx @@ -1,9 +1,20 @@ 'use client'; -import { FC, useRef } from 'react'; -import { MediaPlayer, MediaPlayerInstance, MediaProvider, Track, VideoSrc } from '@vidstack/react'; +import { FC, useEffect, useRef } from 'react'; +import { + MediaPlayer, + MediaPlayerInstance, + MediaProvider, + PlayButton, + Track, + useMediaState, + VideoSrc +} from '@vidstack/react'; +import { NextIcon } from '@vidstack/react/icons'; import { PlyrLayout, plyrLayoutIcons } from '@vidstack/react/player/layouts/plyr'; +import { cn } from '@/lib/utils'; + import CaptionsMenu from './components/CaptionsMenu'; import VideosMenu from './components/VideosMenu'; import useSource from './useSource'; @@ -17,8 +28,19 @@ interface Props { const Player: FC = ({ videos, subtitles, magnet, onReady }) => { const { index, setIndex } = useSource({ magnet }); + const player = useRef(null); + const isEnded = useMediaState('ended', player); + + const hasNext = index + 1 < videos?.length; + + useEffect(() => { + if (!isEnded || !hasNext) return; + + setIndex((index) => index + 1); + }, [isEnded, hasNext, setIndex]); + const source = videos[index]; return ( @@ -40,13 +62,19 @@ const Player: FC = ({ videos, subtitles, magnet, onReady }) => { icons={plyrLayoutIcons} slots={{ playLargeButton: null, + settingsMenu: null, + captionsButton: null, + afterPlayButton: hasNext && ( + setIndex(index + 1)} className={cn('plyr__controls__item plyr__control')}> + + Next + + ), afterVolumeSlider:
    , beforeSettings: Boolean(subtitles.length) && , settings: videos.length > 1 && ( - ), - settingsMenu: null, - captionsButton: null + ) }} /> diff --git a/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx b/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx index 7642fd9..fd0bde9 100644 --- a/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx @@ -1,4 +1,5 @@ -import { FC, ReactNode, useRef } from 'react'; +import { FC, forwardRef, ReactNode, useRef } from 'react'; +import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso'; import { Menu } from '@vidstack/react'; import { cn } from '@/lib/utils'; @@ -9,42 +10,68 @@ interface Props { children: ReactNode; options: Option[]; value: string; + className?: string; } -const PlayerMenu: FC = ({ children, options, value }) => { - const scrollRef = useRef(null); - const refs = useRef>({}); +const PlayerMenu: FC = ({ children, options, value, className }) => { + const virtuosoRef = useRef(null); return ( scrollRef.current?.scrollTo({ top: refs.current[value].offsetTop - 8 })} + onOpen={() => + setTimeout(() => { + virtuosoRef.current?.scrollToIndex({ behavior: 'smooth', index: +value }); + }, 100) + } > {children} - - - {options.map((option) => ( - element && (refs.current[option.value] = element)} - key={option.value} - title={option.label} - onClick={() => option.onSelect(option.value)} - className={cn( - '!m-0 cursor-pointer whitespace-normal break-words rounded-sm bg-secondary !px-2 !py-1 text-foreground', - { 'bg-secondary-foreground text-secondary': value === option.value } - )} - > - {option.label} - - ))} - + + ); }; +const components: VirtuosoProps['components'] = { + Item: ({ item: option, children, context, ...props }) => { + if (!option) return null; + + return ( + +
    option.onSelect(option.value)} + className={cn( + '!m-0 cursor-pointer whitespace-normal break-all rounded-sm bg-secondary text-left !px-2 !py-1 text-foreground', + { + 'bg-secondary-foreground text-secondary': context === option.value + } + )} + > + {option.label} +
    +
    + ); + }, + List: forwardRef(function List({ children, ...props }, ref) { + return ( + +
    + {children} +
    +
    + ); + }) +}; + export default PlayerMenu; diff --git a/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx b/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx index 922c9bc..4104982 100644 --- a/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx @@ -22,6 +22,7 @@ const VideosMenu: FC = ({ sources, source, onChange }) => { value: index.toString(), onSelect: () => onChange(index) }))} + className='max-h-80' > Playlist diff --git a/components/Preview/components/Torrents/components/Player/useSource.ts b/components/Preview/components/Torrents/components/Player/useSource.ts index 0eaed3d..630f90c 100644 --- a/components/Preview/components/Torrents/components/Player/useSource.ts +++ b/components/Preview/components/Torrents/components/Player/useSource.ts @@ -17,12 +17,12 @@ const useSource = ({ magnet }: { magnet: string }) => { const user = account?.username ?? 'local'; const setIndex = useCallback( - (index: number) => { + (update: number | ((index: number) => number)) => { setSource((state) => ({ ...state, [user]: { ...(state[user] || {}), - [magnet]: index + [magnet]: typeof update === 'function' ? update(state[user]?.[magnet] ?? 0) : update } })); },