File size: 1,373 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
import { get } from 'lodash';
import { GUIDED_TOUR_UPDATE, ROUTE_SET, SITE_SETTINGS_RECEIVE } from 'calypso/state/action-types';
import { THEMES_REQUEST_SUCCESS } from 'calypso/state/themes/action-types';

const relevantAnalyticsEvents = [ 'calypso_themeshowcase_theme_click' ];

const relevantTypes = {
	// to catch all actions of a type:
	// ACTION_TYPE,
	// to catch actions of a type that match some criterion:
	// ACTION_TYPE: ( action ) => isValid( action.data )
	GUIDED_TOUR_UPDATE,
	THEMES_REQUEST_SUCCESS,
	ROUTE_SET,
	SITE_SETTINGS_RECEIVE,
};

const hasRelevantAnalytics = ( action ) =>
	get( action, 'meta.analytics', [] ).some( ( record ) =>
		relevantAnalyticsEvents.includes( record.payload.name )
	);

const isRelevantActionType = ( action ) =>
	relevantTypes.hasOwnProperty( action.type ) &&
	( typeof relevantTypes[ action.type ] !== 'function' || relevantTypes[ action.type ]( action ) );

const overSome = ( checks ) => ( item ) => checks.some( ( check ) => check( item ) );
const isRelevantAction = overSome( [ isRelevantActionType, hasRelevantAnalytics ] );

const newAction = ( action ) => ( {
	...action,
	timestamp: Date.now(),
} );

const maybeAdd = ( state, action ) => ( action ? [ ...state, action ].slice( -50 ) : state );

export default ( state = [], action ) =>
	isRelevantAction( action ) ? maybeAdd( state, newAction( action ) ) : state;