File size: 6,512 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { SegmentedControl } from '@automattic/components';
import { formatNumber } from '@automattic/number-formatters';
import { Icon, external } from '@wordpress/icons';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import moment from 'moment';
import { useState, FunctionComponent } from 'react';
import useReferrersQuery from '../hooks/use-referrers-query';
import useTopPostsQuery from '../hooks/use-top-posts-query';
import { HighLightItem } from '../typings';

import './highlights.scss';

interface ItemWrapperProps {
	siteId: number;
	statsBaseUrl: string;
	isItemLink: boolean;
	item: HighLightItem;
	isItemLinkExternal: boolean;
}

interface TopColumnProps {
	items: Array< HighLightItem >;
	viewAllUrl: string;
	viewAllText: string;
	title: string;
	isLoading: boolean;
	statsBaseUrl: string;
	siteId: number;
	isItemLinkExternal?: boolean;
	isItemLink?: boolean;
	className?: null | string;
}

interface HighlightsProps {
	siteId: number;
	gmtOffset: number;
	statsBaseUrl: string;
}

const HIGHLIGHT_ITEMS_LIMIT = 5;
const HIGHLIGHT_TAB_TOP_POSTS_PAGES = 'topPostsAndPages';
const HIGHLIGHT_TAB_TOP_REFERRERS = 'topReferrers';

const postAndPageLink = ( baseUrl: string, siteId: number, postId: number ) => {
	return `${ baseUrl }/stats/post/${ postId }/${ siteId }`;
};

const externalLink = ( item: HighLightItem ) => {
	// Url is for referrers and href is for top posts and pages.
	return item.url || item.href;
};

const ItemWrapper: FunctionComponent< ItemWrapperProps > = ( {
	statsBaseUrl,
	siteId,
	isItemLink,
	item,
	isItemLinkExternal,
} ) => {
	const translate = useTranslate();

	const renderedItem = (
		<div>
			<p>{ item.title }</p>
			<span>
				{ translate( '%(views)s Views', {
					args: {
						views: formatNumber( item.views ),
					},
				} ) }
			</span>
		</div>
	);

	return isItemLink ? (
		<a
			href={
				isItemLinkExternal ? externalLink( item ) : postAndPageLink( statsBaseUrl, siteId, item.id )
			}
			target={ isItemLinkExternal ? '_blank' : '_self' }
			rel="noopener noreferrer"
			title={ translate( 'View detailed stats for %(title)s', {
				args: {
					title: item.title,
				},
				textOnly: true,
				comment: 'Text for anchor linking to a stats page for a given post/page',
			} ) }
		>
			{ renderedItem }
			{ isItemLinkExternal && <Icon className="stats-icon" icon={ external } size={ 18 } /> }
		</a>
	) : (
		renderedItem
	);
};

const TopColumn: FunctionComponent< TopColumnProps > = ( {
	items,
	viewAllUrl,
	viewAllText,
	title,
	isLoading,
	statsBaseUrl,
	siteId,
	isItemLink = false,
	isItemLinkExternal = false,
	className = null,
} ) => {
	const translate = useTranslate();

	return (
		<div className={ clsx( 'stats-widget-highlights-card', className ) }>
			<label className="stats-widget-highlights-card__title">{ title }</label>
			{ items.length === 0 && (
				<p className="stats-widget-highlights-card__empty">
					{ isLoading ? `${ translate( 'Loading' ) }...` : translate( 'No data to show' ) }
				</p>
			) }
			{ items.length > 0 && (
				<ul className="stats-widget-highlights-card__list">
					{ items.slice( 0, HIGHLIGHT_ITEMS_LIMIT ).map( ( item, idx ) => (
						<li key={ idx }>
							<ItemWrapper
								item={ item }
								statsBaseUrl={ statsBaseUrl }
								siteId={ siteId }
								isItemLink={ isItemLink }
								isItemLinkExternal={ isItemLinkExternal }
							/>
						</li>
					) ) }
				</ul>
			) }
			<div className="stats-widget-highlights-card__view-all">
				<a href={ viewAllUrl }>{ viewAllText }</a>
			</div>
		</div>
	);
};

export default function Highlights( { siteId, gmtOffset, statsBaseUrl }: HighlightsProps ) {
	const translate = useTranslate();

	const headingTitle = translate( '7 Day Highlights' );
	const topPostsAndPagesTitle = translate( 'Top Posts & Pages' );
	const topReferrersTitle = translate( 'Top Referrers' );

	const moduleTabs = [
		{
			value: HIGHLIGHT_TAB_TOP_POSTS_PAGES,
			label: topPostsAndPagesTitle,
		},
		{
			value: HIGHLIGHT_TAB_TOP_REFERRERS,
			label: topReferrersTitle,
		},
	];

	// Default to the first tab `topPostsAndPages`.
	const [ selectedTab, setSelectedTab ] = useState( HIGHLIGHT_TAB_TOP_POSTS_PAGES );

	const queryDate = moment()
		.utcOffset( Number.isFinite( gmtOffset ) ? gmtOffset : 0 )
		.format( 'YYYY-MM-DD' );
	const viewAllPostsStatsUrl = `${ statsBaseUrl }/stats/day/posts/${ siteId }?startDate=${ queryDate }&summarize=1&num=7`;
	const viewAllReferrerStatsUrl = `${ statsBaseUrl }/stats/day/referrers/${ siteId }?startDate=${ queryDate }&summarize=1&num=7`;

	const { data: topPostsAndPages = [], isFetching: isFetchingPostsAndPages } = useTopPostsQuery(
		siteId,
		'day',
		7,
		queryDate
	);

	const { data: topReferrers = [], isFetching: isFetchingReferrers } = useReferrersQuery(
		siteId,
		'day',
		7,
		queryDate
	);

	return (
		<div className="stats-widget-highlights stats-widget-card" aria-label={ headingTitle }>
			<div className="stats-widget-highlights__header">
				<label>{ headingTitle }</label>
			</div>
			<div className="stats-widget-highlights__tabs">
				<SegmentedControl primary>
					{ moduleTabs.map( ( tab ) => {
						return (
							<SegmentedControl.Item
								key={ tab.value }
								selected={ tab.value === selectedTab }
								onClick={ () => setSelectedTab( tab.value ) }
							>
								{ tab.label }
							</SegmentedControl.Item>
						);
					} ) }
				</SegmentedControl>
			</div>
			<div className="stats-widget-highlights__body">
				<TopColumn
					className={ clsx( 'stats-widget-highlights__column', {
						'stats-widget-highlights__column--show-in-mobile':
							selectedTab === HIGHLIGHT_TAB_TOP_POSTS_PAGES,
					} ) }
					title={ topPostsAndPagesTitle }
					viewAllUrl={ viewAllPostsStatsUrl }
					viewAllText={ translate( 'View all posts & pages stats' ) }
					items={ topPostsAndPages }
					isLoading={ isFetchingPostsAndPages }
					statsBaseUrl={ statsBaseUrl }
					siteId={ siteId }
					isItemLink
				/>
				<TopColumn
					className={ clsx( 'stats-widget-highlights__column', {
						'stats-widget-highlights__column--show-in-mobile':
							selectedTab === HIGHLIGHT_TAB_TOP_REFERRERS,
					} ) }
					title={ topReferrersTitle }
					viewAllUrl={ viewAllReferrerStatsUrl }
					viewAllText={ translate( 'View all referrer stats' ) }
					items={ topReferrers }
					isLoading={ isFetchingReferrers }
					statsBaseUrl={ statsBaseUrl }
					siteId={ siteId }
					isItemLink
					isItemLinkExternal
				/>
			</div>
		</div>
	);
}