File size: 5,828 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
/* eslint-disable wpcalypso/jsx-classname-namespace */
import {
getRelativeTimeString,
getShortDateString,
getNumericDateString,
getISODateString,
} from '@automattic/i18n-utils';
import { localize, getLocaleSlug } from 'i18n-calypso';
import { Component } from 'react';
import { connect } from 'react-redux';
import getIsNoteApproved from '../state/selectors/get-is-note-approved';
import FollowLink from './follow-link';
import { linkProps } from './functions';
const aSecond = 1000;
const aMinute = aSecond * 60;
const anHour = aMinute * 60;
const aDay = anHour * 24;
function getDisplayURL( url ) {
const parser = document.createElement( 'a' );
parser.href = url;
return ( parser.hostname + parser.pathname ).replace( /\/$/, '' );
}
export class UserBlock extends Component {
/**
* Format a timestamp for showing how long ago an event occurred.
* Specifically here for showing when a comment was made.
*
* If within the past five days, a relative time
* e.g. "23 sec. ago", "15 min. ago", "4 hrs. ago", "1 day ago"
*
* If older than five days, absolute date
* e.g. "30 Apr 2015"
*
* If anything goes wrong, ISO date
* e.g. "2020-12-20"
* Localized dates are always better, but ISO dates should be broadly recognizable.
* @param {string} timestamp - Timestamp in Date.parse()'able format
* @returns {string} - Timestamp formatted for display or '' if input invalid
*/
getTimeString = ( timestamp ) => {
const MAX_LENGTH = 15;
const parsedTime = Date.parse( timestamp );
let timeString;
if ( isNaN( parsedTime ) ) {
return '';
}
const localeSlug = getLocaleSlug();
timeString = getShortDateString( parsedTime, localeSlug );
// If the localized short date format is too long, use numeric one.
if ( timeString.length > MAX_LENGTH ) {
timeString = getNumericDateString( parsedTime, localeSlug );
}
// If the localized numeric date format is still too long, use ISO one.
if ( timeString.length > MAX_LENGTH ) {
timeString = getISODateString( parsedTime );
}
// Use relative time strings (e.g. '2 min. ago') for recent dates.
if ( Date.now() - parsedTime < 5 * aDay ) {
const relativeTimeString = getRelativeTimeString( {
timestamp: parsedTime,
locale: localeSlug,
} );
// Only use relative date if it makes sense and is not too long.
if ( relativeTimeString && relativeTimeString.length <= MAX_LENGTH ) {
timeString = relativeTimeString;
}
}
return timeString;
};
render() {
const grav = this.props.block.media[ 0 ];
let home_url = '';
let home_title = '';
let timeIndicator;
let homeTemplate;
let followLink;
if ( this.props.block.meta ) {
if ( this.props.block.meta.links ) {
home_url = this.props.block.meta.links.home || '';
}
if ( this.props.block.meta.titles ) {
home_title = this.props.block.meta.titles.home || '';
}
}
if ( ! home_title && home_url ) {
home_title = getDisplayURL( home_url );
}
if ( 'comment' === this.props.note.type ) {
// if comment is not approved, show the user's url instead of site
// title so it's easier to tell if they are a spammer
if ( ! this.props.isApproved && home_url ) {
home_title = getDisplayURL( home_url );
}
timeIndicator = (
<span className="wpnc__user__timeIndicator">
<a
href={ this.props.note.url }
target="_blank"
rel="noopener noreferrer"
{ ...linkProps( this.props.note ) }
>
{ this.getTimeString( this.props.note.timestamp ) }
</a>
<span className="wpnc__user__bullet" />
</span>
);
} else {
timeIndicator = '';
}
if ( home_title ) {
const homeClassName =
timeIndicator !== '' ? 'wpnc__user__meta wpnc__user__bulleted' : 'wpnc__user__meta';
homeTemplate = (
<div className={ homeClassName }>
<span className="wpnc__user__ago">{ timeIndicator }</span>
<a
className="wpnc__user__site"
href={ home_url }
target="_blank"
rel="noopener noreferrer"
{ ...linkProps( this.props.note, this.props.block ) }
>
{ home_title }
</a>
</div>
);
} else {
homeTemplate = (
<div className="wpnc__user__meta">
<span className="wpnc__user__ago">{ timeIndicator }</span>
</div>
);
}
if (
this.props.block.actions &&
this.props.block.meta.ids.site !== undefined &&
this.props.block.meta.ids.site !== 'undefined'
) {
followLink = (
<FollowLink
site={ this.props.block.meta.ids.site }
isFollowing={ this.props.block.actions.follow }
noteType={ this.props.note.type }
/>
);
}
const userId = this.props.block?.meta?.ids?.user;
const readerProfileUrl = userId
? `https://wordpress.com/reader/users/id/${ userId }`
: home_url;
if ( home_url ) {
return (
<div className="wpnc__user">
<a
className="wpnc__user__site"
href={ readerProfileUrl }
target="_blank"
rel="noreferrer"
>
<img src={ grav.url } height={ grav.height } width={ grav.width } alt="Avatar" />
</a>
<div>
<span className="wpnc__user__username">
<a
className="wpnc__user__home"
href={ readerProfileUrl }
target="_blank"
rel="noreferrer"
>
{ this.props.block.text }
</a>
</span>
</div>
{ homeTemplate }
{ followLink }
</div>
);
}
return (
<div className="wpnc__user">
<img src={ grav.url } height={ grav.height } width={ grav.width } alt="Avatar" />
<span className="wpnc__user__username">{ this.props.block.text }</span>
{ homeTemplate }
{ followLink }
</div>
);
}
}
const mapStateToProps = ( state, { note } ) => ( {
isApproved: getIsNoteApproved( state, note ),
} );
export default connect( mapStateToProps )( localize( UserBlock ) );
|