-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
231 lines (203 loc) · 6.34 KB
/
index.js
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
/**
* External dependencies
*/
import {
constant,
difference,
find,
findLast,
flatMap,
get,
includes,
map,
startsWith,
uniq,
pick,
} from 'lodash';
import debugFactory from 'debug';
/**
* Internal dependencies
*/
import { GUIDED_TOUR_UPDATE, ROUTE_SET } from 'state/action-types';
import { getSectionName, getSectionGroup } from 'state/ui/selectors';
import getCurrentQueryArguments from 'state/selectors/get-current-query-arguments';
import getInitialQueryArguments from 'state/selectors/get-initial-query-arguments';
import { getActionLog } from 'state/ui/action-log/selectors';
import { preferencesLastFetchedTimestamp } from 'state/preferences/selectors';
import GuidedToursConfig from 'layout/guided-tours/config';
import createSelector from 'lib/create-selector';
import findOngoingTour from './find-ongoing-tour';
import getToursHistory from './get-tours-history';
const SECTIONS_WITHOUT_TOURS = [
'signup',
'upgrades', // checkout
'checkout-thank-you', // thank you page
];
const debug = debugFactory( 'calypso:guided-tours' );
const mappable = ( x ) => ( ! Array.isArray( x ) ? [ x ] : x );
const relevantFeatures = flatMap( GuidedToursConfig, ( tourMeta, key ) =>
mappable( tourMeta.path ).map( ( path ) => ( {
tour: key,
when: tourMeta.when,
path,
} ) )
);
/*
* Returns a collection of tour names. These tours are selected if the user has
* recently navigated to a section of Calypso that comes with a corresponding
* tour.
*/
const getToursFromFeaturesReached = createSelector(
( state ) =>
uniq(
getActionLog( state )
.filter( ( { type } ) => type === ROUTE_SET )
.reduceRight( ( allTours, { path: triggerPath } ) => {
const newTours = relevantFeatures
.filter( ( { path: featurePath } ) => startsWith( triggerPath, featurePath ) )
.map( ( feature ) => feature.tour );
return newTours ? [ ...allTours, ...newTours ] : allTours;
}, [] )
),
[ getActionLog ]
);
/*
* Returns the names of the tours that the user has previously seen, both
* recently and in the past.
*/
const getToursSeen = createSelector(
( state ) => uniq( map( getToursHistory( state ), 'tourName' ) ),
[ getToursHistory ]
);
/*
* Returns the name and timestamp of the tour requested via the URL's query
* arguments, if the tour exists. Returns `undefined` otherwise.
*/
const getTourFromQuery = createSelector(
( state ) => {
const initial = getInitialQueryArguments( state );
const current = getCurrentQueryArguments( state );
const tourProps = [ 'tour', '_timestamp' ];
const { tour, _timestamp } =
current && current.tour ? pick( current, tourProps ) : pick( initial, tourProps );
if ( tour && find( relevantFeatures, { tour } ) ) {
return { tour, _timestamp };
}
},
[ getInitialQueryArguments, getCurrentQueryArguments ]
);
/*
* Returns true if `tour` has been seen in the current Calypso session, false
* otherwise.
*/
const hasJustSeenTour = ( state, { tour, _timestamp } ) =>
getToursHistory( state ).some(
( entry ) => entry.tourName === tour && entry.timestamp > _timestamp
);
/*
* Returns the name of the tour requested via URL query arguments if it hasn't
* "just" been seen (i.e., in the current Calypso session).
*/
const findRequestedTour = ( state ) => {
const requestedTour = getTourFromQuery( state );
if ( requestedTour && ! hasJustSeenTour( state, requestedTour ) ) {
return requestedTour.tour;
}
};
/*
* Returns the name of the first tour available from triggers, assuming the
* tour hasn't been ruled out (e.g. if it has already been seen, or if the
* "when" isn't right).
*/
const findTriggeredTour = ( state ) => {
if ( ! preferencesLastFetchedTimestamp( state ) ) {
debug( 'No fresh user preferences, bailing.' );
return;
}
const toursFromTriggers = uniq( [
...getToursFromFeaturesReached( state ),
// Right now, only one source from which to derive tours, but we may
// have more later. Examples:
// ...getToursFromPurchases( state ),
// ...getToursFromFirstActions( state ),
] );
const toursToDismiss = uniq( [
// Same idea here.
...getToursSeen( state ),
] );
const newTours = difference( toursFromTriggers, toursToDismiss );
return find( newTours, ( tour ) => {
const { when = constant( true ) } = find( relevantFeatures, { tour } );
return when( state );
} );
};
const doesSectionAllowTours = ( state ) =>
! includes( SECTIONS_WITHOUT_TOURS, getSectionName( state ) );
export const hasTourJustBeenVisible = createSelector(
( state, now = Date.now() ) => {
const last = findLast( getActionLog( state ), {
type: GUIDED_TOUR_UPDATE,
shouldShow: false,
} );
// threshold is one minute
return last && now - last.timestamp < 60000;
},
[ getActionLog ]
);
const shouldBailAllTours = ( state ) => ! doesSectionAllowTours( state );
const shouldBailNewTours = ( state ) => hasTourJustBeenVisible( state );
export const findEligibleTour = createSelector(
( state ) => {
if ( shouldBailAllTours( state ) ) {
return;
}
return (
findOngoingTour( state ) ||
( ! shouldBailNewTours( state ) &&
( findRequestedTour( state ) || findTriggeredTour( state ) ) ) ||
undefined
);
},
// Though other state selectors are used in `findEligibleTour`'s body,
// we're intentionally reducing the list of dependants to the following:
[ getActionLog, getToursHistory ]
);
/**
* Returns the current state for Guided Tours.
*
* This includes the raw state from state/ui/guidedTour, but also the available
* configuration (`stepConfig`) for the currently active tour step, if one is
* active.
*
* @param {object} state Global state tree
* @returns {object} Current Guided Tours state
*/
const getRawGuidedTourState = ( state ) => get( state, 'ui.guidedTour', false );
export const getGuidedTourState = createSelector(
( state ) => {
const emptyState = { shouldShow: false };
const tourState = getRawGuidedTourState( state );
const tour = findEligibleTour( state );
const isGutenberg = getSectionGroup( state ) === 'gutenberg';
const shouldShow = !! tour && ! isGutenberg;
const isPaused = !! tourState.isPaused;
debug(
'tours: reached',
getToursFromFeaturesReached( state ),
'seen',
getToursSeen( state ),
'found',
tour
);
if ( ! tour ) {
return { ...tourState, ...emptyState };
}
return {
...tourState,
tour,
shouldShow,
isPaused,
};
},
[ getRawGuidedTourState, getActionLog, preferencesLastFetchedTimestamp ]
);