lbry-desktop/ui/component/channelThumbnail/view.jsx

41 lines
1.3 KiB
React
Raw Normal View History

2019-05-07 04:35:04 +02:00
// @flow
import React from 'react';
2019-05-11 21:06:22 +02:00
import { parseURI } from 'lbry-redux';
import classnames from 'classnames';
import Gerbil from './gerbil.png';
2019-05-07 04:35:04 +02:00
type Props = {
thumbnail: ?string,
uri: ?string,
2019-06-11 20:10:58 +02:00
className?: string,
thumbnailPreview: ?string,
obscure?: boolean,
2019-05-07 04:35:04 +02:00
};
function ChannelThumbnail(props: Props) {
const { thumbnail, uri, className, thumbnailPreview, obscure } = props;
// Generate a random color class based on the first letter of the channel name
let initializer;
if (thumbnail) {
const { channelName } = parseURI(uri);
initializer = channelName.charCodeAt(0) - 65; // will be between 0 and 57
} else {
// if we want to default a thumbnail
initializer = Math.floor(Math.random() * 104729); // 10000th prime number
}
const colorClassName = `channel-thumbnail__default--${initializer % 4}`;
2019-08-02 17:11:31 +02:00
const showThumb = !obscure && !!thumbnail;
2019-05-07 04:35:04 +02:00
return (
2019-05-11 21:06:22 +02:00
<div
2019-06-11 20:10:58 +02:00
className={classnames('channel-thumbnail', className, {
2019-08-02 17:11:31 +02:00
[colorClassName]: !showThumb,
2019-05-11 21:06:22 +02:00
})}
>
2019-08-02 17:11:31 +02:00
{!showThumb && <img className="channel-thumbnail__default" src={thumbnailPreview || Gerbil} />}
{showThumb && <img className="channel-thumbnail__custom" src={thumbnailPreview || thumbnail} />}
2019-05-07 04:35:04 +02:00
</div>
);
}
export default ChannelThumbnail;