Skip to content

Commit

Permalink
fix: ensure props.media is an array
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 21, 2019
1 parent 006168a commit 435f570
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 12 additions & 4 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isNil,
isObject,
preferMedia,
someProp
someProp,
castArray
} from './utils'

import { useIntersectionObserver } from './utils/hooks'
Expand Down Expand Up @@ -43,16 +44,23 @@ function Microlink (props) {
className = '',
size,
lazy,
media: mediaProp,
...restProps
} = props

const mediaProps = castArray(mediaProp)

const isLoadingUndefined = useMemo(() => loadingProp === undefined, [
loadingProp
])
const [loadingState, setLoading] = useState(
isLoadingUndefined ? true : loadingProp
)
const [cardData, setCardData] = useState({})
const [apiUrl, apiUrlProps] = useMemo(() => getApiUrl(props), [props])
const [apiUrl, apiUrlProps] = useMemo(
() => getApiUrl({ ...props, media: mediaProps }),
[props]
)

const isLazyEnabled = useMemo(
() => isLazySupported && (lazy === true || isObject(lazy)),
Expand Down Expand Up @@ -95,14 +103,14 @@ function Microlink (props) {
let isVideo = false
let isAudio = false

if (!isNil(audio) && preferMedia(props.media) === 'audio') {
if (!isNil(audio) && preferMedia(mediaProps) === 'audio') {
isAudio = true
audioUrl = getUrlPath(audio)
} else if (!isNil(video)) {
isVideo = true
videoUrl = getUrlPath(video)
} else {
media = someProp(payload, props.media) || mediaFallback
media = someProp(payload, mediaProps) || mediaFallback
}

const imageUrl = getUrlPath(media)
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const REGEX_LOCALHOST = /http:\/\/localhost/

const isSSR = typeof window === 'undefined'

export const castArray = value => [].concat(value)

export const preferMedia = props => {
const audioIndex = props.findIndex(propName => propName === 'audio')
const videoIndex = props.findIndex(propName => propName === 'video')
return audioIndex < videoIndex ? 'audio' : 'video'
return audioIndex > videoIndex ? 'audio' : 'video'
}

export const isFunction = fn => typeof fn === 'function'
Expand Down Expand Up @@ -37,7 +39,6 @@ export const media = {

export const getApiUrl = ({
apiKey,
audio,
contrast = false,
data,
force,
Expand Down

0 comments on commit 435f570

Please sign in to comment.