|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { Button } from './ui/button' |
| 5 | +import { sendFeedback } from '@/actions/sendFeedback' |
| 6 | +import { RadioGroup } from './ui/radio-group' |
| 7 | +import { Label } from './ui/label' |
| 8 | +import { RadioGroupItem } from '@radix-ui/react-radio-group' |
| 9 | +import { useFormState } from 'react-dom' |
| 10 | +import { CheckIcon } from './icons/CheckIcon' |
| 11 | +import { usePathname } from 'next/navigation' |
| 12 | + |
| 13 | +const choices = [ |
| 14 | + { |
| 15 | + value: 'yes', |
| 16 | + label: 'It was helpful', |
| 17 | + emoji: '🤩', |
| 18 | + }, |
| 19 | + { |
| 20 | + value: 'no', |
| 21 | + label: 'It was not helpful', |
| 22 | + emoji: '😓', |
| 23 | + }, |
| 24 | + { |
| 25 | + value: 'feedback', |
| 26 | + label: 'I have feedback', |
| 27 | + emoji: '📣', |
| 28 | + }, |
| 29 | +] |
| 30 | + |
| 31 | +const initialState = { |
| 32 | + message: '', |
| 33 | +} |
| 34 | + |
| 35 | +const FeedbackForm = () => { |
| 36 | + const [state, formAction] = useFormState(sendFeedback, initialState) |
| 37 | + const [selected, setSelected] = useState(false) |
| 38 | + const pathname = usePathname() |
| 39 | + |
| 40 | + return state.message ? ( |
| 41 | + <div className="flex items-center gap-x-2 text-sm"> |
| 42 | + <CheckIcon className="h-5 w-5 flex-none fill-green-500 stroke-white dark:fill-green-200/20 dark:stroke-green-200" /> |
| 43 | + Thank you for your feedback! 🙌 |
| 44 | + </div> |
| 45 | + ) : ( |
| 46 | + <form |
| 47 | + action={formAction} |
| 48 | + className="flex flex-col items-start justify-start gap-6" |
| 49 | + > |
| 50 | + <input |
| 51 | + name="ua" |
| 52 | + value={navigator.userAgent} |
| 53 | + className="hidden" |
| 54 | + readOnly |
| 55 | + /> |
| 56 | + <input |
| 57 | + name="url" |
| 58 | + value={`/docs${pathname}`} |
| 59 | + className="hidden" |
| 60 | + readOnly |
| 61 | + /> |
| 62 | + <Label htmlFor="choice" className="text-sm text-zinc-900 dark:text-white"> |
| 63 | + What did you think of this content? |
| 64 | + </Label> |
| 65 | + <RadioGroup |
| 66 | + className="flex flex-col gap-2 md:flex-row" |
| 67 | + onChange={() => { |
| 68 | + if (!selected) setSelected(true) |
| 69 | + }} |
| 70 | + name="choice" |
| 71 | + id="choice" |
| 72 | + > |
| 73 | + {choices.map(({ label, value, emoji }) => ( |
| 74 | + <div key={value} className="group flex items-center"> |
| 75 | + <RadioGroupItem value={value} id={value} className="group"> |
| 76 | + <span className="zincscale group-checked:zincscale-0 group-data-[state=checked]:zincscale-0 mr-2"> |
| 77 | + {emoji} |
| 78 | + </span> |
| 79 | + </RadioGroupItem> |
| 80 | + <Label htmlFor={value} className="cursor-pointer text-xs"> |
| 81 | + {label} |
| 82 | + </Label> |
| 83 | + </div> |
| 84 | + ))} |
| 85 | + </RadioGroup> |
| 86 | + {selected && ( |
| 87 | + <div className="flex w-full max-w-[400px] flex-col gap-4 rounded-lg p-2 text-zinc-700 shadow-md ring-1 ring-zinc-300 dark:bg-white/2.5 dark:text-zinc-100 dark:ring-white/10 dark:hover:shadow-black/5"> |
| 88 | + <label htmlFor="comment" className="sr-only"> |
| 89 | + Comment |
| 90 | + </label> |
| 91 | + <div> |
| 92 | + <textarea |
| 93 | + rows={5} |
| 94 | + name="comment" |
| 95 | + id="comment" |
| 96 | + className="block w-full rounded-md border-0 bg-white p-2 py-1.5 text-zinc-900 shadow-sm ring-1 ring-inset ring-zinc-300 placeholder:text-zinc-400 focus:ring-2 focus:ring-inset focus:ring-primary-600 dark:bg-zinc-700/70 dark:text-zinc-50 dark:ring-zinc-600 sm:text-sm sm:leading-6" |
| 97 | + placeholder="We'd love to hear your feedback!" |
| 98 | + defaultValue={''} |
| 99 | + autoFocus |
| 100 | + /> |
| 101 | + </div> |
| 102 | + <div className="mt-2 flex justify-end"> |
| 103 | + <Button type="submit">Send</Button> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + )} |
| 107 | + </form> |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +export default FeedbackForm |
0 commit comments