|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect, useMemo, useRef, useState } from "react" |
|
4
|
// @ts-expect-error can't install the types package because it doesn't support React v19 |
|
5
|
import { CSSTransition, SwitchTransition } from "react-transition-group" |
|
6
|
import { Solutions } from "@/components/Feedback/Solutions" |
|
7
|
import { ExtraData, useAnalytics } from "@/providers/Analytics" |
|
8
|
import clsx from "clsx" |
|
9
|
import { Button } from "@/components/Button" |
|
10
|
import { TextArea } from "@/components/TextArea" |
|
11
|
import { Label } from "@/components/Label" |
|
12
|
import { DottedSeparator } from "@/components/DottedSeparator" |
|
13
|
import { RadioItem } from "@/components/RadioItem" |
|
14
|
import { ChatBubbleLeftRight, ThumbDown, ThumbUp } from "@medusajs/icons" |
|
15
|
import Link from "next/link" |
|
16
|
import { useSiteConfig } from "@/providers/SiteConfig" |
|
17
|
import { RadioGroup } from "@medusajs/ui" |
|
18
|
|
|
19
|
export type FeedbackProps = { |
|
20
|
event: string |
|
21
|
reportLink?: string |
|
22
|
question?: string |
|
23
|
positiveBtn?: string |
|
24
|
negativeBtn?: string |
|
25
|
positiveQuestion?: string |
|
26
|
negativeQuestion?: string |
|
27
|
submitBtn?: string |
|
28
|
submitMessage?: string |
|
29
|
showPossibleSolutions?: boolean |
|
30
|
className?: string |
|
31
|
extraData?: ExtraData |
|
32
|
vertical?: boolean |
|
33
|
showDottedSeparator?: boolean |
|
34
|
questionClassName?: string |
|
35
|
} & React.HTMLAttributes<HTMLDivElement> |
|
36
|
|
|
37
|
const feedbackOptions = { |
|
38
|
positive: [ |
|
39
|
"Easy to understand", |
|
40
|
"Accurate code and text", |
|
41
|
"Exactly what I was looking for", |
|
42
|
"Ease of use", |
|
43
|
"Other", |
|
44
|
], |
|
45
|
negative: [ |
|
46
|
"Difficult to understand", |
|
47
|
"Inaccurate code or text", |
|
48
|
"Didn't find what I was looking for", |
|
49
|
"Trouble using the documentation", |
|
50
|
"Other", |
|
51
|
], |
|
52
|
} |
|
53
|
|
|
54
|
export const Feedback = ({ |
|
55
|
event, |
|
56
|
reportLink: initReportLink, |
|
57
|
question = "Was this page helpful?", |
|
58
|
positiveBtn = "It was helpful", |
|
59
|
negativeBtn = "It wasn't helpful", |
|
60
|
positiveQuestion = "What did you like?", |
|
61
|
negativeQuestion = "What was the problem?", |
|
62
|
submitBtn = "Submit", |
|
63
|
submitMessage = "Thank you for helping improve our documentation!", |
|
64
|
showPossibleSolutions = true, |
|
65
|
className = "", |
|
66
|
extraData = {}, |
|
67
|
vertical = false, |
|
68
|
showDottedSeparator = true, |
|
69
|
questionClassName, |
|
70
|
}: FeedbackProps) => { |
|
71
|
const { |
|
72
|
config: { reportIssueLink }, |
|
73
|
} = useSiteConfig() |
|
74
|
const reportLink = useMemo(() => { |
|
75
|
return initReportLink || reportIssueLink |
|
76
|
}, [initReportLink, reportIssueLink]) |
|
77
|
const [showForm, setShowForm] = useState(false) |
|
78
|
const [submittedFeedback, setSubmittedFeedback] = useState(false) |
|
79
|
const [loading, setLoading] = useState(false) |
|
80
|
const inlineFeedbackRef = useRef<HTMLDivElement>(null) |
|
81
|
const inlineQuestionRef = useRef<HTMLDivElement>(null) |
|
82
|
const inlineMessageRef = useRef<HTMLDivElement>(null) |
|
83
|
const [positiveFeedback, setPositiveFeedback] = useState(false) |
|
84
|
const [message, setMessage] = useState("") |
|
85
|
const [feedbackOption, setFeedbackOption] = useState("") |
|
86
|
const nodeRef = submittedFeedback |
|
87
|
? inlineMessageRef |
|
88
|
: showForm |
|
89
|
? inlineQuestionRef |
|
90
|
: inlineFeedbackRef |
|
91
|
const { track } = useAnalytics() |
|
92
|
|
|
93
|
function handleFeedback(feedback: boolean) { |
|
94
|
setPositiveFeedback(feedback) |
|
95
|
setShowForm(true) |
|
96
|
submitFeedback(feedback) |
|
97
|
} |
|
98
|
|
|
99
|
function submitFeedback(feedback = false) { |
|
100
|
if (showForm) { |
|
101
|
setLoading(true) |
|
102
|
} |
|
103
|
track({ |
|
104
|
event: { |
|
105
|
event, |
|
106
|
options: { |
|
107
|
feedback: |
|
108
|
(feedback !== null && feedback) || |
|
109
|
(feedback === null && positiveFeedback) |
|
110
|
? "yes" |
|
111
|
: "no", |
|
112
|
message: message?.length ? message : null, |
|
113
|
feedbackOption, |
|
114
|
...extraData, |
|
115
|
}, |
|
116
|
callback: function () { |
|
117
|
if (showForm) { |
|
118
|
setLoading(false) |
|
119
|
resetForm() |
|
120
|
} |
|
121
|
}, |
|
122
|
}, |
|
123
|
}) |
|
124
|
} |
|
125
|
|
|
126
|
function resetForm() { |
|
127
|
setShowForm(false) |
|
128
|
setSubmittedFeedback(true) |
|
129
|
} |
|
130
|
|
|
131
|
useEffect(() => { |
|
132
|
setFeedbackOption("Other") |
|
133
|
}, [positiveFeedback]) |
|
134
|
|
|
135
|
return ( |
|
136
|
<div className={clsx(className)}> |
|
137
|
{showDottedSeparator && ( |
|
138
|
<DottedSeparator |
|
139
|
wrapperClassName="!px-0 !my-docs_2" |
|
140
|
data-testid="dotted-separator" |
|
141
|
/> |
|
142
|
)} |
|
143
|
<SwitchTransition mode="out-in"> |
|
144
|
<CSSTransition |
|
145
|
key={ |
|
146
|
showForm |
|
147
|
? "show_form" |
|
148
|
: !submittedFeedback |
|
149
|
? "feedback" |
|
150
|
: "submitted_feedback" |
|
151
|
} |
|
152
|
nodeRef={nodeRef} |
|
153
|
timeout={300} |
|
154
|
addEndListener={(done: () => void) => { |
|
155
|
nodeRef.current?.addEventListener("transitionend", done, false) |
|
156
|
}} |
|
157
|
classNames={{ |
|
158
|
enter: "animate-fadeIn animation-fill-forwards animate-fast", |
|
159
|
exit: "animate-fadeOut animation-fill-forwards animate-fast", |
|
160
|
}} |
|
161
|
> |
|
162
|
<> |
|
163
|
{!showForm && !submittedFeedback && ( |
|
164
|
<div |
|
165
|
className={clsx( |
|
166
|
"flex gap-docs_0.5", |
|
167
|
!vertical && "flex-col md:flex-row md:items-center", |
|
168
|
vertical && "flex-col justify-center" |
|
169
|
)} |
|
170
|
ref={inlineFeedbackRef} |
|
171
|
data-testid="feedback-form" |
|
172
|
> |
|
173
|
<Label |
|
174
|
className={clsx( |
|
175
|
"text-compact-small text-medusa-fg-base", |
|
176
|
questionClassName |
|
177
|
)} |
|
178
|
data-testid="question-label" |
|
179
|
> |
|
180
|
{question} |
|
181
|
</Label> |
|
182
|
<div |
|
183
|
className={clsx( |
|
184
|
"flex gap-docs_0.5", |
|
185
|
"flex-col md:flex-row md:items-center" |
|
186
|
)} |
|
187
|
> |
|
188
|
<Button |
|
189
|
onClick={() => handleFeedback(true)} |
|
190
|
className={clsx( |
|
191
|
"positive gap-[6px] !justify-start md:!justify-center", |
|
192
|
"!px-docs_0.5 !py-docs_0.25 text-left md:text-center" |
|
193
|
)} |
|
194
|
variant="transparent-clear" |
|
195
|
data-testid="positive-button" |
|
196
|
> |
|
197
|
<ThumbUp className="text-medusa-fg-subtle" /> |
|
198
|
<span className="text-medusa-fg-base text-compact-small-plus flex-1"> |
|
199
|
{positiveBtn} |
|
200
|
</span> |
|
201
|
</Button> |
|
202
|
<Button |
|
203
|
onClick={() => handleFeedback(false)} |
|
204
|
className={clsx( |
|
205
|
"gap-[6px] !justify-start md:!justify-center", |
|
206
|
"!px-docs_0.5 !py-docs_0.25 text-left md:text-center" |
|
207
|
)} |
|
208
|
variant="transparent-clear" |
|
209
|
data-testid="negative-button" |
|
210
|
> |
|
211
|
<ThumbDown className="text-medusa-fg-subtle" /> |
|
212
|
<span className="text-medusa-fg-base text-compact-small-plus flex-1"> |
|
213
|
{negativeBtn} |
|
214
|
</span> |
|
215
|
</Button> |
|
216
|
{reportLink && ( |
|
217
|
<Button |
|
218
|
variant="transparent-clear" |
|
219
|
className={clsx( |
|
220
|
"gap-[6px] relative", |
|
221
|
"!px-docs_0.5 !py-docs_0.25", |
|
222
|
"!justify-start md:!justify-center", |
|
223
|
"text-left md:text-center" |
|
224
|
)} |
|
225
|
data-testid="report-issue-button" |
|
226
|
> |
|
227
|
<ChatBubbleLeftRight className="text-medusa-fg-subtle" /> |
|
228
|
<span className="text-medusa-fg-base text-compact-small-plus flex-1"> |
|
229
|
Report Issue |
|
230
|
</span> |
|
231
|
<Link |
|
232
|
href={reportLink} |
|
233
|
className="absolute left-0 top-0 w-full h-full" |
|
234
|
></Link> |
|
235
|
</Button> |
|
236
|
)} |
|
237
|
</div> |
|
238
|
</div> |
|
239
|
)} |
|
240
|
{showForm && !submittedFeedback && ( |
|
241
|
<div className="flex flex-col gap-docs_1" ref={inlineQuestionRef}> |
|
242
|
<Label data-testid="submit-question-label"> |
|
243
|
{positiveFeedback ? positiveQuestion : negativeQuestion} |
|
244
|
</Label> |
|
245
|
<RadioGroup className="gap-docs_0.5"> |
|
246
|
{feedbackOptions[ |
|
247
|
positiveFeedback ? "positive" : "negative" |
|
248
|
].map((option) => ( |
|
249
|
<div |
|
250
|
className="flex items-center gap-x-docs_0.5 cursor-pointer group" |
|
251
|
key={option} |
|
252
|
tabIndex={-1} |
|
253
|
onClick={() => setFeedbackOption(option)} |
|
254
|
> |
|
255
|
<RadioItem |
|
256
|
checked={feedbackOption === option} |
|
257
|
value={option} |
|
258
|
onChange={() => setFeedbackOption(option)} |
|
259
|
className={clsx( |
|
260
|
feedbackOption !== option && |
|
261
|
"group-hover:bg-medusa-bg-component-hover" |
|
262
|
)} |
|
263
|
data-testid="feedback-option" |
|
264
|
/> |
|
265
|
<Label className="text-medusa-fg-base text-compact-small-plus"> |
|
266
|
{option} |
|
267
|
</Label> |
|
268
|
</div> |
|
269
|
))} |
|
270
|
</RadioGroup> |
|
271
|
<TextArea |
|
272
|
rows={4} |
|
273
|
value={message} |
|
274
|
onChange={(e) => setMessage(e.target.value)} |
|
275
|
placeholder="Please provide as many details as possible to help us improve the documentation." |
|
276
|
/> |
|
277
|
<Button |
|
278
|
onClick={() => submitFeedback(positiveFeedback)} |
|
279
|
disabled={loading} |
|
280
|
className="w-fit" |
|
281
|
variant="secondary" |
|
282
|
data-testid="submit-button" |
|
283
|
> |
|
284
|
{submitBtn} |
|
285
|
</Button> |
|
286
|
</div> |
|
287
|
)} |
|
288
|
{submittedFeedback && ( |
|
289
|
<div> |
|
290
|
<div |
|
291
|
className="text-compact-large-plus flex flex-col" |
|
292
|
ref={inlineMessageRef} |
|
293
|
data-testid="submitted-message" |
|
294
|
> |
|
295
|
<span>{submitMessage}</span> |
|
296
|
{showPossibleSolutions && ( |
|
297
|
<Solutions message={message} feedback={positiveFeedback} /> |
|
298
|
)} |
|
299
|
</div> |
|
300
|
</div> |
|
301
|
)} |
|
302
|
</> |
|
303
|
</CSSTransition> |
|
304
|
</SwitchTransition> |
|
305
|
{showDottedSeparator && ( |
|
306
|
<DottedSeparator wrapperClassName="!px-0 !my-docs_2" /> |
|
307
|
)} |
|
308
|
</div> |
|
309
|
) |
|
310
|
} |