From 988085ac8bfeb8f19cefac44792e3395a1ea5096 Mon Sep 17 00:00:00 2001 From: ner Date: Sun, 20 Oct 2024 17:41:57 +0300 Subject: [PATCH] feat: current episode by magnet is saved into localstorage --- components/Header/components/User/index.tsx | 3 +- .../Torrents/components/Player/Player.tsx | 13 ++-- .../Player/components/CaptionsMenu/index.tsx | 9 ++- .../Player/components/PlayerMenu/index.tsx | 66 +++++++++++-------- .../Player/components/VideosMenu/index.tsx | 26 +++++--- .../Torrents/components/Player/index.tsx | 2 +- .../Torrents/components/Player/useSource.ts | 37 +++++++++++ .../components/User => hooks}/useAccount.ts | 0 styles/index.css | 17 ++++- 9 files changed, 122 insertions(+), 51 deletions(-) create mode 100644 components/Preview/components/Torrents/components/Player/useSource.ts rename {components/Header/components/User => hooks}/useAccount.ts (100%) diff --git a/components/Header/components/User/index.tsx b/components/Header/components/User/index.tsx index 593f9d6..3ebce31 100644 --- a/components/Header/components/User/index.tsx +++ b/components/Header/components/User/index.tsx @@ -12,7 +12,8 @@ import { DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; -import useAccount from './useAccount'; +import useAccount from '../../../../hooks/useAccount'; + import useAuth from './useAuth'; import useLogOut from './useLogOut'; diff --git a/components/Preview/components/Torrents/components/Player/Player.tsx b/components/Preview/components/Torrents/components/Player/Player.tsx index bcbe960..fd362c6 100644 --- a/components/Preview/components/Torrents/components/Player/Player.tsx +++ b/components/Preview/components/Torrents/components/Player/Player.tsx @@ -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 = ({ videos, subtitles, onReady }) => { - const [index, setIndex] = useState(0); - +const Player: FC = ({ videos, subtitles, magnet, onReady }) => { + const { index, setIndex } = useSource({ magnet }); const player = useRef(null); const source = videos[index]; @@ -41,7 +42,9 @@ const Player: FC = ({ videos, subtitles, onReady }) => { playLargeButton: null, afterVolumeSlider:
    , beforeSettings: Boolean(subtitles.length) && , - settings: videos.length > 1 && , + settings: videos.length > 1 && ( + + ), settingsMenu: null, captionsButton: null }} diff --git a/components/Preview/components/Torrents/components/Player/components/CaptionsMenu/index.tsx b/components/Preview/components/Torrents/components/Player/components/CaptionsMenu/index.tsx index 1583a43..f276bc1 100644 --- a/components/Preview/components/Torrents/components/Player/components/CaptionsMenu/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/CaptionsMenu/index.tsx @@ -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 ( ({ 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 ? ( ) : ( 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 4847f75..7642fd9 100644 --- a/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx @@ -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 = ({ children, options, isChecked }) => ( - - - {children} - - = ({ children, options, value }) => { + const scrollRef = useRef(null); + const refs = useRef>({}); + + return ( + scrollRef.current?.scrollTo({ top: refs.current[value].offsetTop - 8 })} > - - {options.map((option) => ( - - - {option.label} - - ))} - - - -); + {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} + + ))} + + + + ); +}; 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 5ff4614..922c9bc 100644 --- a/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx @@ -11,14 +11,22 @@ interface Props { onChange: (index: number) => void; } -const VideosMenu: FC = ({ sources, source, onChange }) => ( - ({ label: name, onClick: () => onChange(index) }))} - isChecked={({ label }) => source.name === label} - > - - Playlist - -); +const VideosMenu: FC = ({ sources, source, onChange }) => { + const value = sources.findIndex(({ name }) => source.name === name); + + return ( + ({ + label: name, + value: index.toString(), + onSelect: () => onChange(index) + }))} + > + + Playlist + + ); +}; export default VideosMenu; diff --git a/components/Preview/components/Torrents/components/Player/index.tsx b/components/Preview/components/Torrents/components/Player/index.tsx index 69fef97..a7fd28f 100644 --- a/components/Preview/components/Torrents/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/Player/index.tsx @@ -42,7 +42,7 @@ const showPlayer = create(({ onResolve, magnet, backdrop, title }: Props) => {
    - setIsReady(true)} /> + setIsReady(true)} />
    {(!isReady || isLoading) && (
    diff --git a/components/Preview/components/Torrents/components/Player/useSource.ts b/components/Preview/components/Torrents/components/Player/useSource.ts new file mode 100644 index 0000000..0eaed3d --- /dev/null +++ b/components/Preview/components/Torrents/components/Player/useSource.ts @@ -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>; + +const sourceStateAtom = atomWithStorage('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; diff --git a/components/Header/components/User/useAccount.ts b/hooks/useAccount.ts similarity index 100% rename from components/Header/components/User/useAccount.ts rename to hooks/useAccount.ts diff --git a/styles/index.css b/styles/index.css index ad9991e..82c9db2 100644 --- a/styles/index.css +++ b/styles/index.css @@ -86,14 +86,30 @@ color: hsl(var(--secondary)); background: hsl(var(--secondary-foreground)); } + ::selection { color: hsl(var(--secondary)); background: hsl(var(--secondary-foreground)); } + ::-webkit-scrollbar { + width: 6px; + background-color: transparent; + } + + ::-webkit-scrollbar-thumb { + border-radius: 6px; + background-color: hsl(var(--foreground)); + } + + ::-webkit-scrollbar-thumb:hover { + background-color: hsl(var(--muted-foreground)); + } + body { @apply bg-background text-foreground; } + body [data-overlay]:not(:last-of-type) { background: none; } @@ -139,7 +155,6 @@ } .plyr { - --plyr-menu-background: hsl(var(--card-foreground)); --plyr-menu-color: hsl(var(--secondary)); --plyr-video-control-background-hover: hsl(var(--secondary-foreground)); --plyr-video-control-color-hover: hsl(var(--secondary));