|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { type KeyboardEvent, type ReactNode, useEffect, useState } from "react"; |
| 4 | +import { Button } from "@usesend/ui/src/button"; |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogContent, |
| 8 | + DialogDescription, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | + DialogTrigger, |
| 13 | +} from "@usesend/ui/src/dialog"; |
| 14 | +import { |
| 15 | + Form, |
| 16 | + FormControl, |
| 17 | + FormField, |
| 18 | + FormItem, |
| 19 | + FormLabel, |
| 20 | + FormMessage, |
| 21 | +} from "@usesend/ui/src/form"; |
| 22 | +import { Textarea } from "@usesend/ui/src/textarea"; |
| 23 | +import { toast } from "@usesend/ui/src/toaster"; |
| 24 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 25 | +import { useForm } from "react-hook-form"; |
| 26 | +import { z } from "zod"; |
| 27 | + |
| 28 | +import { api } from "~/trpc/react"; |
| 29 | + |
| 30 | +const FeedbackSchema = z.object({ |
| 31 | + message: z.string().trim().min(1, "Feedback is required").max(2000), |
| 32 | +}); |
| 33 | + |
| 34 | +export function FeedbackDialog({ trigger }: { trigger?: ReactNode }) { |
| 35 | + const [open, setOpen] = useState(false); |
| 36 | + const [isMac, setIsMac] = useState(false); |
| 37 | + |
| 38 | + const form = useForm<z.infer<typeof FeedbackSchema>>({ |
| 39 | + resolver: zodResolver(FeedbackSchema), |
| 40 | + defaultValues: { |
| 41 | + message: "", |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + const feedbackMutation = api.feedback.send.useMutation({ |
| 46 | + onSuccess: () => { |
| 47 | + toast.success("Thanks for sharing your feedback!"); |
| 48 | + form.reset(); |
| 49 | + setOpen(false); |
| 50 | + }, |
| 51 | + onError: (error) => { |
| 52 | + toast.error(error.message); |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const messageValue = form.watch("message"); |
| 57 | + const trimmedMessage = messageValue?.trim() ?? ""; |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + const platform = navigator.userAgent || navigator.platform || "unknown"; |
| 61 | + setIsMac(/Mac|iPhone|iPod|iPad/i.test(platform)); |
| 62 | + }, []); |
| 63 | + |
| 64 | + function handleOpenChange(nextOpen: boolean) { |
| 65 | + setOpen(nextOpen); |
| 66 | + if (!nextOpen) { |
| 67 | + form.reset(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + function onSubmit(values: z.infer<typeof FeedbackSchema>) { |
| 72 | + feedbackMutation.mutate({ message: values.message.trim() }); |
| 73 | + } |
| 74 | + |
| 75 | + function handleKeyDown(event: KeyboardEvent<HTMLTextAreaElement>) { |
| 76 | + const isSubmitShortcut = |
| 77 | + (event.metaKey || event.ctrlKey) && event.key === "Enter"; |
| 78 | + |
| 79 | + if (feedbackMutation.isPending || !isSubmitShortcut) return; |
| 80 | + |
| 81 | + event.preventDefault(); |
| 82 | + form.handleSubmit(onSubmit)(); |
| 83 | + } |
| 84 | + |
| 85 | + return ( |
| 86 | + <Dialog open={open} onOpenChange={handleOpenChange}> |
| 87 | + <DialogTrigger asChild> |
| 88 | + {trigger ?? ( |
| 89 | + <Button variant="outline" size="sm"> |
| 90 | + Feedback |
| 91 | + </Button> |
| 92 | + )} |
| 93 | + </DialogTrigger> |
| 94 | + <DialogContent className="max-w-lg"> |
| 95 | + <DialogHeader> |
| 96 | + <DialogTitle>Send feedback</DialogTitle> |
| 97 | + <DialogDescription> |
| 98 | + Share any thoughts or issues. Your message goes straight to our |
| 99 | + founders. |
| 100 | + </DialogDescription> |
| 101 | + </DialogHeader> |
| 102 | + |
| 103 | + <Form {...form}> |
| 104 | + <form className="space-y-4" onSubmit={form.handleSubmit(onSubmit)}> |
| 105 | + <FormField |
| 106 | + control={form.control} |
| 107 | + name="message" |
| 108 | + render={({ field }) => ( |
| 109 | + <FormItem> |
| 110 | + <FormControl> |
| 111 | + <Textarea |
| 112 | + {...field} |
| 113 | + minLength={1} |
| 114 | + maxLength={2000} |
| 115 | + onKeyDown={handleKeyDown} |
| 116 | + placeholder="Tell us what's on your mind" |
| 117 | + className="min-h-[160px]" |
| 118 | + /> |
| 119 | + </FormControl> |
| 120 | + <FormMessage /> |
| 121 | + </FormItem> |
| 122 | + )} |
| 123 | + /> |
| 124 | + |
| 125 | + <DialogFooter> |
| 126 | + <Button |
| 127 | + type="button" |
| 128 | + variant="outline" |
| 129 | + onClick={() => handleOpenChange(false)} |
| 130 | + disabled={feedbackMutation.isPending} |
| 131 | + > |
| 132 | + Cancel |
| 133 | + </Button> |
| 134 | + <Button |
| 135 | + type="submit" |
| 136 | + disabled={!trimmedMessage || feedbackMutation.isPending} |
| 137 | + > |
| 138 | + {feedbackMutation.isPending ? "Sending..." : "Send feedback"} |
| 139 | + {!feedbackMutation.isPending ? ( |
| 140 | + <> |
| 141 | + <span |
| 142 | + className="ml-2 inline-flex items-center gap-1 text-xs opacity-85" |
| 143 | + aria-hidden |
| 144 | + > |
| 145 | + <kbd className="inline-flex items-center justify-center rounded border border-input bg-muted/20 px-1 py-0.5 h-5 min-w-5 font-sans leading-none h-5 uppercase"> |
| 146 | + {isMac ? "⌘" : "^"} |
| 147 | + </kbd> |
| 148 | + <kbd className="inline-flex items-center justify-center rounded border border-input bg-muted/20 px-1 py-0.5 pt-1 h-5 min-w-5 leading-none font-sans h-5 uppercase"> |
| 149 | + ↵ |
| 150 | + </kbd> |
| 151 | + </span> |
| 152 | + <span className="sr-only"> |
| 153 | + {isMac ? "Command" : "Control"} plus Enter |
| 154 | + </span> |
| 155 | + </> |
| 156 | + ) : null} |
| 157 | + </Button> |
| 158 | + </DialogFooter> |
| 159 | + </form> |
| 160 | + </Form> |
| 161 | + </DialogContent> |
| 162 | + </Dialog> |
| 163 | + ); |
| 164 | +} |
0 commit comments