-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathguideStore.tsx
More file actions
318 lines (276 loc) · 8.24 KB
/
guideStore.tsx
File metadata and controls
318 lines (276 loc) · 8.24 KB
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
import {createStore} from 'reflux';
import getGuidesContent from 'sentry/components/assistant/getGuidesContent';
import type {
Guide,
GuidesContent,
GuidesServerData,
} from 'sentry/components/assistant/types';
import ConfigStore from 'sentry/stores/configStore';
import HookStore from 'sentry/stores/hookStore';
import ModalStore from 'sentry/stores/modalStore';
import type {Organization} from 'sentry/types/organization';
import {trackAnalytics} from 'sentry/utils/analytics';
import type {StrictStoreDefinition} from './types';
function guidePrioritySort(a: Guide, b: Guide) {
const a_priority = a.priority ?? Number.MAX_SAFE_INTEGER;
const b_priority = b.priority ?? Number.MAX_SAFE_INTEGER;
if (a_priority === b_priority) {
return a.guide.localeCompare(b.guide);
}
// lower number takes priority
return a_priority - b_priority;
}
export type GuideStoreState = {
/**
* Anchors that are currently mounted
*/
anchors: Set<string>;
/**
* The current guide
*/
currentGuide: Guide | null;
/**
* Current step of the current guide
*/
currentStep: number;
/**
* Hides guides that normally would be shown
*/
forceHide: boolean;
/**
* We force show a guide if the URL contains #assistant
*/
forceShow: boolean;
/**
* All tooltip guides
*/
guides: Guide[];
/**
* Current organization id
*/
orgId: string | null;
/**
* Current organization slug
*/
orgSlug: string | null;
/**
* Current Organization
*/
organization: Organization | null;
/**
* The previously shown guide
*/
prevGuide: Guide | null;
};
const defaultState: GuideStoreState = {
forceHide: false,
guides: [],
anchors: new Set(),
currentGuide: null,
currentStep: 0,
orgId: null,
orgSlug: null,
organization: null,
forceShow: false,
prevGuide: null,
};
function isForceEnabled() {
return window.location.hash === '#assistant';
}
interface GuideStoreDefinition extends StrictStoreDefinition<GuideStoreState> {
closeGuide(dismissed?: boolean): void;
fetchSucceeded(data: GuidesServerData): void;
modalStoreListener: null | Function;
nextStep(): void;
onURLChange(): void;
recordCue(guide: string): void;
registerAnchor(target: string): void;
setActiveOrganization(data: Organization): void;
setForceHide(forceHide: boolean): void;
teardown(): void;
toStep(step: number): void;
unregisterAnchor(target: string): void;
updateCurrentGuide(dismissed?: boolean): void;
updatePrevGuide(nextGuide: Guide | null): void;
}
const storeConfig: GuideStoreDefinition = {
state: {...defaultState},
modalStoreListener: null,
init() {
// XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
// listeners due to their leaky nature in tests.
this.state = {...defaultState, forceShow: isForceEnabled()};
window.addEventListener('load', this.onURLChange, false);
// Guides will show above modals, but are not interactable because
// of the focus trap, so we force them to be hidden while a modal is open.
this.modalStoreListener = ModalStore.listen(() => {
const isOpen = typeof ModalStore.getState().renderer === 'function';
if (isOpen) {
this.setForceHide(true);
} else {
this.setForceHide(false);
}
}, undefined);
},
teardown() {
window.removeEventListener('load', this.onURLChange);
if (this.modalStoreListener) {
this.modalStoreListener();
}
},
getState() {
return this.state;
},
onURLChange() {
this.state = {...this.state, forceShow: isForceEnabled()};
this.updateCurrentGuide();
},
setActiveOrganization(data: Organization) {
this.state = {
...this.state,
orgId: data ? data.id : null,
orgSlug: data ? data.slug : null,
organization: data ? data : null,
};
this.updateCurrentGuide();
},
fetchSucceeded(data) {
// It's possible we can get empty responses (seems to be Firefox specific)
// Do nothing if `data` is empty
// also, temporarily check data is in the correct format from the updated
// assistant endpoint
if (!data || !Array.isArray(data)) {
return;
}
const guidesContent: GuidesContent = getGuidesContent(this.state.organization);
// map server guide state (i.e. seen status) with guide content
const guides = guidesContent.reduce((acc: Guide[], content) => {
const serverGuide = data.find(guide => guide.guide === content.guide);
if (serverGuide) {
acc.push({
...content,
...serverGuide,
});
}
return acc;
}, []);
this.state = {...this.state, guides};
this.updateCurrentGuide();
},
closeGuide(dismissed?: boolean) {
const {currentGuide, guides} = this.state;
const newGuides = guides.map(guide => {
// update the current guide seen to true or all guides
// if markOthersAsSeen is true and the user is dismissing
if (
guide.guide === currentGuide?.guide ||
(currentGuide?.markOthersAsSeen && dismissed)
) {
return {
...guide,
seen: true,
};
}
return guide;
});
this.state = {...this.state, guides: newGuides, forceShow: false};
this.updateCurrentGuide();
},
nextStep() {
this.state = {...this.state, currentStep: this.state.currentStep + 1};
this.trigger(this.state);
},
toStep(step: number) {
this.state = {...this.state, currentStep: step};
this.trigger(this.state);
},
registerAnchor(target) {
this.state.anchors.add(target);
this.updateCurrentGuide();
},
unregisterAnchor(target) {
this.state.anchors.delete(target);
this.updateCurrentGuide();
},
setForceHide(forceHide) {
this.state = {...this.state, forceHide};
this.trigger(this.state);
},
recordCue(guide) {
const user = ConfigStore.get('user');
if (!user) {
return;
}
trackAnalytics('assistant.guide_cued', {
organization: this.state.orgId,
guide,
});
},
updatePrevGuide(nextGuide) {
const {prevGuide} = this.state;
if (!nextGuide) {
return;
}
if (!prevGuide || prevGuide.guide !== nextGuide.guide) {
this.recordCue(nextGuide.guide);
this.state = {...this.state, prevGuide: nextGuide};
}
},
/**
* Logic to determine if a guide is shown:
*
* - If any required target is missing, don't show the guide
* - If the URL ends with #assistant, show the guide
* - If the user has already seen the guide, don't show the guide
* - Otherwise show the guide
*/
updateCurrentGuide(dismissed?: boolean) {
const {anchors, guides, forceShow} = this.state;
let guideOptions = guides
.sort(guidePrioritySort)
.filter(guide => guide.requiredTargets.every(target => anchors.has(target)));
const user = ConfigStore.get('user');
const assistantThreshold = new Date(2019, 6, 1);
const userDateJoined = new Date(user?.dateJoined);
if (!forceShow) {
guideOptions = guideOptions.filter(({seen, dateThreshold}) => {
if (seen) {
return false;
}
if (user?.isSuperuser) {
return true;
}
if (dateThreshold) {
// Show the guide to users who've joined before the date threshold
return userDateJoined < dateThreshold;
}
return userDateJoined > assistantThreshold;
});
}
// Remove steps that are missing anchors, unless the anchor is included in
// the expectedTargets and will appear at the step.
const nextGuide =
guideOptions.length > 0
? {
...guideOptions[0]!,
steps: guideOptions[0]!.steps.filter(
step =>
anchors.has(step.target) ||
guideOptions[0]?.expectedTargets?.includes(step.target)
),
}
: null;
this.updatePrevGuide(nextGuide);
const currentStep =
this.state.currentGuide &&
nextGuide &&
this.state.currentGuide.guide === nextGuide.guide
? this.state.currentStep
: 0;
this.state = {...this.state, currentGuide: nextGuide, currentStep};
this.trigger(this.state);
HookStore.get('callback:on-guide-update').map(cb => cb(nextGuide, {dismissed}));
},
};
const GuideStore = createStore(storeConfig);
export default GuideStore;