forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonboarding-interactions.ts
120 lines (103 loc) · 3.82 KB
/
onboarding-interactions.ts
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
import { remoteFunction } from 'src/util/webextensionRPC'
import { EVENT_NAMES } from 'src/analytics/internal/constants'
import { getLocalStorage } from 'src/util/storage'
import { FLOWS, STAGES, STORAGE_KEYS } from 'src/overview/onboarding/constants'
import * as utils from 'src/overview/onboarding/utils'
import { destroyRootElement } from 'src/toolbar-notification/content_script/rendering'
const processEventRPC = remoteFunction('processEvent')
/**
* Conditionally trigger after highlight message during onboarding.
* @param toolbarNotifications Toolbar Notification object instance
* @param position Position of the tooltip
*/
export const conditionallyShowHighlightNotification = async ({
toolbarNotifications,
position,
}) => {
if (!utils.isDemoPage()) {
return
}
const annotationStage = await utils.fetchOnboardingStage(FLOWS.annotation)
if (annotationStage !== STAGES.annotation.notifiedHighlightText) {
return
}
// Toolbar Notfication doesn't destroy the previous tooltip by default
// So hack to destroy it using private method.
toolbarNotifications._destroyRootElement()
processEventRPC({
type: EVENT_NAMES.ONBOARDING_HIGHLIGHT_MADE,
})
await utils.setOnboardingStage(
FLOWS.annotation,
STAGES.annotation.notifiedSelectOption,
)
}
/**
* Trigger's the next notification which is seen after the user clicks
* "browse around a bit" in Power Search welcome notification.
*/
const handler = toolbarNotifications => async () => {
toolbarNotifications._destroyRootElement()
toolbarNotifications.showToolbarNotification('go-to-dashboard')
processEventRPC({
type: EVENT_NAMES.POWERSEARCH_BROWSE_PAGE,
})
}
/**
* Shows Toolbar notifications on website based on
* onboarding flags set in local storage.
* @param toolbarNotifications ToolbarNotification instance to trigger notification
*/
export const conditionallyShowOnboardingNotifications = async ({
toolbarNotifications,
}) => {
/*
Fetch shouldShowOnboarding and return if it's false as
that would mean the user has closed the onboarding demo.
*/
const shouldShowOnboarding = await getLocalStorage(
STORAGE_KEYS.shouldShowOnboarding,
true,
)
if (!utils.isDemoPage() || !shouldShowOnboarding) {
return
}
const {
annotationStage,
powerSearchStage,
taggingStage,
} = await utils.fetchAllStages()
if (annotationStage === STAGES.redirected) {
toolbarNotifications.showToolbarNotification('onboarding-higlight-text')
await utils.setOnboardingStage(
FLOWS.annotation,
STAGES.annotation.notifiedHighlightText,
)
}
if (powerSearchStage === STAGES.redirected) {
const position = utils.getPageCenter()
toolbarNotifications._destroyRootElement()
toolbarNotifications.showToolbarNotification('power-search-browse', {
position,
triggerNextNotification: handler(toolbarNotifications),
})
}
if (taggingStage === STAGES.redirected) {
toolbarNotifications.showToolbarNotification('tag-this-page')
}
}
/**
* Conditionally removes the Select Option notifcation in Annotation
* Onboarding Flow. Either used when user clicks outside or an annotation
* is created.
* @param nextStage Next stage to set for annotations flow
*/
export const conditionallyRemoveSelectOption = async nextStage => {
const annotationStage = await utils.fetchOnboardingStage(FLOWS.annotation)
if (annotationStage === STAGES.annotation.notifiedSelectOption) {
await utils.setOnboardingStage(FLOWS.annotation, nextStage)
// Close the curren select-option notification manually since
// accessing the toolbarNotification instance from here is not possible
destroyRootElement()
}
}