-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
useFeedbackWidget.tsx
51 lines (43 loc) · 1.34 KB
/
useFeedbackWidget.tsx
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
import type {RefObject} from 'react';
import {useEffect} from 'react';
import * as Sentry from '@sentry/react';
import {t} from 'sentry/locale';
import ConfigStore from 'sentry/stores/configStore';
import {useLegacyStore} from 'sentry/stores/useLegacyStore';
interface Props {
buttonRef?: RefObject<HTMLButtonElement> | RefObject<HTMLAnchorElement>;
formTitle?: string;
messagePlaceholder?: string;
}
export default function useFeedbackWidget({
buttonRef,
formTitle,
messagePlaceholder,
}: Props) {
const config = useLegacyStore(ConfigStore);
const feedback = Sentry.getFeedback();
useEffect(() => {
if (!feedback) {
return undefined;
}
const options = {
colorScheme: config.theme === 'dark' ? ('dark' as const) : ('light' as const),
buttonLabel: t('Give Feedback'),
submitButtonLabel: t('Send Feedback'),
messagePlaceholder: messagePlaceholder ?? t('What did you expect?'),
formTitle: formTitle ?? t('Give Feedback'),
};
if (buttonRef) {
if (buttonRef.current) {
return feedback.attachTo(buttonRef.current, options);
}
} else {
const widget = feedback.createWidget(options);
return () => {
widget.removeFromDom();
};
}
return undefined;
}, [buttonRef, config.theme, feedback, formTitle, messagePlaceholder]);
return feedback;
}