You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.0 KiB
126 lines
3.0 KiB
8 years ago
|
import React from 'react';
|
||
7 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||
8 years ago
|
import punycode from 'punycode';
|
||
7 years ago
|
import classnames from 'classnames';
|
||
8 years ago
|
|
||
8 years ago
|
const IDNA_PREFIX = 'xn--';
|
||
|
|
||
|
const decodeIDNA = domain => {
|
||
|
return domain
|
||
|
.split('.')
|
||
|
.map(part => part.indexOf(IDNA_PREFIX) === 0 ? punycode.decode(part.slice(IDNA_PREFIX.length)) : part)
|
||
|
.join('.');
|
||
8 years ago
|
};
|
||
8 years ago
|
|
||
|
const getHostname = url => {
|
||
|
const parser = document.createElement('a');
|
||
|
parser.href = url;
|
||
|
return parser.hostname;
|
||
|
};
|
||
|
|
||
8 years ago
|
export default class Card extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
static propTypes = {
|
||
8 years ago
|
card: ImmutablePropTypes.map,
|
||
7 years ago
|
maxDescription: PropTypes.number,
|
||
|
};
|
||
|
|
||
|
static defaultProps = {
|
||
|
maxDescription: 50,
|
||
8 years ago
|
};
|
||
|
|
||
7 years ago
|
state = {
|
||
|
width: 0,
|
||
|
};
|
||
|
|
||
8 years ago
|
renderLink () {
|
||
7 years ago
|
const { card, maxDescription } = this.props;
|
||
8 years ago
|
|
||
8 years ago
|
let image = '';
|
||
|
let provider = card.get('provider_name');
|
||
8 years ago
|
|
||
|
if (card.get('image')) {
|
||
|
image = (
|
||
8 years ago
|
<div className='status-card__image'>
|
||
7 years ago
|
<img src={card.get('image')} alt={card.get('title')} className='status-card__image-image' width={card.get('width')} height={card.get('height')} />
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
if (provider.length < 1) {
|
||
8 years ago
|
provider = decodeIDNA(getHostname(card.get('url')));
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
const className = classnames('status-card', {
|
||
|
'horizontal': card.get('width') > card.get('height'),
|
||
|
});
|
||
|
|
||
8 years ago
|
return (
|
||
7 years ago
|
<a href={card.get('url')} className={className} target='_blank' rel='noopener'>
|
||
8 years ago
|
{image}
|
||
|
|
||
8 years ago
|
<div className='status-card__content'>
|
||
8 years ago
|
<strong className='status-card__title' title={card.get('title')}>{card.get('title')}</strong>
|
||
7 years ago
|
<p className='status-card__description'>{(card.get('description') || '').substring(0, maxDescription)}</p>
|
||
8 years ago
|
<span className='status-card__host'>{provider}</span>
|
||
8 years ago
|
</div>
|
||
|
</a>
|
||
|
);
|
||
|
}
|
||
8 years ago
|
|
||
|
renderPhoto () {
|
||
|
const { card } = this.props;
|
||
|
|
||
|
return (
|
||
|
<a href={card.get('url')} className='status-card-photo' target='_blank' rel='noopener'>
|
||
|
<img src={card.get('url')} alt={card.get('title')} width={card.get('width')} height={card.get('height')} />
|
||
|
</a>
|
||
|
);
|
||
|
}
|
||
|
|
||
7 years ago
|
setRef = c => {
|
||
|
if (c) {
|
||
|
this.setState({ width: c.offsetWidth });
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
renderVideo () {
|
||
7 years ago
|
const { card } = this.props;
|
||
|
const content = { __html: card.get('html') };
|
||
|
const { width } = this.state;
|
||
|
const ratio = card.get('width') / card.get('height');
|
||
|
const height = card.get('width') > card.get('height') ? (width / ratio) : (width * ratio);
|
||
8 years ago
|
|
||
|
return (
|
||
|
<div
|
||
7 years ago
|
ref={this.setRef}
|
||
8 years ago
|
className='status-card-video'
|
||
|
dangerouslySetInnerHTML={content}
|
||
7 years ago
|
style={{ height }}
|
||
8 years ago
|
/>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
render () {
|
||
|
const { card } = this.props;
|
||
|
|
||
|
if (card === null) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
switch(card.get('type')) {
|
||
|
case 'link':
|
||
|
return this.renderLink();
|
||
|
case 'photo':
|
||
|
return this.renderPhoto();
|
||
|
case 'video':
|
||
|
return this.renderVideo();
|
||
|
case 'rich':
|
||
|
default:
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
}
|