2017-05-03 09:04:16 +09:00
|
|
|
import React from 'react';
|
2017-09-14 10:39:10 +09:00
|
|
|
import PropTypes from 'prop-types';
|
2017-11-25 23:41:45 +09:00
|
|
|
import Immutable from 'immutable';
|
2017-01-20 09:00:14 +09:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2017-05-21 00:31:47 +09:00
|
|
|
import punycode from 'punycode';
|
2017-09-01 23:20:16 +09:00
|
|
|
import classnames from 'classnames';
|
2017-01-20 09:00:14 +09:00
|
|
|
|
2017-05-04 22:52:08 +09:00
|
|
|
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('.');
|
2017-05-21 00:31:47 +09:00
|
|
|
};
|
2017-01-20 09:00:14 +09:00
|
|
|
|
|
|
|
const getHostname = url => {
|
|
|
|
const parser = document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
return parser.hostname;
|
|
|
|
};
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
const trim = (text, len) => {
|
|
|
|
const cut = text.indexOf(' ', len);
|
|
|
|
|
|
|
|
if (cut === -1) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.substring(0, cut) + (text.length > len ? '…' : '');
|
|
|
|
};
|
|
|
|
|
2017-06-24 02:36:54 +09:00
|
|
|
export default class Card extends React.PureComponent {
|
2017-01-20 09:00:14 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
static propTypes = {
|
2017-05-21 00:31:47 +09:00
|
|
|
card: ImmutablePropTypes.map,
|
2017-09-14 10:39:10 +09:00
|
|
|
maxDescription: PropTypes.number,
|
2017-11-25 23:41:45 +09:00
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2017-09-14 10:39:10 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
maxDescription: 50,
|
2017-05-12 21:44:10 +09:00
|
|
|
};
|
|
|
|
|
2017-10-08 09:34:49 +09:00
|
|
|
state = {
|
2018-02-15 15:04:28 +09:00
|
|
|
width: 280,
|
|
|
|
embedded: false,
|
2017-10-08 09:34:49 +09:00
|
|
|
};
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (this.props.card !== nextProps.card) {
|
|
|
|
this.setState({ embedded: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 23:41:45 +09:00
|
|
|
handlePhotoClick = () => {
|
|
|
|
const { card, onOpenMedia } = this.props;
|
|
|
|
|
|
|
|
onOpenMedia(
|
|
|
|
Immutable.fromJS([
|
|
|
|
{
|
|
|
|
type: 'image',
|
2017-12-15 07:31:14 +09:00
|
|
|
url: card.get('embed_url'),
|
2017-11-25 23:41:45 +09:00
|
|
|
description: card.get('title'),
|
|
|
|
meta: {
|
|
|
|
original: {
|
|
|
|
width: card.get('width'),
|
|
|
|
height: card.get('height'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
handleEmbedClick = () => {
|
2017-04-27 21:42:22 +09:00
|
|
|
const { card } = this.props;
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
if (card.get('type') === 'photo') {
|
|
|
|
this.handlePhotoClick();
|
|
|
|
} else {
|
|
|
|
this.setState({ embedded: true });
|
|
|
|
}
|
2017-04-27 21:42:22 +09:00
|
|
|
}
|
|
|
|
|
2017-10-08 09:34:49 +09:00
|
|
|
setRef = c => {
|
|
|
|
if (c) {
|
|
|
|
this.setState({ width: c.offsetWidth });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 21:42:22 +09:00
|
|
|
renderVideo () {
|
2017-10-08 09:34:49 +09:00
|
|
|
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);
|
2017-04-27 21:42:22 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2017-10-08 09:34:49 +09:00
|
|
|
ref={this.setRef}
|
2018-02-15 15:04:28 +09:00
|
|
|
className='status-card__image status-card-video'
|
2017-04-27 21:42:22 +09:00
|
|
|
dangerouslySetInnerHTML={content}
|
2017-10-08 09:34:49 +09:00
|
|
|
style={{ height }}
|
2017-04-27 21:42:22 +09:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-02-15 15:04:28 +09:00
|
|
|
const { card, maxDescription } = this.props;
|
|
|
|
const { width, embedded } = this.state;
|
2017-04-27 21:42:22 +09:00
|
|
|
|
|
|
|
if (card === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
const provider = card.get('provider_name').length === 0 ? decodeIDNA(getHostname(card.get('url'))) : card.get('provider_name');
|
|
|
|
const horizontal = card.get('width') > card.get('height') && (card.get('width') + 100 >= width) || card.get('type') !== 'link';
|
|
|
|
const className = classnames('status-card', { horizontal });
|
|
|
|
const interactive = card.get('type') !== 'link';
|
|
|
|
const title = interactive ? <a className='status-card__title' href={card.get('url')} title={card.get('title')} rel='noopener' target='_blank'><strong>{card.get('title')}</strong></a> : <strong className='status-card__title' title={card.get('title')}>{card.get('title')}</strong>;
|
|
|
|
const ratio = card.get('width') / card.get('height');
|
|
|
|
const height = card.get('width') > card.get('height') ? (width / ratio) : (width * ratio);
|
|
|
|
|
|
|
|
const description = (
|
|
|
|
<div className='status-card__content'>
|
|
|
|
{title}
|
|
|
|
{!horizontal && <p className='status-card__description'>{trim(card.get('description') || '', maxDescription)}</p>}
|
|
|
|
<span className='status-card__host'>{provider}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
let embed = '';
|
|
|
|
let thumbnail = <div style={{ backgroundImage: `url(${card.get('image')})`, width: horizontal ? width : null, height: horizontal ? height : null }} className='status-card__image-image' />;
|
|
|
|
|
|
|
|
if (interactive) {
|
|
|
|
if (embedded) {
|
|
|
|
embed = this.renderVideo();
|
|
|
|
} else {
|
|
|
|
let iconVariant = 'play';
|
|
|
|
|
|
|
|
if (card.get('type') === 'photo') {
|
|
|
|
iconVariant = 'search-plus';
|
|
|
|
}
|
|
|
|
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
|
|
|
{thumbnail}
|
|
|
|
|
|
|
|
<div className='status-card__actions'>
|
|
|
|
<div>
|
|
|
|
<button onClick={this.handleEmbedClick}><i className={`fa fa-${iconVariant}`} /></button>
|
|
|
|
<a href={card.get('url')} target='_blank' rel='noopener'><i className='fa fa-external-link' /></a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className} ref={this.setRef}>
|
|
|
|
{embed}
|
|
|
|
{description}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (card.get('image')) {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
|
|
|
{thumbnail}
|
|
|
|
</div>
|
|
|
|
);
|
2017-04-27 21:42:22 +09:00
|
|
|
}
|
2018-02-15 15:04:28 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<a href={card.get('url')} className={className} target='_blank' rel='noopener' ref={this.setRef}>
|
|
|
|
{embed}
|
|
|
|
{description}
|
|
|
|
</a>
|
|
|
|
);
|
2017-04-27 21:42:22 +09:00
|
|
|
}
|
2017-05-21 00:31:47 +09:00
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|