import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import ImmutablePropTypes from 'react-immutable-proptypes' import ImmutablePureComponent from 'react-immutable-pure-component' import { fetchLinkCard } from '../actions/links' import { CX } from '../constants' import { decodeIDNA, getHostname, trim, } from '../utils/urls' import DotTextSeperator from './dot_text_seperator' import Text from './text' import Button from './button' import Image from './image' import RelativeTimestamp from './relative_timestamp' class PreviewCardItem extends ImmutablePureComponent { componentDidMount() { const { id, card } = this.props if (!!id && !card) { this.props.dispatch(fetchLinkCard(id)) } } componentDidUpdate(prevProps) { if (prevProps.id !== this.props.id && !this.props.card) { this.props.dispatch(fetchLinkCard(this.props.id)) } } render() { const { id, card } = this.props if (!card) return null const title = card.get('title') const maxDescription = 120 const description = trim(card.get('description') || '', maxDescription) const image = card.get('image') const website = card.get('url') const provider = card.get('provider_name').length === 0 ? decodeIDNA(getHostname(card.get('url'))) : card.get('provider_name') return ( ) } } const mapStateToProps = (state, { id }) => ({ card: state.getIn(['links', 'items', `${id}`]), isLoading: state.getIn(['links', 'isLoading']), isError: state.getIn(['links', 'isError']), }) PreviewCardItem.propTypes = { card: ImmutablePropTypes.map, id: PropTypes.string.isRequired, isLoading: PropTypes.bool.isRequired, isError: PropTypes.bool.isRequired, } export default connect(mapStateToProps)(PreviewCardItem)