feat: autostart next video
This commit is contained in:
1 parent
988085ac8b
commit
f2f242a684
4 files changed
+89
-33
No files matched your search
@@ -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<Props> = ({ videos, subtitles, magnet, onReady }) => {
|
||||
const { index, setIndex } = useSource({ magnet });
|
||||
|
||||
const player = useRef<MediaPlayerInstance>(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<Props> = ({ videos, subtitles, magnet, onReady }) => {
|
||||
icons={plyrLayoutIcons}
|
||||
slots={{
|
||||
playLargeButton: null,
|
||||
settingsMenu: null,
|
||||
captionsButton: null,
|
||||
afterPlayButton: hasNext && (
|
||||
<PlayButton onClick={() => setIndex(index + 1)} className={cn('plyr__controls__item plyr__control')}>
|
||||
<NextIcon className={cn('vds-icon')} />
|
||||
<span className={cn('plyr__tooltip')}>Next</span>
|
||||
</PlayButton>
|
||||
),
|
||||
afterVolumeSlider: <ul className='w-2' />,
|
||||
beforeSettings: Boolean(subtitles.length) && <CaptionsMenu />,
|
||||
settings: videos.length > 1 && (
|
||||
<VideosMenu key={source?.src} source={source} sources={videos} onChange={setIndex} />
|
||||
),
|
||||
settingsMenu: null,
|
||||
captionsButton: null
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</MediaPlayer>
|
||||
|
||||
+53
-26
@@ -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<Props> = ({ children, options, value }) => {
|
||||
const scrollRef = useRef<HTMLElement>(null);
|
||||
const refs = useRef<Record<string, HTMLElement>>({});
|
||||
const PlayerMenu: FC<Props> = ({ children, options, value, className }) => {
|
||||
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
|
||||
|
||||
return (
|
||||
<Menu.Root
|
||||
className={cn('plyr__menu')}
|
||||
onOpen={() => scrollRef.current?.scrollTo({ top: refs.current[value].offsetTop - 8 })}
|
||||
onOpen={() =>
|
||||
setTimeout(() => {
|
||||
virtuosoRef.current?.scrollToIndex({ behavior: 'smooth', index: +value });
|
||||
}, 100)
|
||||
}
|
||||
>
|
||||
<Menu.Button className={cn('plyr__controls__item plyr__control')}>{children}</Menu.Button>
|
||||
<Menu.Items
|
||||
ref={scrollRef}
|
||||
className={cn('plyr__menu__container max-h-52 max-w-64 rounded-lg overflow-auto !bg-background after:hidden')}
|
||||
placement='top end'
|
||||
>
|
||||
<Menu.RadioGroup className='flex flex-col gap-2 p-2' value={value}>
|
||||
{options.map((option) => (
|
||||
<Menu.Radio
|
||||
ref={(element) => 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}
|
||||
</Menu.Radio>
|
||||
))}
|
||||
</Menu.RadioGroup>
|
||||
<Menu.Items className={cn('!bg-background rounded-lg h-60 after:hidden p-2', className)} placement='top end'>
|
||||
<Virtuoso
|
||||
ref={virtuosoRef}
|
||||
className='h-full w-72'
|
||||
initialItemCount={10}
|
||||
data={options}
|
||||
context={value}
|
||||
components={components}
|
||||
/>
|
||||
</Menu.Items>
|
||||
</Menu.Root>
|
||||
);
|
||||
};
|
||||
|
||||
const components: VirtuosoProps<Option, string>['components'] = {
|
||||
Item: ({ item: option, children, context, ...props }) => {
|
||||
if (!option) return null;
|
||||
|
||||
return (
|
||||
<Menu.Radio asChild>
|
||||
<div
|
||||
{...props}
|
||||
key={option.value}
|
||||
title={option.label}
|
||||
onClick={() => 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}
|
||||
</div>
|
||||
</Menu.Radio>
|
||||
);
|
||||
},
|
||||
List: forwardRef(function List({ children, ...props }, ref) {
|
||||
return (
|
||||
<Menu.RadioGroup asChild className='flex flex-col gap-2' value={props.context}>
|
||||
<div {...props} ref={ref}>
|
||||
{children}
|
||||
</div>
|
||||
</Menu.RadioGroup>
|
||||
);
|
||||
})
|
||||
};
|
||||
|
||||
export default PlayerMenu;
|
||||
+1
@@ -22,6 +22,7 @@ const VideosMenu: FC<Props> = ({ sources, source, onChange }) => {
|
||||
value: index.toString(),
|
||||
onSelect: () => onChange(index)
|
||||
}))}
|
||||
className='max-h-80'
|
||||
>
|
||||
<PlaylistIcon className={cn('vds-icon !transform-none')} />
|
||||
<span className={cn('plyr__tooltip')}>Playlist</span>
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}));
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user