2023-05-28 14:18:23 +02:00
|
|
|
import { PureComponent } from 'react';
|
2017-09-14 03:39:10 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2018-04-08 23:06:33 +02:00
|
|
|
import Immutable from 'immutable';
|
2017-01-20 01:00:14 +01:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2020-06-06 17:41:56 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-09-01 16:20:16 +02:00
|
|
|
import classnames from 'classnames';
|
2022-10-11 10:41:15 +02:00
|
|
|
import { decode as decodeIDNA } from 'flavours/glitch/utils/idna';
|
2023-05-09 03:11:56 +02:00
|
|
|
import { Icon } from 'flavours/glitch/components/icon';
|
2022-10-11 10:17:04 +02:00
|
|
|
import { useBlurhash } from 'flavours/glitch/initial_state';
|
2023-05-09 03:11:56 +02:00
|
|
|
import { Blurhash } from 'flavours/glitch/components/blurhash';
|
2017-01-20 01:00:14 +01:00
|
|
|
|
|
|
|
const getHostname = url => {
|
|
|
|
const parser = document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
return parser.hostname;
|
|
|
|
};
|
|
|
|
|
2018-09-10 17:58:21 +02:00
|
|
|
const domParser = new DOMParser();
|
|
|
|
|
|
|
|
const addAutoPlay = html => {
|
|
|
|
const document = domParser.parseFromString(html, 'text/html').documentElement;
|
|
|
|
const iframe = document.querySelector('iframe');
|
|
|
|
|
|
|
|
if (iframe) {
|
|
|
|
if (iframe.src.indexOf('?') !== -1) {
|
|
|
|
iframe.src += '&';
|
|
|
|
} else {
|
|
|
|
iframe.src += '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe.src += 'autoplay=1&auto_play=1';
|
|
|
|
|
|
|
|
// DOM parser creates html/body elements around original HTML fragment,
|
|
|
|
// so we need to get innerHTML out of the body and not the entire document
|
|
|
|
return document.querySelector('body').innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
2023-05-28 14:18:23 +02:00
|
|
|
export default class Card extends PureComponent {
|
2017-01-20 01:00:14 +01:00
|
|
|
|
2017-05-12 14:44:10 +02:00
|
|
|
static propTypes = {
|
2017-05-20 17:31:47 +02:00
|
|
|
card: ImmutablePropTypes.map,
|
2018-04-08 23:06:33 +02:00
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2018-10-29 14:56:53 +01:00
|
|
|
compact: PropTypes.bool,
|
2020-06-06 17:41:56 +02:00
|
|
|
sensitive: PropTypes.bool,
|
2017-09-14 03:39:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2018-10-29 14:44:04 +01:00
|
|
|
compact: false,
|
2017-05-12 14:44:10 +02:00
|
|
|
};
|
|
|
|
|
2017-10-08 02:34:49 +02:00
|
|
|
state = {
|
2020-06-06 17:41:56 +02:00
|
|
|
previewLoaded: false,
|
2018-09-10 17:53:28 +02:00
|
|
|
embedded: false,
|
2020-06-06 17:41:56 +02:00
|
|
|
revealed: !this.props.sensitive,
|
2017-10-08 02:34:49 +02:00
|
|
|
};
|
|
|
|
|
2023-05-10 09:05:32 +02:00
|
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
2019-03-05 22:51:23 +01:00
|
|
|
if (!Immutable.is(this.props.card, nextProps.card)) {
|
2020-06-06 17:41:56 +02:00
|
|
|
this.setState({ embedded: false, previewLoaded: false });
|
|
|
|
}
|
|
|
|
if (this.props.sensitive !== nextProps.sensitive) {
|
|
|
|
this.setState({ revealed: !nextProps.sensitive });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
2020-06-24 10:25:32 +02:00
|
|
|
window.addEventListener('resize', this.handleResize, { passive: true });
|
2020-06-06 17:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 10:25:32 +02:00
|
|
|
componentWillUnmount () {
|
|
|
|
window.removeEventListener('resize', this.handleResize);
|
|
|
|
}
|
|
|
|
|
2018-04-08 23:06:33 +02:00
|
|
|
handlePhotoClick = () => {
|
|
|
|
const { card, onOpenMedia } = this.props;
|
|
|
|
|
|
|
|
onOpenMedia(
|
|
|
|
Immutable.fromJS([
|
|
|
|
{
|
|
|
|
type: 'image',
|
2018-09-10 17:52:40 +02:00
|
|
|
url: card.get('embed_url'),
|
2018-04-08 23:06:33 +02:00
|
|
|
description: card.get('title'),
|
|
|
|
meta: {
|
|
|
|
original: {
|
|
|
|
width: card.get('width'),
|
|
|
|
height: card.get('height'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]),
|
2020-03-08 16:02:36 +01:00
|
|
|
0,
|
2018-04-08 23:06:33 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
handleEmbedClick = () => {
|
2017-04-27 14:42:22 +02:00
|
|
|
const { card } = this.props;
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
if (card.get('type') === 'photo') {
|
|
|
|
this.handlePhotoClick();
|
|
|
|
} else {
|
|
|
|
this.setState({ embedded: true });
|
|
|
|
}
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2017-04-27 14:42:22 +02:00
|
|
|
|
2017-10-08 02:34:49 +02:00
|
|
|
setRef = c => {
|
2020-06-24 10:25:32 +02:00
|
|
|
this.node = c;
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2017-10-08 02:34:49 +02:00
|
|
|
|
2020-06-06 17:41:56 +02:00
|
|
|
handleImageLoad = () => {
|
|
|
|
this.setState({ previewLoaded: true });
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2020-06-06 17:41:56 +02:00
|
|
|
|
2020-06-25 22:42:01 +02:00
|
|
|
handleReveal = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2020-06-06 17:41:56 +02:00
|
|
|
this.setState({ revealed: true });
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2020-06-06 17:41:56 +02:00
|
|
|
|
2017-04-27 14:42:22 +02:00
|
|
|
renderVideo () {
|
2017-10-08 02:34:49 +02:00
|
|
|
const { card } = this.props;
|
2018-09-10 17:58:21 +02:00
|
|
|
const content = { __html: addAutoPlay(card.get('html')) };
|
2017-04-27 14:42:22 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2017-10-08 02:34:49 +02:00
|
|
|
ref={this.setRef}
|
2018-09-10 17:53:28 +02:00
|
|
|
className='status-card__image status-card-video'
|
2017-04-27 14:42:22 +02:00
|
|
|
dangerouslySetInnerHTML={content}
|
2023-05-02 13:58:48 +02:00
|
|
|
style={{ aspectRatio: `${card.get('width')} / ${card.get('height')}` }}
|
2017-04-27 14:42:22 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2023-05-07 18:22:25 +02:00
|
|
|
const { card, compact } = this.props;
|
2023-05-02 13:58:48 +02:00
|
|
|
const { embedded, revealed } = this.state;
|
2017-04-27 14:42:22 +02:00
|
|
|
|
|
|
|
if (card === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
const provider = card.get('provider_name').length === 0 ? decodeIDNA(getHostname(card.get('url'))) : card.get('provider_name');
|
2023-05-02 13:58:48 +02:00
|
|
|
const horizontal = (!compact && card.get('width') > card.get('height')) || card.get('type') !== 'link' || embedded;
|
2018-09-10 17:53:28 +02:00
|
|
|
const interactive = card.get('type') !== 'link';
|
2018-10-29 14:44:04 +01:00
|
|
|
const className = classnames('status-card', { horizontal, compact, interactive });
|
2019-10-24 22:44:42 +02:00
|
|
|
const title = interactive ? <a className='status-card__title' href={card.get('url')} title={card.get('title')} rel='noopener noreferrer' target='_blank'><strong>{card.get('title')}</strong></a> : <strong className='status-card__title' title={card.get('title')}>{card.get('title')}</strong>;
|
2023-02-24 20:04:38 +01:00
|
|
|
const language = card.get('language') || '';
|
2018-09-10 17:53:28 +02:00
|
|
|
|
|
|
|
const description = (
|
2023-02-24 20:04:38 +01:00
|
|
|
<div className='status-card__content' lang={language}>
|
2018-09-10 17:53:28 +02:00
|
|
|
{title}
|
2023-04-23 22:32:47 +02:00
|
|
|
{!(horizontal || compact) && <p className='status-card__description' title={card.get('description')}>{card.get('description')}</p>}
|
2018-09-10 17:53:28 +02:00
|
|
|
<span className='status-card__host'>{provider}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2023-05-02 13:58:48 +02:00
|
|
|
const thumbnailStyle = {
|
|
|
|
visibility: revealed? null : 'hidden',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (horizontal) {
|
|
|
|
thumbnailStyle.aspectRatio = (compact && !embedded) ? '16 / 9' : `${card.get('width')} / ${card.get('height')}`;
|
|
|
|
}
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
let embed = '';
|
2020-07-09 13:01:30 +02:00
|
|
|
let canvas = (
|
|
|
|
<Blurhash
|
|
|
|
className={classnames('status-card__image-preview', {
|
|
|
|
'status-card__image-preview--hidden': revealed && this.state.previewLoaded,
|
|
|
|
})}
|
|
|
|
hash={card.get('blurhash')}
|
|
|
|
dummy={!useBlurhash}
|
|
|
|
/>
|
|
|
|
);
|
2023-05-02 13:58:48 +02:00
|
|
|
let thumbnail = <img src={card.get('image')} alt='' style={thumbnailStyle} onLoad={this.handleImageLoad} className='status-card__image-image' />;
|
2020-06-06 17:41:56 +02:00
|
|
|
let spoilerButton = (
|
|
|
|
<button type='button' onClick={this.handleReveal} className='spoiler-button__overlay'>
|
|
|
|
<span className='spoiler-button__overlay__label'><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
spoilerButton = (
|
2020-06-26 14:31:13 +02:00
|
|
|
<div className={classnames('spoiler-button', { 'spoiler-button--minified': revealed })}>
|
2020-06-06 17:41:56 +02:00
|
|
|
{spoilerButton}
|
|
|
|
</div>
|
|
|
|
);
|
2018-09-10 17:53:28 +02:00
|
|
|
|
|
|
|
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'>
|
2020-06-06 17:41:56 +02:00
|
|
|
{canvas}
|
2018-09-10 17:53:28 +02:00
|
|
|
{thumbnail}
|
|
|
|
|
2020-06-06 17:41:56 +02:00
|
|
|
{revealed && (
|
|
|
|
<div className='status-card__actions'>
|
|
|
|
<div>
|
|
|
|
<button onClick={this.handleEmbedClick}><Icon id={iconVariant} /></button>
|
|
|
|
{horizontal && <a href={card.get('url')} target='_blank' rel='noopener noreferrer'><Icon id='external-link' /></a>}
|
|
|
|
</div>
|
2018-09-10 17:53:28 +02:00
|
|
|
</div>
|
2020-06-06 17:41:56 +02:00
|
|
|
)}
|
|
|
|
{!revealed && spoilerButton}
|
2018-09-10 17:53:28 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-06-25 22:42:01 +02:00
|
|
|
<div className={className} ref={this.setRef} onClick={revealed ? null : this.handleReveal} role={revealed ? 'button' : null}>
|
2018-09-10 17:53:28 +02:00
|
|
|
{embed}
|
2018-10-29 14:44:04 +01:00
|
|
|
{!compact && description}
|
2018-09-10 17:53:28 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (card.get('image')) {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
2020-06-06 17:41:56 +02:00
|
|
|
{canvas}
|
2018-09-10 17:53:28 +02:00
|
|
|
{thumbnail}
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-10 20:19:45 +01:00
|
|
|
} else {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
2019-09-09 16:41:41 +02:00
|
|
|
<Icon id='file-text' />
|
2019-01-10 20:19:45 +01:00
|
|
|
</div>
|
|
|
|
);
|
2017-04-27 14:42:22 +02:00
|
|
|
}
|
2018-09-10 17:53:28 +02:00
|
|
|
|
|
|
|
return (
|
2019-10-24 22:44:42 +02:00
|
|
|
<a href={card.get('url')} className={className} target='_blank' rel='noopener noreferrer' ref={this.setRef}>
|
2018-09-10 17:53:28 +02:00
|
|
|
{embed}
|
|
|
|
{description}
|
|
|
|
</a>
|
|
|
|
);
|
2017-04-27 14:42:22 +02:00
|
|
|
}
|
2017-05-20 17:31:47 +02:00
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|