/
InfoIndicatorButton.js
321 lines (302 loc) · 8.7 KB
/
InfoIndicatorButton.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
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
319
320
321
import React, { useEffect, useState } from "react"
import { formatDistance } from "date-fns"
import IndicatorButton from "./IndicatorButton"
import { infoIcon, infoAlertIcon } from "../icons"
import {
FeedbackTooltipContent,
BuildErrorTooltipContent,
BuildSuccessTooltipContent,
} from "../tooltips"
import { useTrackEvent, useCookie, useFeedback } from "../../utils"
import {
FEEDBACK_COOKIE_NAME,
FEEDBACK_URL,
INTERACTION_COOKIE_NAME,
} from "../../constants"
import { BuildStatus } from "../../models/enums"
const InfoIndicatorButton = ({
buttonIndex,
orgId,
siteId,
erroredBuildId,
isOnPrettyUrl,
sitePrefix,
buildId,
createdAt,
buildStatus,
nodeManifestRedirectUrl,
usingContentSync,
nodeManifestErrorMessage,
}) => {
const initialButtonProps = { buttonIndex, testId: `info`, hoverable: true }
const [buttonProps, setButtonProps] = useState(initialButtonProps)
const { setCookie } = useCookie()
const { shouldShowFeedback } = useFeedback()
const { track } = useTrackEvent()
const trackClick = () => {
track({
eventType: `PREVIEW_INDICATOR_CLICK`,
orgId,
siteId,
buildId,
name: `info click`,
})
}
const trackHover = () => {
track({
eventType: `PREVIEW_INDICATOR_HOVER`,
orgId,
siteId,
buildId,
name: `info hover`,
})
}
const updateTootipVisibility = visible => {
setButtonProps(btnProps => {
return {
...btnProps,
tooltip: {
...btnProps.tooltip,
overrideShow: visible,
show: visible,
},
}
})
}
const closeInfoTooltip = () => {
trackClick()
setButtonProps(btnProps => {
return {
...btnProps,
tooltip: {
...btnProps.tooltip,
show: false,
overrideShow: false,
hoverable: false,
},
}
})
}
const onTooltipToogle = () => {
trackClick()
setButtonProps(btnProps => {
return {
...btnProps,
tooltip: {
...btnProps.tooltip,
overrideShow: !btnProps.tooltip.overrideShow,
},
}
})
}
const closeFeedbackTooltip = () => {
setButtonProps(btnProps => {
return {
...btnProps,
tooltip: {
...btnProps.tooltip,
overrideShow: false,
show: false,
},
highlighted: false,
}
})
// added settimeout until reveist to refactor code
setTimeout(() => {
const now = new Date()
setCookie(FEEDBACK_COOKIE_NAME, now.toISOString())
setCookie(INTERACTION_COOKIE_NAME, 0)
}, 500)
}
const onInfoClick = () => {
if (buttonProps?.active && buttonProps?.onClick) {
buttonProps.onClick()
} else if (buttonProps?.active) {
trackClick()
}
}
useEffect(() => {
const displaySimpleMessage = message => {
setButtonProps({
...initialButtonProps,
tooltip: {
testId: initialButtonProps.testId,
content: message,
overrideShow: false,
show: false,
hoverable: true,
},
active: true,
hoverable: true,
})
}
const buildStatusActions = {
[BuildStatus.UPTODATE]: () => {
if (shouldShowFeedback) {
const url = FEEDBACK_URL
setButtonProps({
...initialButtonProps,
tooltip: {
testId: initialButtonProps.testId,
content: (
<FeedbackTooltipContent
url={url}
onOpened={() => {
closeFeedbackTooltip()
}}
/>
),
overrideShow: true,
closable: true,
hoverable: false,
onClose: closeFeedbackTooltip,
},
active: true,
highlighted: true,
hoverable: true,
})
} else {
const message = `Preview updated ${formatDistance(
Date.now(),
new Date(createdAt),
{ includeSeconds: true }
)} ago`
displaySimpleMessage(message)
}
},
[BuildStatus.SUCCESS]: () => {
setButtonProps({
...initialButtonProps,
tooltip: {
testId: initialButtonProps.testId,
content: (
<BuildSuccessTooltipContent
isOnPrettyUrl={isOnPrettyUrl}
sitePrefix={sitePrefix}
buildId={buildId}
siteId={siteId}
orgId={orgId}
/>
),
overrideShow: true,
closable: true,
hoverable: false,
onClose: closeInfoTooltip,
},
active: true,
hoverable: true,
onClick: onTooltipToogle,
})
},
[BuildStatus.ERROR]: () => {
setButtonProps({
...initialButtonProps,
tooltip: {
testId: initialButtonProps.testId,
content: (
<BuildErrorTooltipContent
siteId={siteId}
orgId={orgId}
buildId={erroredBuildId}
/>
),
overrideShow: true,
closable: true,
hoverable: false,
onClose: closeInfoTooltip,
},
active: true,
hoverable: true,
onClick: onTooltipToogle,
})
},
[BuildStatus.BUILDING]: displaySimpleMessage(`Building your preview...`),
[BuildStatus.QUEUED]: () =>
displaySimpleMessage(`Kicking off your build...`),
[BuildStatus.UPLOADING]: () => displaySimpleMessage(`Deploying.....`),
}
// this is because the build status enum is used for ui state - so we can't have 1 ui state that covers multiple build statuses.
// If we don't have content sync info (pollForNodeManifest), we have to assume that a currently building build isn't applicable to the current user. So we default to the least noisy state which is UPTODATE.
// @todo refactor and decouple build states from UI states - this state should be something like uiState === `IDLE`
buildStatusActions[BuildStatus.BUILDING] =
buildStatusActions[BuildStatus.UPTODATE]
const contentSyncSuccessAction = nodeManifestRedirectUrl
? () => {
setButtonProps(btnProps => {
return {
...btnProps,
tooltip: {
testId: btnProps.testId,
content: (
<BuildSuccessTooltipContent
nodeManifestRedirectUrl={nodeManifestRedirectUrl}
/>
),
overrideShow: true,
closable: true,
onClose: closeInfoTooltip,
},
active: true,
hoverable: true,
showSpinner: false,
onClick: onTooltipToogle,
}
})
}
: false
const contentSyncLoadingAction = usingContentSync
? () => {
setButtonProps(btnProps => {
return {
...btnProps,
tooltip: {
testId: btnProps.testId,
content: `Building a new preview`,
overrideShow: true,
},
active: true,
hoverable: true,
showSpinner: true,
}
})
}
: false
const contentSyncErrorAction = nodeManifestErrorMessage
? buildStatusActions[BuildStatus.ERROR]
: false
const contentSyncAction =
contentSyncSuccessAction ||
contentSyncErrorAction ||
contentSyncLoadingAction
const buildStatusAction =
// with the introduction of Content Sync eager redirects, button state is no longer tied to build status. So we need a separate action for Content Sync.
contentSyncAction || buildStatusActions[buildStatus]
if (typeof buildStatusAction === `function`) {
buildStatusAction()
} else {
setButtonProps({ ...buttonProps, active: true, hoverable: true })
}
}, [
buildStatus,
shouldShowFeedback,
nodeManifestRedirectUrl,
nodeManifestErrorMessage,
])
const showNotificationInfoIcon =
shouldShowFeedback ||
nodeManifestRedirectUrl ||
nodeManifestErrorMessage ||
// we only use build statuses to update UI state when not running content sync logic
(!usingContentSync &&
[BuildStatus.SUCCESS, BuildStatus.ERROR].includes(buildStatus))
return (
<IndicatorButton
{...buttonProps}
onClick={onInfoClick}
onMouseEnter={buttonProps?.active ? trackHover : undefined}
onTooltipToogle={updateTootipVisibility}
iconSvg={showNotificationInfoIcon ? infoAlertIcon : infoIcon}
/>
)
}
export default InfoIndicatorButton