Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions components/atom/videoPlayer/src/hooks/useVideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@ import {lazy} from 'react'

import {UNKNOWN} from '../settings/index.js'
import useDetectVideoType from './useDetectVideoType.js'

// Static imports for Vite compatibility (instead of dynamic template strings)
const HLSPlayer = lazy(() => import('../components/HLSPlayer.js'))
const NativePlayer = lazy(() => import('../components/NativePlayer.js'))
const VimeoPlayer = lazy(() => import('../components/VimeoPlayer.js'))
const YouTubePlayer = lazy(() => import('../components/YouTubePlayer.js'))

// Map player component names to actual components
const PLAYER_COMPONENTS = {
HLSPlayer,
NativePlayer,
VimeoPlayer,
YouTubePlayer
}

const useVideoPlayer = props => {
const {src} = props
const {detectVideoType} = useDetectVideoType()
const videoType = detectVideoType(src)
if (videoType === UNKNOWN) return ['p', {...props, children: 'Not supported media type'}]

return [lazy(() => import(`../components/${videoType.PLAYER_COMPONENT}.js`)), props]
if (videoType === UNKNOWN) {
return ['p', {...props, children: 'Not supported media type'}]
}

// Get the component from the map instead of using dynamic import
const Component = PLAYER_COMPONENTS[videoType.PLAYER_COMPONENT]

if (!Component) {
console.error(`[useVideoPlayer] Unknown player component: ${videoType.PLAYER_COMPONENT}`)

return ['p', {...props, children: 'Not supported media type'}]
}

return [Component, props]
}

export default useVideoPlayer
18 changes: 17 additions & 1 deletion components/atom/videoPlayer/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createRef, forwardRef, Suspense, useRef} from 'react'
import {createRef, forwardRef, Suspense, useEffect, useRef, useState} from 'react'

import PropTypes from 'prop-types'

Expand Down Expand Up @@ -34,6 +34,13 @@ const AtomVideoPlayer = forwardRef(
},
forwardedRef = createRef()
) => {
// SSR-safe: Only render on client-side to avoid Suspense issues
const [isClient, setIsClient] = useState(false)

useEffect(() => {
setIsClient(true)
}, [])

const [Component, props] = useVideoPlayer({
autoPlay,
controls,
Expand All @@ -58,6 +65,15 @@ const AtomVideoPlayer = forwardRef(
const needsToRenderFallbackComponent =
fallbackWhileNotOnViewport === true && autoPlay === AUTOPLAY.ON_VIEWPORT && !autoPlayState

// Don't render during SSR - only on client
if (!isClient) {
return (
<div ref={componentRef} className={BASE_CLASS}>
{fallbackComponent}
</div>
)
}

return (
<div ref={componentRef} className={BASE_CLASS}>
{needsToRenderFallbackComponent ? (
Expand Down
Loading