From f78af76da53e56f4f347bcf89a6f14940754ce62 Mon Sep 17 00:00:00 2001 From: ner Date: Sun, 17 Aug 2025 20:40:34 +0300 Subject: [PATCH] feat: safari autoplay --- .../Player/components/Captions/index.tsx | 18 ++- .../Player/components/Menu/index.tsx | 47 +++---- .../Torrents/components/Player/index.tsx | 127 +++++++++--------- .../Actions/components/Player/index.tsx | 24 +++- .../Actions/components/Player/useStats.ts | 5 +- .../components/Actions/useFiles.ts | 2 +- .../Preview/components/Torrents/index.tsx | 1 - hooks/useCanPlay.ts | 7 + 8 files changed, 127 insertions(+), 104 deletions(-) create mode 100644 hooks/useCanPlay.ts diff --git a/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx b/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx index 22f1282..d76913b 100644 --- a/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx @@ -1,4 +1,5 @@ -import { useCaptionOptions } from '@vidstack/react'; +import { useMemo } from 'react'; +import { CaptionOption, useCaptionOptions } from '@vidstack/react'; import { ClosedCaptionsIcon, ClosedCaptionsOnIcon } from '@vidstack/react/icons'; import { cn } from '@/lib/utils'; @@ -8,11 +9,24 @@ import PlayerMenu from '../Menu'; const CaptionsMenu = () => { const tracks = useCaptionOptions(); + const captions = useMemo(() => { + const record = tracks.reduce>((record, track) => { + record[track.value] = track; + return record; + }, {}); + + return Object.values(record); + }, [tracks]); + return ( ({ label: track.label, value: track.value, onSelect: () => track.select() }))} + options={captions.map((track) => ({ + label: track.label, + value: track.value, + onSelect: () => track.select() + }))} > {tracks.selectedTrack ? ( diff --git a/components/Preview/components/Torrents/components/Player/components/Menu/index.tsx b/components/Preview/components/Torrents/components/Player/components/Menu/index.tsx index 8eee7b3..7f1e953 100644 --- a/components/Preview/components/Torrents/components/Player/components/Menu/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/Menu/index.tsx @@ -1,4 +1,4 @@ -import React, { FC, forwardRef, MutableRefObject, ReactNode, useLayoutEffect, useRef } from 'react'; +import React, { FC, forwardRef, MutableRefObject, ReactNode, useRef } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso'; import { Menu } from '@vidstack/react'; @@ -20,29 +20,20 @@ const showMenu = create( const virtuosoRef = useRef(null); const scrollRef = useRef(null) as MutableRefObject; - useLayoutEffect(() => { - const index = +value || 0; - - if (!index || !virtuosoRef.current) return; - - setTimeout(() => virtuosoRef.current?.scrollToIndex(index), 1000); - }, [value]); - return ( ['components'] = { Item: ({ item: option, children, context, ...props }) => { if (!option) return null; + const index = props['data-index']; + return ( -
option.onSelect(option.value)} - className={cn( - '!m-0 cursor-pointer line-clamp-2 rounded-sm bg-secondary text-left !px-2 !py-1 text-foreground !mb-2', - { - 'bg-secondary-foreground text-secondary': context === option.value - } - )} - > - {option.label} +
option.onSelect(option.value)}> + {!index &&
} +
+ {option.label} +
+
); }, List: forwardRef(function List({ children, ...props }, ref) { return ( -
+
{children}
); diff --git a/components/Preview/components/Torrents/components/Player/index.tsx b/components/Preview/components/Torrents/components/Player/index.tsx index 02275b4..4cbbcac 100644 --- a/components/Preview/components/Torrents/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/Player/index.tsx @@ -12,8 +12,8 @@ import { } from '@vidstack/react'; import { NextIcon } from '@vidstack/react/icons'; import { PlyrLayout, plyrLayoutIcons } from '@vidstack/react/player/layouts/plyr'; -import { Loader } from 'lucide-react'; +import { useCanPlay, useSetCanPlay } from '@/hooks/useCanPlay'; import { cn } from '@/lib/utils'; import Captions from './components/Captions'; @@ -30,16 +30,8 @@ interface Stream { const sample = { type: 'video/mp4', src: '/sample.mp4', name: 'Sample' }; -const initialStream: Stream = { - magnet: '', - subtitles: [], - playlist: [sample] -}; - export type PlayerControlRef = MutableRefObject<{ - sample: () => void; play: (options: { sources: Sources; magnet: string }) => void; - stop: () => void; }>; interface Props { @@ -47,8 +39,13 @@ interface Props { } const Player: FC = ({ controlRef }) => { - const [{ magnet, playlist, subtitles }, setStream] = useState(initialStream); - const [canPlay, setCanPlay] = useState(false); + const [{ magnet, playlist, subtitles }, setStream] = useState({ + magnet: '', + subtitles: [], + playlist: [sample] + }); + const canPlay = useCanPlay(); + const setCanPlay = useSetCanPlay(); const { index, setIndex } = useSource({ magnet }); @@ -72,71 +69,69 @@ const Player: FC = ({ controlRef }) => { player.current .play() .catch(() => {}) - .finally(() => setCanPlay(true)); + .finally(() => { + if (source === sample) return; + + setCanPlay(true); + }); }; useImperativeHandle(controlRef, () => ({ - sample() { - handlePlay(); - setCanPlay(false); - }, play({ magnet, sources }) { setStream({ magnet, ...sources }); - }, - stop() { - setStream(initialStream); + setCanPlay(false); } })); - const pending = !canPlay || source === sample; + const pending = !canPlay; return ( - <> - - - {subtitles.map((track, index) => ( - - ))} - - setIndex(index + 1)} className={cn('plyr__controls__item plyr__control')}> - - Next - - ), - afterVolumeSlider:
    , - beforeSettings: Boolean(subtitles.length) && , - settings: playlist.length > 1 && ( - - ) - }} - /> - - {pending && ( -
    -
    -
    - -
    -
    -
    - )} - + + + {subtitles.map((track, index) => ( + + ))} + + setIndex(index + 1)} className={cn('plyr__controls__item plyr__control')}> + + Next + + ), + afterVolumeSlider:
      , + beforeSettings: Boolean(subtitles.length) && , + settings: playlist.length > 1 && ( + + ) + }} + /> + ); }; diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx index 18a14dc..b3161fa 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx @@ -1,7 +1,9 @@ import { create, InstanceProps } from 'react-modal-promise'; import { DialogContentProps } from '@radix-ui/react-dialog'; +import { Loader } from 'lucide-react'; import { Modal } from '@/components/ui/dialog'; +import { useCanPlay, useSetCanPlay } from '@/hooks/useCanPlay'; import { useStats } from './useStats'; @@ -13,27 +15,40 @@ interface Props extends InstanceProps { } const showPlayerModal = create(({ onResolve, hash, backdrop, title, playerEntryId }: Props) => { - const { isReady, preloadingProgress, downloadSpeed, peers } = useStats({ hash }); + const canPlay = useCanPlay(); + const setCanPlay = useSetCanPlay(); + const { isReady, preloadingProgress, downloadSpeed } = useStats({ hash, canPlay }); const handlePlayerInteraction: DialogContentProps['onInteractOutside'] = (event) => { if (document.getElementById(playerEntryId)?.contains(event.target as Node)) event.preventDefault(); }; + const handleClose = () => { + setCanPlay(false); + onResolve(); + }; + return (
      - {!isReady && ( +
      + {(!isReady || !canPlay) && (
      {title}
      +
      +
      + +
      +

      Buffering: {preloadingProgress.toFixed()}%

      @@ -44,7 +59,6 @@ const showPlayerModal = create(({ onResolve, hash, backdrop, title, playerEntryI
      )} -
      ); diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/useStats.ts b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/useStats.ts index 7b67f10..480ff42 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/useStats.ts +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/useStats.ts @@ -6,6 +6,7 @@ import { formatBytes } from '@/lib/utils'; interface Props { hash?: string; + canPlay: boolean; } export enum StreamStatus { @@ -14,14 +15,14 @@ export enum StreamStatus { ready = 3 } -export const useStats = ({ hash }: Props) => { +export const useStats = ({ hash, canPlay }: Props) => { const streamUrl = useStreamUrl(); const { data } = useQuery({ enabled: Boolean(hash), queryKey: ['stats', hash], refetchInterval: ({ state }) => { - if (state.data?.stat === StreamStatus.ready) return false; + if (state.data?.stat === StreamStatus.ready && canPlay) return false; return 1000; }, diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useFiles.ts b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useFiles.ts index 60d63f4..fda67f6 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useFiles.ts +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/useFiles.ts @@ -11,7 +11,7 @@ const useFiles = ({ magnet }: Props) => { return useMutation({ mutationFn: () => - fetch(`${streamUrl}&stat&preload`) + fetch(`${streamUrl}&stat`) .then((response) => { if (!response.ok) return Promise.reject('Download Failed: unable to retrieve the video'); diff --git a/components/Preview/components/Torrents/index.tsx b/components/Preview/components/Torrents/index.tsx index 189595a..4b0e752 100644 --- a/components/Preview/components/Torrents/index.tsx +++ b/components/Preview/components/Torrents/index.tsx @@ -137,7 +137,6 @@ const showTorrentsModal = create(({ title: initialTitle, year, imdbID, backdrop, onPlay={(options) => { if (!playerControlRef.current) return; - playerControlRef.current.sample(); options.then((options) => playerControlRef.current.play(options)); }} /> diff --git a/hooks/useCanPlay.ts b/hooks/useCanPlay.ts new file mode 100644 index 0000000..276836d --- /dev/null +++ b/hooks/useCanPlay.ts @@ -0,0 +1,7 @@ +import { atom, useAtomValue, useSetAtom } from 'jotai'; + +const canPlayAtom = atom(false as boolean); + +export const useCanPlay = () => useAtomValue(canPlayAtom); + +export const useSetCanPlay = () => useSetAtom(canPlayAtom);