|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useRef, useState } from 'react' |
| 4 | +import { ThumbsDown, ThumbsUp } from '@sim/emcn/icons' |
| 5 | +import { generateId } from '@sim/utils/id' |
| 6 | +import { QueryClient, QueryClientProvider, useMutation } from '@tanstack/react-query' |
| 7 | +import { DocsButton } from '@/components/docs/primitives' |
| 8 | + |
| 9 | +interface FeedbackProps { |
| 10 | + page: string |
| 11 | +} |
| 12 | + |
| 13 | +export function Feedback({ page }: FeedbackProps) { |
| 14 | + const [client] = useState(() => new QueryClient()) |
| 15 | + return ( |
| 16 | + <QueryClientProvider client={client}> |
| 17 | + <FeedbackForm key={page} page={page} /> |
| 18 | + </QueryClientProvider> |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +function FeedbackForm({ page }: FeedbackProps) { |
| 23 | + const [helpful, setHelpful] = useState<boolean | null>(null) |
| 24 | + const [comment, setComment] = useState('') |
| 25 | + const pendingSubmission = useRef<{ |
| 26 | + submissionId: string |
| 27 | + helpful: boolean |
| 28 | + comment: string |
| 29 | + } | null>(null) |
| 30 | + const submission = useMutation({ |
| 31 | + retry: false, |
| 32 | + mutationFn: async (payload: { submissionId: string; helpful: boolean; comment: string }) => { |
| 33 | + const response = await fetch('/api/feedback', { |
| 34 | + method: 'POST', |
| 35 | + headers: { 'Content-Type': 'application/json' }, |
| 36 | + body: JSON.stringify({ ...payload, page }), |
| 37 | + signal: AbortSignal.timeout(20_000), |
| 38 | + }) |
| 39 | + if (!response.ok) |
| 40 | + throw new Error( |
| 41 | + response.status === 429 |
| 42 | + ? 'Too many submissions. Please try again in a minute.' |
| 43 | + : 'Could not send feedback. Please try again.' |
| 44 | + ) |
| 45 | + const result: unknown = await response.json() |
| 46 | + if ( |
| 47 | + !result || |
| 48 | + typeof result !== 'object' || |
| 49 | + !('accepted' in result) || |
| 50 | + result.accepted !== true |
| 51 | + ) { |
| 52 | + throw new Error('Could not confirm feedback. Please try again.') |
| 53 | + } |
| 54 | + }, |
| 55 | + }) |
| 56 | + function submit(vote: boolean) { |
| 57 | + if (submission.isPending) return |
| 58 | + const text = vote ? '' : comment.trim() |
| 59 | + if ( |
| 60 | + !pendingSubmission.current || |
| 61 | + pendingSubmission.current.helpful !== vote || |
| 62 | + pendingSubmission.current.comment !== text |
| 63 | + ) { |
| 64 | + pendingSubmission.current = { submissionId: generateId(), helpful: vote, comment: text } |
| 65 | + } |
| 66 | + submission.mutate(pendingSubmission.current) |
| 67 | + } |
| 68 | + if (submission.isSuccess) return <p role='status'>Thanks for your feedback.</p> |
| 69 | + return ( |
| 70 | + <form |
| 71 | + className='min-w-0 space-y-3' |
| 72 | + onSubmit={(event) => { |
| 73 | + event.preventDefault() |
| 74 | + if (helpful !== null && !submission.isPending) submit(helpful) |
| 75 | + }} |
| 76 | + > |
| 77 | + <div className='flex items-center gap-2'> |
| 78 | + <span>Was this helpful?</span> |
| 79 | + <DocsButton |
| 80 | + type='button' |
| 81 | + aria-label='Yes, this was helpful' |
| 82 | + aria-pressed={helpful === true} |
| 83 | + disabled={submission.isPending} |
| 84 | + iconOnly |
| 85 | + className='size-11 aria-pressed:bg-[var(--docs-selected)]' |
| 86 | + onClick={() => { |
| 87 | + setHelpful(true) |
| 88 | + submission.reset() |
| 89 | + submit(true) |
| 90 | + }} |
| 91 | + > |
| 92 | + <ThumbsUp className='size-4' /> |
| 93 | + </DocsButton> |
| 94 | + <DocsButton |
| 95 | + type='button' |
| 96 | + aria-label='No, this could be improved' |
| 97 | + aria-pressed={helpful === false} |
| 98 | + disabled={submission.isPending} |
| 99 | + iconOnly |
| 100 | + className='size-11 aria-pressed:bg-[var(--docs-selected)]' |
| 101 | + onClick={() => { |
| 102 | + setHelpful(false) |
| 103 | + submission.reset() |
| 104 | + }} |
| 105 | + > |
| 106 | + <ThumbsDown className='size-4' /> |
| 107 | + </DocsButton> |
| 108 | + </div> |
| 109 | + {helpful === false && ( |
| 110 | + <div className='space-y-2'> |
| 111 | + <label className='block' htmlFor='docs-feedback-comment'> |
| 112 | + What could be better? (optional) |
| 113 | + </label> |
| 114 | + <textarea |
| 115 | + id='docs-feedback-comment' |
| 116 | + aria-describedby='docs-feedback-privacy' |
| 117 | + value={comment} |
| 118 | + maxLength={2000} |
| 119 | + rows={3} |
| 120 | + disabled={submission.isPending} |
| 121 | + onChange={(event) => setComment(event.target.value)} |
| 122 | + className='w-full min-w-0 resize-y rounded-lg border border-[var(--docs-border)] bg-[var(--docs-canvas)] p-3 text-[var(--docs-heading)] text-docs-body focus-visible:outline-2 focus-visible:outline-[var(--docs-link)] focus-visible:outline-offset-2' |
| 123 | + placeholder='Tell us what was missing or unclear.' |
| 124 | + /> |
| 125 | + <p id='docs-feedback-privacy'>Please don’t include private information.</p> |
| 126 | + <DocsButton |
| 127 | + type='submit' |
| 128 | + disabled={submission.isPending} |
| 129 | + className='h-11 border border-[var(--docs-border)]' |
| 130 | + > |
| 131 | + {submission.isPending ? 'Sending…' : 'Send feedback'} |
| 132 | + </DocsButton> |
| 133 | + </div> |
| 134 | + )} |
| 135 | + {submission.isPending && helpful === true && <p role='status'>Sending…</p>} |
| 136 | + {submission.isError && <p role='alert'>{submission.error.message}</p>} |
| 137 | + </form> |
| 138 | + ) |
| 139 | +} |
0 commit comments