-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add theater mode and video player components
- Loading branch information
Showing
4 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { usePlayerState } from '@/store/use-player' | ||
import { VideoPlayerPortalContainer } from '../video-player' | ||
import { Lyrics } from '../player' | ||
|
||
export const TheaterMode = () => { | ||
const currentSong = usePlayerState((state) => state.currentSong) | ||
|
||
if (!currentSong) { | ||
return <p className='my-auto text-center'>No song playing</p> | ||
} | ||
|
||
return ( | ||
<div className='grid grid-cols-1 lg:grow lg:grid-cols-3'> | ||
<VideoPlayerPortalContainer | ||
className='aspect-video max-w-full lg:col-span-2 lg:h-full' | ||
position='theater-mode' | ||
/> | ||
<div className='lg:col-span-1'> | ||
<Lyrics | ||
artist={currentSong?.artist} | ||
song={currentSong?.title} | ||
className='h-[calc(100svh/1.95)] lg:h-[calc(100svh-11rem)]' | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { isEmpty, sample } from 'lodash' | ||
import dynamic from 'next/dynamic' | ||
import { memo, useCallback, useEffect, useMemo, useState } from 'react' | ||
import type ReactPlayer from 'react-player' | ||
|
||
import { invidiousUrls } from '@/server/modules/invidious' | ||
import { useLayoutState, type VideoPosition } from '@/store/use-layout-state' | ||
import { | ||
usePlayerInstance, | ||
usePlayerProgressState, | ||
usePlayerState, | ||
} from '@/store/use-player' | ||
import { ytGetId } from '@/utils/get-yt-url-id' | ||
|
||
interface VideoPlayerPortalContainerProps { | ||
position: VideoPosition | ||
className?: string | ||
} | ||
|
||
export const VideoPlayerPortalContainer = ( | ||
props: VideoPlayerPortalContainerProps | ||
) => { | ||
const { position, className } = props | ||
|
||
const setVideoPosition = useLayoutState((state) => state.setVideoPosition) | ||
const theaterMode = useLayoutState((state) => state.theaterMode) | ||
|
||
const dynamicData = useMemo(() => { | ||
return { [`data-${theaterMode ? 'theater-mode' : position}`]: '' } | ||
}, [position, theaterMode]) | ||
|
||
useEffect(() => { | ||
setVideoPosition(theaterMode ? 'theater-mode' : position) | ||
}, [setVideoPosition, theaterMode, position]) | ||
|
||
return <div className={className} {...dynamicData} /> | ||
} | ||
|
||
const DynamicReactPlayer = dynamic(() => import('react-player/lazy'), { | ||
ssr: false, | ||
}) | ||
|
||
const VideoPlayer = memo(() => { | ||
const { isPlaying, setIsPlaying, currentSong, setDuration, playNext } = | ||
usePlayerState() | ||
|
||
const { instance } = usePlayerInstance() | ||
|
||
const playedProgress = usePlayerProgressState( | ||
(state) => state.progress.played | ||
) | ||
|
||
const setPlayerProgress = usePlayerProgressState((state) => state.setProgress) | ||
|
||
const videoPosition = useLayoutState((state) => state.videoPosition) | ||
|
||
const onPlayerPlay = useCallback(() => { | ||
if (!currentSong) { | ||
return | ||
} | ||
|
||
setIsPlaying(true) | ||
}, [currentSong, setIsPlaying]) | ||
|
||
const onPlayerEnd = useCallback(() => { | ||
playNext({ isUserAction: false }) | ||
}, [playNext]) | ||
|
||
const onPlayerPause = useCallback(() => { | ||
setIsPlaying(false) | ||
}, [setIsPlaying]) | ||
|
||
const onPlayerProgress = useCallback( | ||
(options: { playedSeconds: number; played: number }) => { | ||
setPlayerProgress({ | ||
playedSeconds: options.playedSeconds, | ||
played: options.played, | ||
}) | ||
}, | ||
[setPlayerProgress] | ||
) | ||
|
||
const updatePlayerProgress = useCallback( | ||
(node: Omit<ReactPlayer, 'refs'>) => { | ||
if (playedProgress !== 0) { | ||
node.seekTo(playedProgress, 'fraction') | ||
} | ||
}, | ||
[playedProgress] | ||
) | ||
|
||
const [videoChoice, setVideoChoice] = useState(0) | ||
|
||
const url = useMemo(() => { | ||
if (isEmpty(currentSong?.urls)) return undefined | ||
|
||
const isSoundCloud = | ||
currentSong?.urls?.length === 1 && | ||
currentSong?.urls[0].includes('soundcloud.com') | ||
|
||
if (isSoundCloud) { | ||
return currentSong?.urls![0] | ||
} | ||
|
||
const videoUrl = currentSong?.urls?.[videoChoice] | ||
|
||
const isVideoUrlBlocked = !videoUrl | ||
|
||
if (isVideoUrlBlocked) { | ||
return `${sample(invidiousUrls)}/latest_version?id=${ytGetId(currentSong?.urls?.[0] ?? '')?.id}&itag=18` | ||
} | ||
|
||
return videoUrl | ||
}, [currentSong?.urls, videoChoice]) | ||
|
||
useEffect(() => { | ||
setVideoChoice(0) | ||
}, [currentSong?.title, currentSong?.artist]) | ||
|
||
return ( | ||
<DynamicReactPlayer | ||
width='100%' | ||
height='100%' | ||
style={{ minHeight: 48 }} | ||
playing={isPlaying} | ||
url={url} | ||
controls | ||
onPlay={onPlayerPlay} | ||
onPause={onPlayerPause} | ||
onEnded={onPlayerEnd} | ||
stopOnUnmount={false} | ||
progressInterval={500} | ||
onProgress={onPlayerProgress} | ||
position={videoPosition} | ||
onError={(error) => { | ||
console.log(error) | ||
if (error) { | ||
setVideoChoice((prev) => prev + 1) | ||
} | ||
}} | ||
onDuration={(duration) => { | ||
if (!currentSong) { | ||
return | ||
} | ||
setDuration(duration) | ||
}} | ||
onReady={(node: Omit<ReactPlayer, 'refs'>) => { | ||
// @ts-expect-error - hide from redux devtools for performance | ||
node.toJSON = () => ({ hidden: 'to help redux devtools :)' }) | ||
|
||
const previousPosition = instance.current?.props.position | ||
|
||
instance.current = node | ||
|
||
if (previousPosition !== node.props.position) { | ||
updatePlayerProgress(instance.current) | ||
} | ||
}} | ||
/> | ||
) | ||
}) | ||
|
||
VideoPlayer.displayName = 'VideoPlayer' | ||
|
||
export { VideoPlayer } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters