File size: 3,477 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
import { localize } from 'i18n-calypso';
import { get } from 'lodash';
import { connect } from 'react-redux';
import QueryPosts from 'calypso/components/data/query-posts';
import HeaderCake from 'calypso/components/header-cake';
import { convertDateToUserLocation } from 'calypso/components/post-schedule/utils';
import StickyPanel from 'calypso/components/sticky-panel';
import { decodeEntities, stripHTML } from 'calypso/lib/formatting';
import { gmtOffset, timezone } from 'calypso/lib/site/utils';
import { bumpStat, composeAnalytics, recordTracksEvent } from 'calypso/state/analytics/actions';
import { getSiteComments } from 'calypso/state/comments/selectors';
import { getSitePost } from 'calypso/state/posts/selectors';
import hasNavigated from 'calypso/state/selectors/has-navigated';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import {
	getSelectedSite,
	getSelectedSiteId,
	getSelectedSiteSlug,
} from 'calypso/state/ui/selectors';

function goBack() {
	window.history.back();
}

export const CommentListHeader = ( {
	commentId,
	postDate,
	postId,
	postTitle,
	postUrl,
	recordReaderArticleOpened,
	site,
	siteId,
	siteSlug,
	navigated,
	translate,
} ) => {
	const formattedDate = postDate
		? convertDateToUserLocation( postDate, timezone( site ), gmtOffset( site ) ).format( 'll LT' )
		: '';

	const title = postTitle.trim() || translate( 'Untitled' );

	const shouldUseHistoryBack = window.history.length > 1 && navigated;
	const backHref = ! shouldUseHistoryBack ? `/comments/all/${ siteSlug }` : null;

	return (
		<StickyPanel className="comment-list__header">
			<QueryPosts siteId={ siteId } postId={ postId } />

			<HeaderCake
				actionHref={ postUrl }
				actionIcon="visible"
				actionOnClick={ recordReaderArticleOpened }
				actionText={ translate( 'View Post' ) }
				onClick={ shouldUseHistoryBack ? goBack : undefined }
				backHref={ backHref }
				alwaysShowActionText
			>
				<div className="comment-list__header-title">
					{ translate(
						'Comment on {{span}}%(postTitle)s{{/span}}',
						'Comments on {{span}}%(postTitle)s{{/span}}',
						{
							count: commentId ? 1 : 2,
							args: { postTitle: title },
							components: { span: <span className="comment-list__header-post-title" /> },
						}
					) }
				</div>
				<div className="comment-list__header-date">{ formattedDate }</div>
			</HeaderCake>
		</StickyPanel>
	);
};

const mapStateToProps = ( state, { postId } ) => {
	const site = getSelectedSite( state );
	const siteId = getSelectedSiteId( state );
	const siteSlug = getSelectedSiteSlug( state );
	const post = getSitePost( state, siteId, postId );
	const postDate = get( post, 'date' );
	const postTitle = decodeEntities(
		stripHTML(
			get( post, 'title' ) ||
				get( post, 'excerpt' ) ||
				get( getSiteComments( state, siteId ), '[0].post.title' )
		)
	);
	const isJetpack = isJetpackSite( state, siteId );
	const navigated = hasNavigated( state );

	return {
		postDate,
		postTitle,
		postUrl: isJetpack ? get( post, 'URL' ) : `/reader/blogs/${ siteId }/posts/${ postId }`,
		site,
		siteId,
		siteSlug,
		navigated,
	};
};

const mapDispatchToProps = ( dispatch ) => ( {
	recordReaderArticleOpened: () =>
		dispatch(
			composeAnalytics(
				recordTracksEvent( 'calypso_comment_management_article_opened' ),
				bumpStat( 'calypso_comment_management', 'article_opened' )
			)
		),
} );

export default connect( mapStateToProps, mapDispatchToProps )( localize( CommentListHeader ) );