Skip to content

Instantly share code, notes, and snippets.

@tristanpendergrass
Created February 13, 2018 16:52
Show Gist options
  • Save tristanpendergrass/f7d6ae630259724689a1b926a64485e6 to your computer and use it in GitHub Desktop.
Save tristanpendergrass/f7d6ae630259724689a1b926a64485e6 to your computer and use it in GitHub Desktop.
import { Fragment } from 'react';
import {
StringAttribute,
} from 'exp-ui-api-lib';
const defaultStyle = {
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center top',
}
// eslint-disable-next-line no-useless-escape
const YOUTUBE_REGEX = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i;
const getYoutubeUrl = url => {
const variant = 'mqdefault';
const matches = YOUTUBE_REGEX.exec(url);
if (matches && matches[1]) {
const videoId = matches[1];
return `https://img.youtube.com/vi/${videoId}/${variant}.jpg`;
} else {
throw new Error('Not a youtube url');
}
};
const HasYoutubeThumbnail = ({ children }) => (
<StringAttribute
path="url"
render={({ get }) => (
get() && YOUTUBE_REGEX.test(get()) ? children || null : null
)}
/>
);
const YoutubeThumbnail = ({ get, thumbnailStyle }) => (
<div
style={{
...defaultStyle,
...thumbnailStyle,
backgroundImage: `url(${getYoutubeUrl(get())})`,
}}
/>
);
const HasImageThumbnail = ({ children }) => (
<StringAttribute
path="variants"
render={({ get }) => (
(get() || []).some(variant => variant.name === '320.png') ? children || null : null
)}
/>
);
const ImageThumbnail = ({ get, thumbnailStyle }) => (
<div
style={{
...defaultStyle,
...thumbnailStyle,
backgroundImage: `url(https://eagle.goexp.io/api/delivery${get()}?variant=320.png)`,
}}
/>
);
export default ({ thumbnailStyle }) => (
<Fragment>
<HasImageThumbnail>
<StringAttribute path="path" render={ImageThumbnail} thumbnailStyle={thumbnailStyle} />
</HasImageThumbnail>
<HasYoutubeThumbnail>
<StringAttribute path="url" render={YoutubeThumbnail} thumbnailStyle={thumbnailStyle} />
</HasYoutubeThumbnail>
</Fragment>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment