feat: current episode by magnet is saved into localstorage
This commit is contained in:
1 parent
29b22f88ae
commit
988085ac8b
9 files changed
+122
-51
No files matched your search
@@ -1,21 +1,22 @@
|
||||
'use client';
|
||||
|
||||
import { FC, useRef, useState } from 'react';
|
||||
import { FC, useRef } from 'react';
|
||||
import { MediaPlayer, MediaPlayerInstance, MediaProvider, Track, VideoSrc } from '@vidstack/react';
|
||||
import { PlyrLayout, plyrLayoutIcons } from '@vidstack/react/player/layouts/plyr';
|
||||
|
||||
import CaptionsMenu from './components/CaptionsMenu';
|
||||
import VideosMenu from './components/VideosMenu';
|
||||
import useSource from './useSource';
|
||||
|
||||
interface Props {
|
||||
magnet: string;
|
||||
subtitles: Source[];
|
||||
videos: Source[];
|
||||
onReady: () => void;
|
||||
}
|
||||
|
||||
const Player: FC<Props> = ({ videos, subtitles, onReady }) => {
|
||||
const [index, setIndex] = useState(0);
|
||||
|
||||
const Player: FC<Props> = ({ videos, subtitles, magnet, onReady }) => {
|
||||
const { index, setIndex } = useSource({ magnet });
|
||||
const player = useRef<MediaPlayerInstance>(null);
|
||||
|
||||
const source = videos[index];
|
||||
@@ -41,7 +42,9 @@ const Player: FC<Props> = ({ videos, subtitles, onReady }) => {
|
||||
playLargeButton: null,
|
||||
afterVolumeSlider: <ul className='w-2' />,
|
||||
beforeSettings: Boolean(subtitles.length) && <CaptionsMenu />,
|
||||
settings: videos.length > 1 && <VideosMenu source={source} sources={videos} onChange={setIndex} />,
|
||||
settings: videos.length > 1 && (
|
||||
<VideosMenu key={source?.src} source={source} sources={videos} onChange={setIndex} />
|
||||
),
|
||||
settingsMenu: null,
|
||||
captionsButton: null
|
||||
}}
|
||||
|
||||
+4
-5
@@ -1,4 +1,4 @@
|
||||
import { useActiveTextTrack, useCaptionOptions } from '@vidstack/react';
|
||||
import { useCaptionOptions } from '@vidstack/react';
|
||||
import { ClosedCaptionsIcon, ClosedCaptionsOnIcon } from '@vidstack/react/icons';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
@@ -6,15 +6,14 @@ import { cn } from '@/lib/utils';
|
||||
import PlayerMenu from '../PlayerMenu';
|
||||
|
||||
const CaptionsMenu = () => {
|
||||
const track = useActiveTextTrack(['subtitles']);
|
||||
const tracks = useCaptionOptions();
|
||||
|
||||
return (
|
||||
<PlayerMenu
|
||||
options={tracks.map((track) => ({ label: track.label, onClick: () => track.select() }))}
|
||||
isChecked={({ label }) => (label === 'Off' ? !track : label === track?.label)}
|
||||
value={tracks.selectedValue}
|
||||
options={tracks.map((track) => ({ label: track.label, value: track.value, onSelect: () => track.select() }))}
|
||||
>
|
||||
{track ? (
|
||||
{tracks.selectedTrack ? (
|
||||
<ClosedCaptionsOnIcon className={cn('vds-icon !transform-none !size-5')} />
|
||||
) : (
|
||||
<ClosedCaptionsIcon className={cn('vds-icon !transform-none !size-5')} />
|
||||
|
||||
+37
-29
@@ -1,42 +1,50 @@
|
||||
import { FC, ReactNode } from 'react';
|
||||
import { FC, ReactNode, useRef } from 'react';
|
||||
import { Menu } from '@vidstack/react';
|
||||
import { CheckIcon } from '@vidstack/react/icons';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
type Option = { onClick: () => void; label: string };
|
||||
type Option = { onSelect: (value: string) => void; value: string; label: string };
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
options: Option[];
|
||||
isChecked: (option: Option) => boolean;
|
||||
value: string;
|
||||
}
|
||||
|
||||
const PlayerMenu: FC<Props> = ({ children, options, isChecked }) => (
|
||||
<Menu.Root className={cn('plyr__menu')}>
|
||||
<Menu.Button className={cn('plyr__controls__item plyr__control')} aria-expanded={false}>
|
||||
{children}
|
||||
</Menu.Button>
|
||||
<Menu.Items
|
||||
className={cn('plyr__menu__container !p-0 max-h-52 overflow-auto no-scrollbar')}
|
||||
placement='top end'
|
||||
offset={0}
|
||||
const PlayerMenu: FC<Props> = ({ children, options, value }) => {
|
||||
const scrollRef = useRef<HTMLElement>(null);
|
||||
const refs = useRef<Record<string, HTMLElement>>({});
|
||||
|
||||
return (
|
||||
<Menu.Root
|
||||
className={cn('plyr__menu')}
|
||||
onOpen={() => scrollRef.current?.scrollTo({ top: refs.current[value].offsetTop - 8 })}
|
||||
>
|
||||
<Menu.RadioGroup>
|
||||
{options.map((option) => (
|
||||
<Menu.Radio
|
||||
key={option.label}
|
||||
title={option.label}
|
||||
className='!m-0 flex w-64 cursor-pointer items-center gap-2 truncate p-2'
|
||||
onClick={option.onClick}
|
||||
>
|
||||
<CheckIcon className={cn('vds-icon shrink-0', { invisible: !isChecked(option) })} size={16} />
|
||||
<span className='truncate'>{option.label}</span>
|
||||
</Menu.Radio>
|
||||
))}
|
||||
</Menu.RadioGroup>
|
||||
</Menu.Items>
|
||||
</Menu.Root>
|
||||
);
|
||||
<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>
|
||||
</Menu.Root>
|
||||
);
|
||||
};
|
||||
|
||||
export default PlayerMenu;
|
||||
+17
-9
@@ -11,14 +11,22 @@ interface Props {
|
||||
onChange: (index: number) => void;
|
||||
}
|
||||
|
||||
const VideosMenu: FC<Props> = ({ sources, source, onChange }) => (
|
||||
<PlayerMenu
|
||||
options={sources.map(({ name }, index) => ({ label: name, onClick: () => onChange(index) }))}
|
||||
isChecked={({ label }) => source.name === label}
|
||||
>
|
||||
<PlaylistIcon className={cn('vds-icon !transform-none')} />
|
||||
<span className={cn('plyr__tooltip')}>Playlist</span>
|
||||
</PlayerMenu>
|
||||
);
|
||||
const VideosMenu: FC<Props> = ({ sources, source, onChange }) => {
|
||||
const value = sources.findIndex(({ name }) => source.name === name);
|
||||
|
||||
return (
|
||||
<PlayerMenu
|
||||
value={value.toString()}
|
||||
options={sources.map(({ name }, index) => ({
|
||||
label: name,
|
||||
value: index.toString(),
|
||||
onSelect: () => onChange(index)
|
||||
}))}
|
||||
>
|
||||
<PlaylistIcon className={cn('vds-icon !transform-none')} />
|
||||
<span className={cn('plyr__tooltip')}>Playlist</span>
|
||||
</PlayerMenu>
|
||||
);
|
||||
};
|
||||
|
||||
export default VideosMenu;
|
||||
@@ -42,7 +42,7 @@ const showPlayer = create(({ onResolve, magnet, backdrop, title }: Props) => {
|
||||
<Modal className='overflow-hidden border-none bg-black p-0' onClose={onResolve}>
|
||||
<div className='relative w-full overflow-hidden pt-[56.25%]'>
|
||||
<div className='absolute left-0 top-0 size-full'>
|
||||
<Player subtitles={subtitles} videos={videos} onReady={() => setIsReady(true)} />
|
||||
<Player magnet={magnet} subtitles={subtitles} videos={videos} onReady={() => setIsReady(true)} />
|
||||
</div>
|
||||
{(!isReady || isLoading) && (
|
||||
<div className='absolute left-0 top-0 size-full'>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
|
||||
import useAccount from '@/hooks/useAccount';
|
||||
|
||||
type SourceState = Record<string, Record<string, number>>;
|
||||
|
||||
const sourceStateAtom = atomWithStorage<SourceState>('current-video-source', {}, undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
|
||||
const useSource = ({ magnet }: { magnet: string }) => {
|
||||
const [source, setSource] = useAtom(sourceStateAtom);
|
||||
const account = useAccount();
|
||||
|
||||
const user = account?.username ?? 'local';
|
||||
|
||||
const setIndex = useCallback(
|
||||
(index: number) => {
|
||||
setSource((state) => ({
|
||||
...state,
|
||||
[user]: {
|
||||
...(state[user] || {}),
|
||||
[magnet]: index
|
||||
}
|
||||
}));
|
||||
},
|
||||
[setSource, user, magnet]
|
||||
);
|
||||
|
||||
const index = source[user]?.[magnet] || 0;
|
||||
|
||||
return { index, setIndex };
|
||||
};
|
||||
|
||||
export default useSource;
|
||||
Reference in new issue
Block a user