2020-10-31 23:31:12 +00:00
|
|
|
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'
|
2020-11-03 21:29:38 +00:00
|
|
|
import Dummy from './dummy'
|
2020-10-31 23:31:12 +00:00
|
|
|
|
|
|
|
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() {
|
2020-11-03 21:29:38 +00:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
isUnlinked,
|
|
|
|
card,
|
|
|
|
isVertical,
|
|
|
|
isBordered,
|
|
|
|
} = this.props
|
2020-10-31 23:31:12 +00:00
|
|
|
|
|
|
|
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')
|
2020-11-03 21:29:38 +00:00
|
|
|
const updated = card.get('updated_at')
|
2020-10-31 23:31:12 +00:00
|
|
|
const provider = card.get('provider_name').length === 0 ? decodeIDNA(getHostname(card.get('url'))) : card.get('provider_name')
|
2020-11-03 21:29:38 +00:00
|
|
|
const Wrapper = isUnlinked ? Dummy : Button
|
|
|
|
|
|
|
|
const innerContainerClasses = CX({
|
|
|
|
d: 1,
|
|
|
|
w100PC: 1,
|
|
|
|
flexRow: !isVertical,
|
|
|
|
px15: 1,
|
|
|
|
py15: 1,
|
|
|
|
boxShadowBlock: 1,
|
|
|
|
bgPrimary: 1,
|
|
|
|
overflowHidden: 1,
|
|
|
|
radiusSmall: 1,
|
|
|
|
borderColorSecondary: isBordered,
|
|
|
|
borderTop1PX: isBordered,
|
|
|
|
bgSubtle_onHover: !isUnlinked,
|
|
|
|
})
|
2020-10-31 23:31:12 +00:00
|
|
|
|
|
|
|
return (
|
2020-11-03 21:29:38 +00:00
|
|
|
<Wrapper
|
2020-10-31 23:31:12 +00:00
|
|
|
noClasses
|
|
|
|
to={`/links/${id}`}
|
2020-11-03 21:29:38 +00:00
|
|
|
className={[_s.d, _s.mb10, _s.noUnderline].join(' ')}
|
2020-10-31 23:31:12 +00:00
|
|
|
>
|
2020-11-03 21:29:38 +00:00
|
|
|
<div className={innerContainerClasses}>
|
2020-10-31 23:31:12 +00:00
|
|
|
{
|
|
|
|
!!image &&
|
|
|
|
<Image
|
|
|
|
width='110px'
|
|
|
|
height='110px'
|
|
|
|
alt={'title'}
|
|
|
|
src={image}
|
|
|
|
className={[_s.radiusSmall, _s.overflowHidden, _s.mr10].join(' ')}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
|
|
|
|
<div className={[_s.d, _s.flexNormal].join(' ')}>
|
|
|
|
<div className={_s.d}>
|
|
|
|
<Text size='medium' color='primary' weight='bold'>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
</div>
|
|
|
|
|
2020-11-03 21:29:38 +00:00
|
|
|
{
|
|
|
|
!!description &&
|
|
|
|
<div className={[_s.d, _s.maxH40PX, _s.overflowHidden, _s.pt5].join(' ')}>
|
|
|
|
<Text size='small' color='secondary'>
|
|
|
|
{description}
|
|
|
|
</Text>
|
|
|
|
</div>
|
|
|
|
}
|
2020-10-31 23:31:12 +00:00
|
|
|
|
2020-11-03 21:29:38 +00:00
|
|
|
<Text size='small' color='secondary' className={_s.mt5}>
|
2020-10-31 23:31:12 +00:00
|
|
|
{provider}
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
<div className={[_s.d, _s.flexRow, _s.pt5].join(' ')}>
|
|
|
|
<Text color='secondary' size='small'>
|
2020-11-03 21:29:38 +00:00
|
|
|
<RelativeTimestamp timestamp={updated} />
|
2020-10-31 23:31:12 +00:00
|
|
|
</Text>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-11-03 21:29:38 +00:00
|
|
|
</Wrapper>
|
2020-10-31 23:31:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-11-03 21:29:38 +00:00
|
|
|
isUnlinked: PropTypes.bool,
|
|
|
|
isVertical: PropTypes.bool,
|
|
|
|
isBordered: PropTypes.bool,
|
2020-10-31 23:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(PreviewCardItem)
|