File size: 12,119 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import page from '@automattic/calypso-router';
import { Button } from '@wordpress/components';
import closest from 'component-closest';
import i18n, { localize } from 'i18n-calypso';
import { defer, startsWith } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import QueryReaderLists from 'calypso/components/data/query-reader-lists';
import QueryReaderOrganizations from 'calypso/components/data/query-reader-organizations';
import QueryReaderTeams from 'calypso/components/data/query-reader-teams';
import { withCurrentRoute } from 'calypso/components/route';
import GlobalSidebar, { GLOBAL_SIDEBAR_EVENTS } from 'calypso/layout/global-sidebar';
import Sidebar from 'calypso/layout/sidebar';
import SidebarFooter from 'calypso/layout/sidebar/footer';
import SidebarItem from 'calypso/layout/sidebar/item';
import SidebarMenu from 'calypso/layout/sidebar/menu';
import SidebarRegion from 'calypso/layout/sidebar/region';
import SidebarSeparator from 'calypso/layout/sidebar/separator';
import ReaderA8cConversationsIcon from 'calypso/reader/components/icons/a8c-conversations-icon';
import ReaderConversationsIcon from 'calypso/reader/components/icons/conversations-icon';
import ReaderDiscoverIcon from 'calypso/reader/components/icons/discover-icon';
import ReaderLikesIcon from 'calypso/reader/components/icons/likes-icon';
import ReaderManageSubscriptionsIcon from 'calypso/reader/components/icons/manage-subscriptions-icon';
import ReaderSearchIcon from 'calypso/reader/components/icons/search-icon';
import { isAutomatticTeamMember } from 'calypso/reader/lib/teams';
import { getTagStreamUrl } from 'calypso/reader/route';
import { recordAction, recordGaEvent } from 'calypso/reader/stats';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getShouldShowGlobalSidebar } from 'calypso/state/global-sidebar/selectors';
import { recordReaderTracksEvent } from 'calypso/state/reader/analytics/actions';
import { getSubscribedLists } from 'calypso/state/reader/lists/selectors';
import { getReaderOrganizations } from 'calypso/state/reader/organizations/selectors';
import {
	toggleReaderSidebarLists,
	toggleReaderSidebarFollowing,
	toggleReaderSidebarTags,
} from 'calypso/state/reader-ui/sidebar/actions';
import {
	isListsOpen,
	isFollowingOpen,
	isTagsOpen,
} from 'calypso/state/reader-ui/sidebar/selectors';
import { getReaderTeams } from 'calypso/state/teams/selectors';
import { setNextLayoutFocus } from 'calypso/state/ui/layout-focus/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import ReaderSidebarHelper from './helper';
import ReaderSidebarLists from './reader-sidebar-lists';
import ReaderSidebarNudges from './reader-sidebar-nudges';
import ReaderSidebarOrganizations from './reader-sidebar-organizations';
import ReaderSidebarRecent from './reader-sidebar-recent';
import ReaderSidebarTags from './reader-sidebar-tags';
import 'calypso/my-sites/sidebar/style.scss'; // Copy styles from the My Sites sidebar.
import './style.scss';

export class ReaderSidebar extends Component {
	state = {};

	componentDidMount() {
		// If we're browsing a tag or list, open the sidebar menu
		this.openExpandableMenuForCurrentTagOrList();
	}

	handleClick = ( event ) => {
		if ( ! event.isDefaultPrevented() && closest( event.target, 'a,span' ) ) {
			this.props.setNextLayoutFocus( 'content' );
		}
	};

	highlightNewTag( tagSlug ) {
		const tagStreamUrl = getTagStreamUrl( tagSlug );
		if ( tagStreamUrl !== page.current ) {
			defer( function () {
				page( tagStreamUrl );
				window.scrollTo( 0, 0 );
			} );
		}
	}

	openExpandableMenuForCurrentTagOrList = () => {
		const pathParts = this.props.path.split( '/' );

		if ( startsWith( this.props.path, '/tag/' ) ) {
			const tagSlug = pathParts[ 2 ];
			if ( tagSlug ) {
				// Open the sidebar
				if ( ! this.props.isTagsOpen ) {
					this.props.toggleTagsVisibility();
					this.setState( { currentTag: tagSlug } );
				}
			}
		}

		if ( startsWith( this.props.path, '/reader/list/' ) ) {
			const listOwner = pathParts[ 3 ];
			const listSlug = pathParts[ 4 ];
			if ( listOwner && listSlug ) {
				// Open the sidebar
				if ( ! this.props.isListsOpen ) {
					this.props.toggleListsVisibility();
					this.setState( { currentListOwner: listOwner, currentListSlug: listSlug } );
				}
			}
		}
	};

	handleGlobalSidebarMenuItemClick = ( path ) => {
		if ( ! this.props.shouldShowGlobalSidebar ) {
			return;
		}

		this.props.recordTracksEvent( GLOBAL_SIDEBAR_EVENTS.MENU_ITEM_CLICK, {
			section: 'read',
			path,
		} );
	};

	handleReaderSidebarConversationsClicked = ( event, path ) => {
		recordAction( 'clicked_reader_sidebar_conversations' );
		recordGaEvent( 'Clicked Reader Sidebar Conversations' );
		this.props.recordReaderTracksEvent( 'calypso_reader_sidebar_conversations_clicked' );
		this.handleGlobalSidebarMenuItemClick( path );
	};

	handleReaderSidebarA8cConversationsClicked = ( event, path ) => {
		recordAction( 'clicked_reader_sidebar_a8c_conversations' );
		recordGaEvent( 'Clicked Reader Sidebar A8C Conversations' );
		this.props.recordReaderTracksEvent( 'calypso_reader_sidebar_automattic_conversations_clicked' );
		this.handleGlobalSidebarMenuItemClick( path );
	};

	handleReaderSidebarDiscoverClicked = ( event, path ) => {
		recordAction( 'clicked_reader_sidebar_discover' );
		recordGaEvent( 'Clicked Reader Sidebar Discover' );
		this.props.recordReaderTracksEvent( 'calypso_reader_sidebar_discover_clicked' );
		this.handleGlobalSidebarMenuItemClick( path );
	};

	handleReaderSidebarSearchClicked = ( event, path ) => {
		recordAction( 'clicked_reader_sidebar_search' );
		recordGaEvent( 'Clicked Reader Sidebar Search' );
		this.props.recordReaderTracksEvent( 'calypso_reader_sidebar_search_clicked' );
		this.handleGlobalSidebarMenuItemClick( path );
	};

	handleReaderSidebarLikeActivityClicked = ( event, path ) => {
		recordAction( 'clicked_reader_sidebar_like_activity' );
		recordGaEvent( 'Clicked Reader Sidebar Like Activity' );
		this.props.recordReaderTracksEvent( 'calypso_reader_sidebar_like_activity_clicked' );
		this.handleGlobalSidebarMenuItemClick( path );
	};

	handleReaderSidebarManageSubscriptionsClicked = ( event, path ) => {
		recordAction( 'clicked_reader_sidebar_manage_subscriptions' );
		recordGaEvent( 'Clicked Reader Sidebar Manage Subscriptions' );
		this.props.recordReaderTracksEvent( 'calypso_reader_sidebar_manage_subscriptions_clicked' );
		this.handleGlobalSidebarMenuItemClick( path );
	};

	renderSidebarMenu() {
		const { path, translate, teams } = this.props;

		return (
			<div className="sidebar-menu-container">
				<div className="sidebar-header">
					<div>
						<h3>{ translate( 'Reader' ) }</h3>
						<p>{ translate( 'Keep up with your interests.' ) }</p>
					</div>

					<Button
						className="reader-search-icon"
						variant="tertiary"
						href="/reader/search"
						onClick={ this.handleReaderSidebarSearchClicked }
						aria-label={ translate( 'Search' ) }
					>
						<ReaderSearchIcon viewBox="0 0 24 24" />
					</Button>
				</div>
				<SidebarMenu>
					<QueryReaderLists />
					<QueryReaderTeams />
					<QueryReaderOrganizations />

					<li className="sidebar-streams__following">
						<ReaderSidebarRecent
							onClick={ this.props.toggleFollowingVisibility }
							isOpen={ this.props.isFollowingOpen }
							path={ path }
						/>
					</li>

					<SidebarItem
						className={ ReaderSidebarHelper.itemLinkClass( '/discover', path, {
							'sidebar-streams__discover': true,
						} ) }
						label={ translate( 'Discover' ) }
						onNavigate={ this.handleReaderSidebarDiscoverClicked }
						customIcon={ <ReaderDiscoverIcon viewBox="0 0 24 24" /> }
						link="/discover"
					/>

					<SidebarItem
						label={ translate( 'Likes' ) }
						onNavigate={ this.handleReaderSidebarLikeActivityClicked }
						customIcon={ <ReaderLikesIcon viewBox="0 0 24 24" /> }
						link="/activities/likes"
						className={ ReaderSidebarHelper.itemLinkClass( '/activities/likes', path, {
							'sidebar-activity__likes': true,
						} ) }
					/>

					<SidebarItem
						className={ ReaderSidebarHelper.itemLinkClass( '/reader/conversations', path, {
							'sidebar-streams__conversations': true,
						} ) }
						label={ translate( 'Conversations' ) }
						onNavigate={ this.handleReaderSidebarConversationsClicked }
						customIcon={ <ReaderConversationsIcon iconSize={ 24 } viewBox="0 0 24 24" /> }
						link="/reader/conversations"
					/>

					<ReaderSidebarLists
						lists={ this.props.subscribedLists }
						path={ path }
						isOpen={ this.props.isListsOpen }
						onClick={ this.props.toggleListsVisibility }
						currentListOwner={ this.state.currentListOwner }
						currentListSlug={ this.state.currentListSlug }
					/>

					<ReaderSidebarTags
						tags={ this.props.followedTags }
						path={ path }
						isOpen={ this.props.isTagsOpen }
						onClick={ this.props.toggleTagsVisibility }
						onFollowTag={ this.highlightNewTag }
						currentTag={ this.state.currentTag }
					/>

					{ this.props.organizations && (
						<>
							<SidebarSeparator />
							<ReaderSidebarOrganizations
								organizations={ this.props.organizations }
								path={ path }
							/>
						</>
					) }

					{ isAutomatticTeamMember( teams ) && (
						<SidebarItem
							className={ ReaderSidebarHelper.itemLinkClass( '/reader/conversations/a8c', path, {
								'sidebar-streams__conversations': true,
							} ) }
							label="A8C Conversations"
							onNavigate={ this.handleReaderSidebarA8cConversationsClicked }
							link="/reader/conversations/a8c"
							customIcon={ <ReaderA8cConversationsIcon size={ 24 } viewBox="-2 -2 24 24" /> }
						/>
					) }

					<SidebarSeparator />

					<SidebarItem
						className={ ReaderSidebarHelper.itemLinkClass( '/reader/subscriptions', path, {
							'sidebar-streams__manage-subscriptions': true,
						} ) }
						label={ translate( 'Manage Subscriptions' ) }
						onNavigate={ this.handleReaderSidebarManageSubscriptionsClicked }
						customIcon={ <ReaderManageSubscriptionsIcon size={ 24 } viewBox="0 0 24 24" /> }
						link="/reader/subscriptions"
					/>
					{ /*
					Keep a separator at the end to avoid having the last item covered by browser breadcrumbs,
					url links when hovering other items, etc. Otherwise when a user scrolls to the end of the
					sidebar, their cursor is generally on other menu items causing the urls to popup in the
					bottom right and obscure view the last menu item.
				*/ }
					<SidebarSeparator />
				</SidebarMenu>
			</div>
		);
	}

	renderGlobalSidebar() {
		const props = {
			path: this.props.path,
			onClick: this.handleClick,
			siteTitle: i18n.translate( 'Reader' ),
		};
		return (
			<GlobalSidebar { ...props }>
				<ReaderSidebarNudges />
				{ this.renderSidebarMenu() }
			</GlobalSidebar>
		);
	}

	renderSidebar() {
		return (
			<Sidebar onClick={ this.handleClick }>
				<SidebarRegion>
					<ReaderSidebarNudges />
					{ this.renderSidebarMenu() }
				</SidebarRegion>
				<SidebarFooter />
			</Sidebar>
		);
	}

	render() {
		if ( this.props.shouldShowGlobalSidebar ) {
			return this.renderGlobalSidebar();
		}
		return this.renderSidebar();
	}
}

export default withCurrentRoute(
	connect(
		( state, { currentSection } ) => {
			const siteId = getSelectedSiteId( state );
			const shouldShowGlobalSidebar = getShouldShowGlobalSidebar( {
				state,
				siteId,
				section: currentSection,
			} );

			return {
				isListsOpen: isListsOpen( state ),
				isFollowingOpen: isFollowingOpen( state ),
				isTagsOpen: isTagsOpen( state ),
				subscribedLists: getSubscribedLists( state ),
				teams: getReaderTeams( state ),
				organizations: getReaderOrganizations( state ),
				shouldShowGlobalSidebar,
			};
		},
		{
			recordReaderTracksEvent,
			recordTracksEvent,
			setNextLayoutFocus,
			toggleListsVisibility: toggleReaderSidebarLists,
			toggleFollowingVisibility: toggleReaderSidebarFollowing,
			toggleTagsVisibility: toggleReaderSidebarTags,
		}
	)( localize( ReaderSidebar ) )
);