|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | +import { |
| 5 | + Form, |
| 6 | + FormControl, |
| 7 | + FormField, |
| 8 | + FormItem, |
| 9 | + FormLabel, |
| 10 | +} from "@openstatus/ui/components/ui/form"; |
| 11 | +import { Kbd } from "@openstatus/ui/components/ui/kbd"; |
| 12 | +import { |
| 13 | + Popover, |
| 14 | + PopoverContent, |
| 15 | + PopoverTrigger, |
| 16 | +} from "@openstatus/ui/components/ui/popover"; |
| 17 | +import { Textarea } from "@openstatus/ui/components/ui/textarea"; |
| 18 | +import { Inbox, LoaderCircle } from "lucide-react"; |
| 19 | +import { usePathname } from "next/navigation"; |
| 20 | +import { useEffect, useState } from "react"; |
| 21 | +import { useForm } from "react-hook-form"; |
| 22 | +import { z } from "zod"; |
| 23 | + |
| 24 | +import { toastAction } from "@/lib/toast"; |
| 25 | + |
| 26 | +type Rating = "up" | "down"; |
| 27 | + |
| 28 | +const ratingKey = (path: string) => `docs-rating:${path}`; |
| 29 | + |
| 30 | +const schema = z.object({ |
| 31 | + message: z.string().trim().min(1).max(2000), |
| 32 | +}); |
| 33 | + |
| 34 | +type FeedbackBody = |
| 35 | + | { kind: "rating"; path: string; rating: Rating; previous?: Rating } |
| 36 | + | { kind: "message"; path: string; message: string }; |
| 37 | + |
| 38 | +// never throws; returns whether the server accepted the submission |
| 39 | +async function postFeedback(body: FeedbackBody): Promise<boolean> { |
| 40 | + try { |
| 41 | + const res = await fetch("/api/feedback/docs", { |
| 42 | + method: "POST", |
| 43 | + headers: { "Content-Type": "application/json" }, |
| 44 | + body: JSON.stringify(body), |
| 45 | + }); |
| 46 | + return res.ok; |
| 47 | + } catch { |
| 48 | + return false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function DocsFeedbackBar({ path }: { path: string }) { |
| 53 | + const [rating, setRating] = useState<Rating>(); |
| 54 | + const [open, setOpen] = useState(false); |
| 55 | + const [sent, setSent] = useState(false); |
| 56 | + const form = useForm<z.infer<typeof schema>>({ |
| 57 | + resolver: zodResolver(schema), |
| 58 | + defaultValues: { message: "" }, |
| 59 | + }); |
| 60 | + |
| 61 | + // rehydrate a prior vote so the chosen arrow stays highlighted across loads |
| 62 | + useEffect(() => { |
| 63 | + const stored = window.localStorage.getItem(ratingKey(path)); |
| 64 | + if (stored === "up" || stored === "down") setRating(stored); |
| 65 | + }, [path]); |
| 66 | + |
| 67 | + // reset the popover contents shortly after it closes (300ms = close anim) |
| 68 | + useEffect(() => { |
| 69 | + if (!open && sent) { |
| 70 | + const t = setTimeout(() => { |
| 71 | + setSent(false); |
| 72 | + form.reset(); |
| 73 | + }, 300); |
| 74 | + return () => clearTimeout(t); |
| 75 | + } |
| 76 | + }, [open, sent, form]); |
| 77 | + |
| 78 | + function rate(value: Rating) { |
| 79 | + // read the prior vote from localStorage (not state) so a click before the |
| 80 | + // hydration effect still reports the correct `previous` to the server |
| 81 | + const stored = window.localStorage.getItem(ratingKey(path)); |
| 82 | + const previous = stored === "up" || stored === "down" ? stored : undefined; |
| 83 | + if (previous === value) return; |
| 84 | + setRating(value); |
| 85 | + window.localStorage.setItem(ratingKey(path), value); |
| 86 | + void postFeedback({ kind: "rating", path, rating: value, previous }); |
| 87 | + } |
| 88 | + |
| 89 | + async function onSubmit(values: z.infer<typeof schema>) { |
| 90 | + const ok = await postFeedback({ |
| 91 | + kind: "message", |
| 92 | + path, |
| 93 | + message: values.message, |
| 94 | + }); |
| 95 | + if (ok) { |
| 96 | + setSent(true); |
| 97 | + } else { |
| 98 | + toastAction("error"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return ( |
| 103 | + <div className="text-sm"> |
| 104 | + <p className="text-foreground py-2 font-mono font-medium"> |
| 105 | + Was this helpful? |
| 106 | + </p> |
| 107 | + <Popover open={open} onOpenChange={setOpen}> |
| 108 | + <div className="bg-border text-muted-foreground [&>*]:bg-background [&>*]:hover:bg-muted flex items-stretch gap-px border font-mono [&>*]:flex [&>*]:items-center [&>*]:justify-center [&>*]:py-2 [&>*]:transition-colors [&>*]:disabled:pointer-events-none"> |
| 109 | + <button |
| 110 | + type="button" |
| 111 | + aria-label="Yes, this page was helpful" |
| 112 | + data-active={rating === "up"} |
| 113 | + onClick={() => rate("up")} |
| 114 | + className="hover:text-success data-[active=true]:bg-muted data-[active=true]:text-success w-10" |
| 115 | + > |
| 116 | + <span className="leading-none" aria-hidden="true"> |
| 117 | + ▲ |
| 118 | + </span> |
| 119 | + </button> |
| 120 | + <button |
| 121 | + type="button" |
| 122 | + aria-label="No, this page was not helpful" |
| 123 | + data-active={rating === "down"} |
| 124 | + onClick={() => rate("down")} |
| 125 | + className="hover:text-destructive data-[active=true]:bg-muted data-[active=true]:text-destructive w-10" |
| 126 | + > |
| 127 | + <span className="leading-none" aria-hidden="true"> |
| 128 | + ▼ |
| 129 | + </span> |
| 130 | + </button> |
| 131 | + <PopoverTrigger asChild> |
| 132 | + <button |
| 133 | + type="button" |
| 134 | + data-active={open} |
| 135 | + className="hover:text-foreground data-[active=true]:bg-muted data-[active=true]:text-foreground flex-1" |
| 136 | + > |
| 137 | + Send feedback |
| 138 | + </button> |
| 139 | + </PopoverTrigger> |
| 140 | + </div> |
| 141 | + <PopoverContent |
| 142 | + align="end" |
| 143 | + side="bottom" |
| 144 | + className="w-64 rounded-none p-0" |
| 145 | + > |
| 146 | + {sent ? ( |
| 147 | + <div className="flex flex-col items-center justify-center gap-1 p-3"> |
| 148 | + <Inbox className="size-4 shrink-0" /> |
| 149 | + <p className="text-center text-sm font-medium"> |
| 150 | + Thanks for sharing! |
| 151 | + </p> |
| 152 | + <p className="text-muted-foreground text-center text-xs"> |
| 153 | + We read every note. |
| 154 | + </p> |
| 155 | + </div> |
| 156 | + ) : ( |
| 157 | + <Form {...form}> |
| 158 | + <form className="relative" onSubmit={form.handleSubmit(onSubmit)}> |
| 159 | + <FormField |
| 160 | + control={form.control} |
| 161 | + name="message" |
| 162 | + render={({ field }) => ( |
| 163 | + <FormItem> |
| 164 | + <FormLabel className="sr-only">Feedback</FormLabel> |
| 165 | + <FormControl> |
| 166 | + <Textarea |
| 167 | + placeholder="Ideas, corrections, or anything missing..." |
| 168 | + className="field-sizing-fixed h-[110px] resize-none rounded-none p-3" |
| 169 | + rows={4} |
| 170 | + onKeyDown={(e) => { |
| 171 | + if ((e.metaKey || e.ctrlKey) && e.key === "Enter") { |
| 172 | + e.preventDefault(); |
| 173 | + void form.handleSubmit(onSubmit)(); |
| 174 | + } |
| 175 | + }} |
| 176 | + {...field} |
| 177 | + /> |
| 178 | + </FormControl> |
| 179 | + </FormItem> |
| 180 | + )} |
| 181 | + /> |
| 182 | + <button |
| 183 | + type="submit" |
| 184 | + className="text-muted-foreground hover:text-foreground absolute right-2 bottom-2 flex items-center font-mono text-sm disabled:opacity-50" |
| 185 | + disabled={form.formState.isSubmitting} |
| 186 | + > |
| 187 | + {form.formState.isSubmitting ? ( |
| 188 | + <LoaderCircle className="size-4 animate-spin" /> |
| 189 | + ) : ( |
| 190 | + <> |
| 191 | + Send |
| 192 | + <Kbd className="ml-1 font-mono">⌘</Kbd> |
| 193 | + <Kbd className="ml-1 font-mono">↵</Kbd> |
| 194 | + </> |
| 195 | + )} |
| 196 | + </button> |
| 197 | + </form> |
| 198 | + </Form> |
| 199 | + )} |
| 200 | + </PopoverContent> |
| 201 | + </Popover> |
| 202 | + </div> |
| 203 | + ); |
| 204 | +} |
| 205 | + |
| 206 | +export function DocsFeedback() { |
| 207 | + const pathname = usePathname(); |
| 208 | + // key by path so rating + popover state reset when navigating between docs |
| 209 | + return <DocsFeedbackBar key={pathname} path={pathname} />; |
| 210 | +} |
0 commit comments