Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components/atom/videoPlayer): add factory hook #2524

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 0 additions & 27 deletions components/atom/videoPlayer/src/components/Switcher.js

This file was deleted.

26 changes: 11 additions & 15 deletions components/atom/videoPlayer/src/components/VimeoPlayer.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import PropTypes from 'prop-types'

import useVimeoProperties from '../hooks/useVimeoProperties.js'
import {BASE_CLASS} from '../settings.js'

const VimeoPlayer = ({src}) => {
const {getEmbeddableUrl} = useVimeoProperties()
const videoSrc = getEmbeddableUrl(src)

return (
<>
<div className={`${BASE_CLASS}-vimeoPlayer`}>
<iframe
className={`${BASE_CLASS}-vimeoPlayerFrame`}
title="VIMEO video player"
src={videoSrc}
frameBorder="0"
allow="autoplay; fullscreen; picture-in-picture"
allowFullScreen
></iframe>
</div>
{/* Is this really needed? */}
{/* <script src="https://player.vimeo.com/api/player.js"></script> */}
</>
<iframe
title="VIMEO video player"
src={videoSrc}
frameBorder="0"
allow="autoplay; fullscreen; picture-in-picture"
allowFullScreen
/>
)
/*
** Is this really needed?
** <script src="https://player.vimeo.com/api/player.js"></script>
* */
}

VimeoPlayer.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const YouTubePlayer = ({src}) => {
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen
></iframe>
/>
)
}

Expand Down
4 changes: 4 additions & 0 deletions components/atom/videoPlayer/src/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import YouTubePlayer from './YouTubePlayer.js'
import VimeoPlayer from './VimeoPlayer.js'

export {YouTubePlayer, VimeoPlayer}
15 changes: 0 additions & 15 deletions components/atom/videoPlayer/src/hooks/useDetectVideoType.js

This file was deleted.

35 changes: 35 additions & 0 deletions components/atom/videoPlayer/src/hooks/useVideoPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {VIMEO, YOUTUBE, BASE_CLASS} from '../settings.js'
import {VimeoPlayer, YouTubePlayer} from '../components/index.js'

const matcher = (arrayPattern, value) =>
arrayPattern.find(pattern => value.includes(pattern))

const useVideoPlayer = ({
as: As = 'div',
children = 'Not supported media type',
className,
src = '',
...props
} = {}) => {
if (matcher([YOUTUBE.VIDEO_TYPE, YOUTUBE.SHORT_URL], src)) {
return [
YouTubePlayer,
{
className: [BASE_CLASS, `${BASE_CLASS}-youtubePlayer`].join(' '),
src,
...props
}
]
} else if (matcher([VIMEO.VIDEO_TYPE], src)) {
return [
VimeoPlayer,
{
className: [BASE_CLASS, `${BASE_CLASS}-vimeoPlayer`].join(' '),
src,
...props
}
]
}
return ['h1', {className: BASE_CLASS, children, ...props}]
}
Comment on lines +7 to +34
Copy link
Collaborator

@oegea oegea Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your proposal.

I'm wondering if this is an agreement of how to create these kind of structures in Sui.

Probably I'm missing the benefit or architectural pattern behind this change, but IMO this is making the same effect than the already existing code, but in a less obvious way, delegating some representation responsibilities to the useVideoPlayer hook.

All of this is still entirely my opinion, but without knowing the benefits of applying this alternative approach, it's more difficult in my opinion to read the code and follow the logic flow. For example is less obvious were the vimeoPlayer style class is used. In the already existing approach, the Vimeo player component self-contains both all the needed logic and css class references, and the switch mechanism has a very specific and limited responsibility.

Maybe we can have a discussion about this when you have a moment for us!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduces the VDOM tree reconciliation. Take a look on react-dev-tools, there is non Switcher component

export default useVideoPlayer
22 changes: 15 additions & 7 deletions components/atom/videoPlayer/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import {forwardRef} from 'react'
import PropTypes from 'prop-types'

import Switcher from './components/Switcher.js'
import {BASE_CLASS} from './settings.js'
export default function AtomVideoPlayer({src = ''}) {
import useVideoPlayer from './hooks/useVideoPlayer.js'

const AtomVideoPlayer = forwardRef((props, forwardedRef) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, we totally forgot about forwarding ref.

const [Component, {className, as: As = 'div', ...componentProps}] =
useVideoPlayer(props)
return (
<div className={BASE_CLASS}>
<Switcher src={src} />
</div>
<As className={className} ref={forwardedRef}>
<Component {...componentProps} />
</As>
)
}
})

AtomVideoPlayer.displayName = 'AtomVideoPlayer'
AtomVideoPlayer.propTypes = {
/* Render the passed value as the correspondent HTML tag or the component if a function is passed */
as: PropTypes.elementType,
// TODO: document
src: PropTypes.string
}

export default AtomVideoPlayer
Loading