File size: 2,287 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
import config from '@automattic/calypso-config';
import { isEqual } from 'lodash';
import IsCurrentUserAdminSwitch from 'calypso/components/jetpack/is-current-user-admin-switch';
import NotAuthorizedPage from 'calypso/components/jetpack/not-authorized-page';
import ActivityLog from 'calypso/my-sites/activity/activity-log';
import ActivityLogV2 from 'calypso/my-sites/activity/activity-log-v2';
import { recordTrack } from 'calypso/reader/stats';
import { setFilter } from 'calypso/state/activity-log/actions';
import { queryToFilterState } from 'calypso/state/activity-log/utils';
import getActivityLogFilter from 'calypso/state/selectors/get-activity-log-filter';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';

function queryFilterToStats( filter ) {
	// These values are hardcoded so that the attributes that we collect via stats are not unbound
	const possibleGroups = [
		'attachment',
		'comment',
		'core',
		'feedback',
		'jetpack',
		'menu',
		'module',
		'monitor',
		'pingback',
		'plan',
		'plugin',
		'post',
		'protect',
		'rewind',
		'setting',
		'theme',
		'user',
		'widget',
	];

	const groupStats = {};
	possibleGroups.forEach( ( groupSlug ) => {
		groupStats[ 'filter_group_' + groupSlug ] = !! (
			filter.group && filter.group.indexOf( groupSlug ) >= 0
		);
	} );

	return {
		...groupStats,
		filter_date_on: !! filter.on || !! filter.before || !! filter.after,
		page: filter.page ? parseInt( filter.page ) : 1,
	};
}

export function activity( context, next ) {
	const state = context.store.getState();
	const siteId = getSelectedSiteId( state );

	const filter = siteId && getActivityLogFilter( state, siteId );
	const queryFilter = queryToFilterState( context.query );

	if ( ! isEqual( filter, queryFilter ) ) {
		context.store.dispatch( setFilter( siteId, queryFilter, true ) );
	}

	recordTrack( 'calypso_activitylog_view', queryFilterToStats( queryFilter ) );
	context.primary = config.isEnabled( 'activity-log/v2' ) ? (
		<ActivityLogV2 />
	) : (
		<ActivityLog siteId={ siteId } context={ context } />
	);

	next();
}

export function showNotAuthorizedForNonAdmins( context, next ) {
	context.primary = (
		<IsCurrentUserAdminSwitch
			trueComponent={ context.primary }
			falseComponent={ <NotAuthorizedPage /> }
		/>
	);

	next();
}