|
@@ -3,7 +3,9 @@ import { FC, useEffect, useState } from 'react'
|
|
import { CSSTransition, SwitchTransition } from 'react-transition-group'
|
|
import { CSSTransition, SwitchTransition } from 'react-transition-group'
|
|
|
|
|
|
import { useBasicVideos } from '@/api/hooks/video'
|
|
import { useBasicVideos } from '@/api/hooks/video'
|
|
|
|
+import { VideoOrderByInput, VideoWhereInput } from '@/api/queries/__generated__/baseTypes.generated'
|
|
import { BasicVideoFieldsFragment } from '@/api/queries/__generated__/fragments.generated'
|
|
import { BasicVideoFieldsFragment } from '@/api/queries/__generated__/fragments.generated'
|
|
|
|
+import { publicVideoFilter } from '@/config/contentFilter'
|
|
import { cVar, transitions } from '@/styles'
|
|
import { cVar, transitions } from '@/styles'
|
|
import { getRandomIntInclusive } from '@/utils/number'
|
|
import { getRandomIntInclusive } from '@/utils/number'
|
|
|
|
|
|
@@ -20,7 +22,9 @@ type VideoOverlayProps = {
|
|
isPlayNextDisabled?: boolean
|
|
isPlayNextDisabled?: boolean
|
|
playRandomVideoOnEnded?: boolean
|
|
playRandomVideoOnEnded?: boolean
|
|
isMinimized?: boolean
|
|
isMinimized?: boolean
|
|
|
|
+ currentVideoCreatedAt?: Date
|
|
}
|
|
}
|
|
|
|
+
|
|
export const VideoOverlay: FC<VideoOverlayProps> = ({
|
|
export const VideoOverlay: FC<VideoOverlayProps> = ({
|
|
playerState,
|
|
playerState,
|
|
onPlayAgain,
|
|
onPlayAgain,
|
|
@@ -31,28 +35,37 @@ export const VideoOverlay: FC<VideoOverlayProps> = ({
|
|
isPlayNextDisabled,
|
|
isPlayNextDisabled,
|
|
isMinimized,
|
|
isMinimized,
|
|
playRandomVideoOnEnded = true,
|
|
playRandomVideoOnEnded = true,
|
|
|
|
+ currentVideoCreatedAt,
|
|
}) => {
|
|
}) => {
|
|
const [randomNextVideo, setRandomNextVideo] = useState<BasicVideoFieldsFragment | null>(null)
|
|
const [randomNextVideo, setRandomNextVideo] = useState<BasicVideoFieldsFragment | null>(null)
|
|
- const { videos } = useBasicVideos(
|
|
|
|
- {
|
|
|
|
- where: {
|
|
|
|
- channel: {
|
|
|
|
- id_eq: channelId,
|
|
|
|
- },
|
|
|
|
|
|
+ const commonFiltersFactory = (where?: VideoWhereInput) => ({
|
|
|
|
+ limit: 1,
|
|
|
|
+ orderBy: VideoOrderByInput.ChannelCreatedAtAsc,
|
|
|
|
+ where: {
|
|
|
|
+ ...publicVideoFilter,
|
|
|
|
+ channel: {
|
|
|
|
+ id_eq: channelId,
|
|
},
|
|
},
|
|
|
|
+ ...where,
|
|
},
|
|
},
|
|
- { context: { delay: 2000 } }
|
|
|
|
|
|
+ })
|
|
|
|
+ const { videos: newerVideos, loading: loadingNewestVideos } = useBasicVideos(
|
|
|
|
+ commonFiltersFactory({ createdAt_gt: currentVideoCreatedAt })
|
|
)
|
|
)
|
|
|
|
+ const { videos: olderVideos } = useBasicVideos(commonFiltersFactory({ createdAt_lt: currentVideoCreatedAt }), {
|
|
|
|
+ skip: loadingNewestVideos || !!newerVideos?.length,
|
|
|
|
+ })
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- if (!videos?.length || videos.length <= 1) {
|
|
|
|
|
|
+ const videos = newerVideos?.length ? newerVideos : olderVideos
|
|
|
|
+ if (!videos?.length || videos.length === 0) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
const filteredVideos = videos.filter((video) => video.id !== videoId)
|
|
const filteredVideos = videos.filter((video) => video.id !== videoId)
|
|
const randomNumber = getRandomIntInclusive(0, filteredVideos.length - 1)
|
|
const randomNumber = getRandomIntInclusive(0, filteredVideos.length - 1)
|
|
|
|
|
|
setRandomNextVideo(filteredVideos[randomNumber])
|
|
setRandomNextVideo(filteredVideos[randomNumber])
|
|
- }, [videoId, videos])
|
|
|
|
|
|
+ }, [channelId, currentVideoCreatedAt, newerVideos, olderVideos, videoId])
|
|
|
|
|
|
return (
|
|
return (
|
|
<SwitchTransition>
|
|
<SwitchTransition>
|