|
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | +import { AnimatePresence, motion } from "framer-motion"; |
| 3 | +import { cn } from "@/lib/cn"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Formspree form ID — the path segment from formspree.io/f/<this-bit>. |
| 7 | + * Maintained by the goodbird owner; rotate by creating a new form at |
| 8 | + * formspree.io and pasting its ID here. |
| 9 | + */ |
| 10 | +const FORMSPREE_FORM_ID = "xojyzwzn"; |
| 11 | + |
| 12 | +interface Props { |
| 13 | + open: boolean; |
| 14 | + onClose: () => void; |
| 15 | +} |
| 16 | + |
| 17 | +type SubmitState = |
| 18 | + | { kind: "idle" } |
| 19 | + | { kind: "submitting" } |
| 20 | + | { kind: "ok" } |
| 21 | + | { kind: "error"; message: string }; |
| 22 | + |
| 23 | +export function FeedbackDialog({ open, onClose }: Props) { |
| 24 | + const [state, setState] = useState<SubmitState>({ kind: "idle" }); |
| 25 | + const formRef = useRef<HTMLFormElement>(null); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if (!open) return; |
| 29 | + // eslint-disable-next-line react-hooks/set-state-in-effect |
| 30 | + setState({ kind: "idle" }); |
| 31 | + const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; |
| 32 | + window.addEventListener("keydown", onKey); |
| 33 | + const prev = document.body.style.overflow; |
| 34 | + document.body.style.overflow = "hidden"; |
| 35 | + return () => { |
| 36 | + window.removeEventListener("keydown", onKey); |
| 37 | + document.body.style.overflow = prev; |
| 38 | + }; |
| 39 | + }, [open, onClose]); |
| 40 | + |
| 41 | + const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
| 42 | + e.preventDefault(); |
| 43 | + if (!FORMSPREE_FORM_ID) return; |
| 44 | + setState({ kind: "submitting" }); |
| 45 | + const form = e.currentTarget; |
| 46 | + const data = new FormData(form); |
| 47 | + // Auto-attach diagnostic info so bug reports include something useful. |
| 48 | + data.set("_subject", `goodbird feedback (v${__APP_VERSION__})`); |
| 49 | + data.set("app_version", __APP_VERSION__); |
| 50 | + data.set("user_agent", navigator.userAgent); |
| 51 | + data.set("page", window.location.pathname); |
| 52 | + try { |
| 53 | + const res = await fetch(`https://formspree.io/f/${FORMSPREE_FORM_ID}`, { |
| 54 | + method: "POST", |
| 55 | + body: data, |
| 56 | + headers: { Accept: "application/json" }, |
| 57 | + }); |
| 58 | + if (res.ok) { |
| 59 | + setState({ kind: "ok" }); |
| 60 | + } else { |
| 61 | + const body = await res.json().catch(() => ({})); |
| 62 | + setState({ kind: "error", message: body?.error || "Couldn't send. Try again?" }); |
| 63 | + } |
| 64 | + } catch { |
| 65 | + setState({ kind: "error", message: "Couldn't send. Check your connection?" }); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <AnimatePresence> |
| 71 | + {open && ( |
| 72 | + <> |
| 73 | + <motion.div |
| 74 | + initial={{ opacity: 0 }} |
| 75 | + animate={{ opacity: 1 }} |
| 76 | + exit={{ opacity: 0 }} |
| 77 | + transition={{ duration: 0.18 }} |
| 78 | + onClick={onClose} |
| 79 | + className="fixed inset-0 z-[60] bg-(--color-ink)/45 backdrop-blur-[2px]" |
| 80 | + aria-hidden |
| 81 | + /> |
| 82 | + <motion.div |
| 83 | + role="dialog" |
| 84 | + aria-label="Send feedback" |
| 85 | + initial={{ opacity: 0, y: 24 }} |
| 86 | + animate={{ opacity: 1, y: 0 }} |
| 87 | + exit={{ opacity: 0, y: 24 }} |
| 88 | + transition={{ duration: 0.22, ease: "easeOut" }} |
| 89 | + className="fixed inset-x-0 bottom-0 z-[61] mx-auto max-h-[88dvh] max-w-md overflow-y-auto overscroll-contain rounded-t-3xl bg-(--color-surface) px-6 pb-8 pt-6 shadow-(--shadow-pop)" |
| 90 | + > |
| 91 | + <div className="flex items-start justify-between gap-3"> |
| 92 | + <div> |
| 93 | + <p className="font-mono text-[10px] font-medium uppercase tracking-[0.22em] text-(--color-ink-soft)"> |
| 94 | + Field report |
| 95 | + </p> |
| 96 | + <h2 className="mt-1 font-display text-3xl">Send feedback</h2> |
| 97 | + </div> |
| 98 | + <button |
| 99 | + onClick={onClose} |
| 100 | + aria-label="Close" |
| 101 | + className="grid h-9 w-9 shrink-0 place-items-center rounded-full text-(--color-ink-soft) hover:bg-(--color-line) cursor-pointer" |
| 102 | + > |
| 103 | + <svg viewBox="0 0 24 24" className="h-4 w-4" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round"> |
| 104 | + <path d="M6 6l12 12M18 6L6 18" /> |
| 105 | + </svg> |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + |
| 109 | + {!FORMSPREE_FORM_ID ? ( |
| 110 | + <p className="mt-6 rounded-2xl border-2 border-(--color-line) bg-(--color-bg) px-4 py-3 text-sm leading-snug text-(--color-ink-soft)"> |
| 111 | + Feedback isn't set up yet. The maintainer needs to paste a |
| 112 | + Formspree form ID into{" "} |
| 113 | + <code className="font-mono text-xs text-(--color-ink)"> |
| 114 | + src/components/FeedbackDialog.tsx |
| 115 | + </code> |
| 116 | + . |
| 117 | + </p> |
| 118 | + ) : state.kind === "ok" ? ( |
| 119 | + <div role="status" aria-live="polite" className="mt-6 space-y-3 text-center"> |
| 120 | + <p className="font-display text-xl text-(--color-moss-700)">Got it. Thanks.</p> |
| 121 | + <p className="text-sm text-(--color-ink-soft)"> |
| 122 | + Your note went through. The maintainer reads them all. |
| 123 | + </p> |
| 124 | + <button |
| 125 | + onClick={onClose} |
| 126 | + className="tap-target mt-3 w-full rounded-full bg-(--color-moss-500) px-6 py-3 font-semibold text-white shadow-(--shadow-pop) hover:bg-(--color-moss-600) cursor-pointer" |
| 127 | + > |
| 128 | + Close |
| 129 | + </button> |
| 130 | + </div> |
| 131 | + ) : ( |
| 132 | + <form ref={formRef} onSubmit={handleSubmit} className="mt-6 space-y-3"> |
| 133 | + <p className="text-sm leading-snug text-(--color-ink-soft)"> |
| 134 | + What broke, what's confusing, what could be better. App version |
| 135 | + and the page you're on are auto-attached so you don't have to |
| 136 | + describe them. |
| 137 | + </p> |
| 138 | + <label className="block"> |
| 139 | + <span className="font-mono text-[10px] font-medium uppercase tracking-[0.2em] text-(--color-ink-soft)"> |
| 140 | + Email (optional) |
| 141 | + </span> |
| 142 | + <input |
| 143 | + type="email" |
| 144 | + name="email" |
| 145 | + autoComplete="email" |
| 146 | + placeholder="so we can reply" |
| 147 | + className="mt-1 w-full rounded-2xl border-2 border-(--color-line) bg-(--color-surface) px-4 py-2.5 text-sm focus:border-(--color-moss-500) focus:outline-none" |
| 148 | + /> |
| 149 | + </label> |
| 150 | + <label className="block"> |
| 151 | + <span className="font-mono text-[10px] font-medium uppercase tracking-[0.2em] text-(--color-ink-soft)"> |
| 152 | + Message |
| 153 | + </span> |
| 154 | + <textarea |
| 155 | + name="message" |
| 156 | + required |
| 157 | + rows={5} |
| 158 | + placeholder="Type away." |
| 159 | + className="mt-1 w-full rounded-2xl border-2 border-(--color-line) bg-(--color-surface) px-4 py-2.5 text-sm focus:border-(--color-moss-500) focus:outline-none" |
| 160 | + /> |
| 161 | + </label> |
| 162 | + {state.kind === "error" && ( |
| 163 | + <p role="alert" className="text-sm text-(--color-wrong)">{state.message}</p> |
| 164 | + )} |
| 165 | + <div className="flex flex-col-reverse gap-2 pt-2 sm:flex-row sm:justify-end"> |
| 166 | + <button |
| 167 | + type="button" |
| 168 | + onClick={onClose} |
| 169 | + className="tap-target rounded-full border-2 border-(--color-line) px-5 py-3 font-semibold text-(--color-ink-soft) hover:border-(--color-ink-soft) cursor-pointer" |
| 170 | + > |
| 171 | + Cancel |
| 172 | + </button> |
| 173 | + <button |
| 174 | + type="submit" |
| 175 | + disabled={state.kind === "submitting"} |
| 176 | + className={cn( |
| 177 | + "tap-target rounded-full bg-(--color-moss-500) px-6 py-3 font-semibold text-white shadow-(--shadow-pop) transition hover:bg-(--color-moss-600) cursor-pointer", |
| 178 | + state.kind === "submitting" && "opacity-70 cursor-default", |
| 179 | + )} |
| 180 | + > |
| 181 | + {state.kind === "submitting" ? "Sending…" : "Send"} |
| 182 | + </button> |
| 183 | + </div> |
| 184 | + </form> |
| 185 | + )} |
| 186 | + </motion.div> |
| 187 | + </> |
| 188 | + )} |
| 189 | + </AnimatePresence> |
| 190 | + ); |
| 191 | +} |
0 commit comments