File size: 1,777 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
import { Gridicon, TimeSince } from '@automattic/components';
import { get } from 'lodash';
import { getUserProfileUrl } from 'calypso/reader/user-profile/user-profile.utils';

import './post-comment.scss'; // yes, this is intentional. they share styles.

interface PostTrackbackProps {
	commentId: number;
	commentsTree: Record< number, PostComment >;
}

interface PostComment {
	data: {
		author: {
			ID: number;
			name: string;
			URL: string;
			wpcom_login: string;
		};
		date: string;
		URL: string;
	};
}

function unescape( str: string ): string {
	return str.replace( /&#(\d+);/g, ( match, entity ) => String.fromCharCode( entity ) );
}

export default function PostTrackback( props: PostTrackbackProps ): JSX.Element | null {
	const commentsTree = props.commentsTree;
	const comment = get( commentsTree[ props.commentId ], 'data' );
	if ( ! comment ) {
		return null;
	}
	const unescapedAuthorName = unescape( get( comment, 'author.name', '' ) );

	const authorUrlLink = comment.author?.wpcom_login
		? getUserProfileUrl( comment.author.wpcom_login )
		: comment.author?.URL;

	return (
		<li className="comments__comment depth-0">
			<div className="comments__comment-author">
				<div className="comments__comment-trackbackicon">
					<Gridicon icon="link" size={ 24 } />
				</div>

				{ authorUrlLink ? (
					<a
						href={ authorUrlLink }
						target="_blank"
						rel="noopener noreferrer"
						className="comments__comment-username"
					>
						{ unescapedAuthorName }
					</a>
				) : (
					<strong className="comments__comment-username">{ unescapedAuthorName }</strong>
				) }

				<div className="comments__comment-timestamp">
					<a href={ comment.URL }>
						<TimeSince date={ comment.date } />
					</a>
				</div>
			</div>
		</li>
	);
}