|
| 1 | +'use client'; |
| 2 | +import {Fragment, useEffect, useState} from 'react'; |
| 3 | +import {CheckIcon as Check} from '@radix-ui/react-icons'; |
| 4 | +import {Button} from '@radix-ui/themes'; |
| 5 | +import * as Sentry from '@sentry/browser'; |
| 6 | + |
| 7 | +import {usePlausibleEvent} from 'sentry-docs/hooks/usePlausibleEvent'; |
| 8 | + |
| 9 | +type Props = { |
| 10 | + pathname: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export function DocFeedback({pathname}: Props) { |
| 14 | + const {emit} = usePlausibleEvent(); |
| 15 | + const [showFeedback, setShowFeedback] = useState(false); |
| 16 | + const [feedbackSubmitted, setFeedbackSubmitted] = useState(false); |
| 17 | + const [feedbackType, setFeedbackType] = useState<'helpful' | 'not_helpful' | null>( |
| 18 | + null |
| 19 | + ); |
| 20 | + |
| 21 | + // Initialize feedback state from sessionStorage |
| 22 | + useEffect(() => { |
| 23 | + const storedFeedback = sessionStorage.getItem(`feedback_${pathname}`); |
| 24 | + if (storedFeedback === 'submitted') { |
| 25 | + setFeedbackSubmitted(true); |
| 26 | + } |
| 27 | + }, [pathname]); |
| 28 | + |
| 29 | + const handleFeedback = (helpful: boolean) => { |
| 30 | + emit('Doc Feedback', {props: {page: pathname, helpful}}); |
| 31 | + setFeedbackType(helpful ? 'helpful' : 'not_helpful'); |
| 32 | + setShowFeedback(true); |
| 33 | + }; |
| 34 | + |
| 35 | + const handleSubmitFeedback = (e: React.FormEvent<HTMLFormElement>) => { |
| 36 | + e.preventDefault(); |
| 37 | + const formData = new FormData(e.currentTarget); |
| 38 | + const comments = formData.get('comments') as string; |
| 39 | + |
| 40 | + try { |
| 41 | + Sentry.captureFeedback( |
| 42 | + { |
| 43 | + message: comments, |
| 44 | + }, |
| 45 | + {captureContext: {tags: {page: pathname, type: feedbackType}}} |
| 46 | + ); |
| 47 | + setFeedbackSubmitted(true); |
| 48 | + sessionStorage.setItem(`feedback_${pathname}`, 'submitted'); |
| 49 | + } catch (error) { |
| 50 | + if (process.env.NODE_ENV === 'development') { |
| 51 | + // eslint-disable-next-line no-console |
| 52 | + console.error('Failed to submit feedback:', error); |
| 53 | + } |
| 54 | + Sentry.captureException(error); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <Fragment> |
| 60 | + <div className="space-y-4 py-4 border-[var(--gray-6)]"> |
| 61 | + {feedbackSubmitted ? ( |
| 62 | + <div className="flex items-center gap-2 text-sm text-[var(--gray-11)]"> |
| 63 | + <Check className="w-4 h-4" /> Thanks for your feedback |
| 64 | + </div> |
| 65 | + ) : ( |
| 66 | + <Fragment> |
| 67 | + <div className="flex items-center gap-4 text-sm mt-8"> |
| 68 | + <span className="font-medium">Was this helpful?</span> |
| 69 | + <div className="flex"> |
| 70 | + <button |
| 71 | + onClick={() => handleFeedback(true)} |
| 72 | + className="py-1 px-2 gap-4 hover:bg-[var(--gray-3)] rounded flex items-center justify-center" |
| 73 | + aria-label="Yes, this was helpful" |
| 74 | + > |
| 75 | + Yes 👍 |
| 76 | + </button> |
| 77 | + <button |
| 78 | + onClick={() => handleFeedback(false)} |
| 79 | + className="py-1 px-2 gap-4 hover:bg-[var(--gray-3)] rounded flex items-center justify-center" |
| 80 | + aria-label="No, this wasn't helpful" |
| 81 | + > |
| 82 | + No 👎 |
| 83 | + </button> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div |
| 88 | + className={`overflow-hidden transition-all duration-300 ease-in-out ${ |
| 89 | + showFeedback ? 'max-h-[300px] opacity-100' : 'max-h-0 opacity-0' |
| 90 | + }`} |
| 91 | + > |
| 92 | + <form onSubmit={handleSubmitFeedback} className="space-y-4"> |
| 93 | + <div> |
| 94 | + <label htmlFor="comments" className="block text-sm font-medium mb-4"> |
| 95 | + {feedbackType === 'helpful' |
| 96 | + ? 'What did you like about this page?' |
| 97 | + : 'How can we improve this page?'} |
| 98 | + </label> |
| 99 | + <textarea |
| 100 | + id="comments" |
| 101 | + name="comments" |
| 102 | + required |
| 103 | + rows={2} |
| 104 | + className="w-[calc(100%-4px)] ml-[2px] px-3 py-2 border border-[var(--gray-6)] rounded-lg focus:outline-none focus:ring-2 focus:ring-[var(--accent)] bg-transparent text-sm" |
| 105 | + placeholder="Please share your thoughts..." |
| 106 | + /> |
| 107 | + </div> |
| 108 | + <Button |
| 109 | + type="submit" |
| 110 | + className="px-4 py-2 text-sm rounded-lg bg-[var(--accent-purple)]" |
| 111 | + size={'3'} |
| 112 | + > |
| 113 | + Submit feedback |
| 114 | + </Button> |
| 115 | + </form> |
| 116 | + </div> |
| 117 | + </Fragment> |
| 118 | + )} |
| 119 | + </div> |
| 120 | + </Fragment> |
| 121 | + ); |
| 122 | +} |
0 commit comments