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';
|
'use client';
|
||||||
|
|
||||||
import { FC, useRef } from 'react';
|
import { FC, useEffect, useRef } from 'react';
|
||||||
import { MediaPlayer, MediaPlayerInstance, MediaProvider, Track, VideoSrc } from '@vidstack/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 { PlyrLayout, plyrLayoutIcons } from '@vidstack/react/player/layouts/plyr';
|
||||||
|
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
import CaptionsMenu from './components/CaptionsMenu';
|
import CaptionsMenu from './components/CaptionsMenu';
|
||||||
import VideosMenu from './components/VideosMenu';
|
import VideosMenu from './components/VideosMenu';
|
||||||
import useSource from './useSource';
|
import useSource from './useSource';
|
||||||
@@ -17,8 +28,19 @@ interface Props {
|
|||||||
|
|
||||||
const Player: FC<Props> = ({ videos, subtitles, magnet, onReady }) => {
|
const Player: FC<Props> = ({ videos, subtitles, magnet, onReady }) => {
|
||||||
const { index, setIndex } = useSource({ magnet });
|
const { index, setIndex } = useSource({ magnet });
|
||||||
|
|
||||||
const player = useRef<MediaPlayerInstance>(null);
|
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];
|
const source = videos[index];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -40,13 +62,19 @@ const Player: FC<Props> = ({ videos, subtitles, magnet, onReady }) => {
|
|||||||
icons={plyrLayoutIcons}
|
icons={plyrLayoutIcons}
|
||||||
slots={{
|
slots={{
|
||||||
playLargeButton: null,
|
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' />,
|
afterVolumeSlider: <ul className='w-2' />,
|
||||||
beforeSettings: Boolean(subtitles.length) && <CaptionsMenu />,
|
beforeSettings: Boolean(subtitles.length) && <CaptionsMenu />,
|
||||||
settings: videos.length > 1 && (
|
settings: videos.length > 1 && (
|
||||||
<VideosMenu key={source?.src} source={source} sources={videos} onChange={setIndex} />
|
<VideosMenu key={source?.src} source={source} sources={videos} onChange={setIndex} />
|
||||||
),
|
)
|
||||||
settingsMenu: null,
|
|
||||||
captionsButton: null
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</MediaPlayer>
|
</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 { Menu } from '@vidstack/react';
|
||||||
|
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
@@ -9,42 +10,68 @@ interface Props {
|
|||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
options: Option[];
|
options: Option[];
|
||||||
value: string;
|
value: string;
|
||||||
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PlayerMenu: FC<Props> = ({ children, options, value }) => {
|
const PlayerMenu: FC<Props> = ({ children, options, value, className }) => {
|
||||||
const scrollRef = useRef<HTMLElement>(null);
|
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
|
||||||
const refs = useRef<Record<string, HTMLElement>>({});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Menu.Root
|
<Menu.Root
|
||||||
className={cn('plyr__menu')}
|
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.Button className={cn('plyr__controls__item plyr__control')}>{children}</Menu.Button>
|
||||||
<Menu.Items
|
<Menu.Items className={cn('!bg-background rounded-lg h-60 after:hidden p-2', className)} placement='top end'>
|
||||||
ref={scrollRef}
|
<Virtuoso
|
||||||
className={cn('plyr__menu__container max-h-52 max-w-64 rounded-lg overflow-auto !bg-background after:hidden')}
|
ref={virtuosoRef}
|
||||||
placement='top end'
|
className='h-full w-72'
|
||||||
>
|
initialItemCount={10}
|
||||||
<Menu.RadioGroup className='flex flex-col gap-2 p-2' value={value}>
|
data={options}
|
||||||
{options.map((option) => (
|
context={value}
|
||||||
<Menu.Radio
|
components={components}
|
||||||
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>
|
</Menu.Items>
|
||||||
</Menu.Root>
|
</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;
|
export default PlayerMenu;
|
||||||
+1
@@ -22,6 +22,7 @@ const VideosMenu: FC<Props> = ({ sources, source, onChange }) => {
|
|||||||
value: index.toString(),
|
value: index.toString(),
|
||||||
onSelect: () => onChange(index)
|
onSelect: () => onChange(index)
|
||||||
}))}
|
}))}
|
||||||
|
className='max-h-80'
|
||||||
>
|
>
|
||||||
<PlaylistIcon className={cn('vds-icon !transform-none')} />
|
<PlaylistIcon className={cn('vds-icon !transform-none')} />
|
||||||
<span className={cn('plyr__tooltip')}>Playlist</span>
|
<span className={cn('plyr__tooltip')}>Playlist</span>
|
||||||
|
|||||||
@@ -17,12 +17,12 @@ const useSource = ({ magnet }: { magnet: string }) => {
|
|||||||
const user = account?.username ?? 'local';
|
const user = account?.username ?? 'local';
|
||||||
|
|
||||||
const setIndex = useCallback(
|
const setIndex = useCallback(
|
||||||
(index: number) => {
|
(update: number | ((index: number) => number)) => {
|
||||||
setSource((state) => ({
|
setSource((state) => ({
|
||||||
...state,
|
...state,
|
||||||
[user]: {
|
[user]: {
|
||||||
...(state[user] || {}),
|
...(state[user] || {}),
|
||||||
[magnet]: index
|
[magnet]: typeof update === 'function' ? update(state[user]?.[magnet] ?? 0) : update
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user