|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { MessageSquarePlus } from "lucide-react"; |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogContent, |
| 8 | + DialogDescription, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | +} from "@repowise-dev/ui/ui/dialog"; |
| 13 | +import { Button } from "@repowise-dev/ui/ui/button"; |
| 14 | +import { toast } from "sonner"; |
| 15 | +import { submitFeedback, type FeedbackCategory } from "@/lib/api/feedback"; |
| 16 | + |
| 17 | +const CATEGORIES: { value: FeedbackCategory; label: string }[] = [ |
| 18 | + { value: "ui_ux", label: "UI / UX" }, |
| 19 | + { value: "bug", label: "Bug" }, |
| 20 | + { value: "feature_request", label: "Feature request" }, |
| 21 | + { value: "other", label: "Other" }, |
| 22 | +]; |
| 23 | + |
| 24 | +const MAX_LENGTH = 4000; |
| 25 | + |
| 26 | +/** |
| 27 | + * Sidebar-footer feedback entry point for the self-hosted dashboard. Opens a |
| 28 | + * categorised dialog; submissions POST to the local server's `/api/feedback`, |
| 29 | + * which forwards them to the Repowise maintainers. Works without an account. |
| 30 | + */ |
| 31 | +export function FeedbackButton() { |
| 32 | + const [open, setOpen] = useState(false); |
| 33 | + const [category, setCategory] = useState<FeedbackCategory>("ui_ux"); |
| 34 | + const [message, setMessage] = useState(""); |
| 35 | + const [email, setEmail] = useState(""); |
| 36 | + const [submitting, setSubmitting] = useState(false); |
| 37 | + |
| 38 | + const reset = () => { |
| 39 | + setCategory("ui_ux"); |
| 40 | + setMessage(""); |
| 41 | + setEmail(""); |
| 42 | + }; |
| 43 | + |
| 44 | + const handleOpenChange = (next: boolean) => { |
| 45 | + if (submitting) return; |
| 46 | + setOpen(next); |
| 47 | + if (!next) reset(); |
| 48 | + }; |
| 49 | + |
| 50 | + const handleSubmit = async () => { |
| 51 | + const trimmed = message.trim(); |
| 52 | + if (!trimmed) { |
| 53 | + toast.error("Please enter your feedback."); |
| 54 | + return; |
| 55 | + } |
| 56 | + setSubmitting(true); |
| 57 | + try { |
| 58 | + await submitFeedback({ |
| 59 | + category, |
| 60 | + message: trimmed, |
| 61 | + ...(email.trim() && { email: email.trim() }), |
| 62 | + ...(typeof window !== "undefined" && { pageUrl: window.location.href }), |
| 63 | + }); |
| 64 | + toast.success("Thanks for the feedback!"); |
| 65 | + setOpen(false); |
| 66 | + reset(); |
| 67 | + } catch { |
| 68 | + toast.error("Couldn't send feedback. Please try again."); |
| 69 | + } finally { |
| 70 | + setSubmitting(false); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + <button |
| 77 | + type="button" |
| 78 | + onClick={() => setOpen(true)} |
| 79 | + aria-label="Send feedback" |
| 80 | + className="flex w-full items-center gap-1.5 text-xs text-[var(--color-text-tertiary)] transition-colors hover:text-[var(--color-text-secondary)]" |
| 81 | + > |
| 82 | + <MessageSquarePlus className="h-3.5 w-3.5 shrink-0" /> |
| 83 | + <span>Send feedback</span> |
| 84 | + </button> |
| 85 | + |
| 86 | + <Dialog open={open} onOpenChange={handleOpenChange}> |
| 87 | + <DialogContent className="max-w-md"> |
| 88 | + <DialogHeader> |
| 89 | + <DialogTitle>Send feedback</DialogTitle> |
| 90 | + <DialogDescription> |
| 91 | + Found a bug or have an idea? It goes straight to the maintainers. We read every |
| 92 | + message. |
| 93 | + </DialogDescription> |
| 94 | + </DialogHeader> |
| 95 | + |
| 96 | + <div className="space-y-4"> |
| 97 | + {/* Category */} |
| 98 | + <div> |
| 99 | + <label className="mb-1.5 block text-xs font-medium text-[var(--color-text-tertiary)]"> |
| 100 | + Category |
| 101 | + </label> |
| 102 | + <div className="flex flex-wrap gap-2"> |
| 103 | + {CATEGORIES.map((option) => ( |
| 104 | + <button |
| 105 | + key={option.value} |
| 106 | + type="button" |
| 107 | + onClick={() => setCategory(option.value)} |
| 108 | + className={`cursor-pointer rounded-full border px-3 py-1.5 text-xs font-medium transition-colors ${ |
| 109 | + category === option.value |
| 110 | + ? "border-[var(--color-accent-primary)] bg-[var(--color-accent-muted)] text-[var(--color-accent-primary)]" |
| 111 | + : "border-[var(--color-border-default)] text-[var(--color-text-secondary)] hover:border-[var(--color-text-tertiary)]" |
| 112 | + }`} |
| 113 | + > |
| 114 | + {option.label} |
| 115 | + </button> |
| 116 | + ))} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + |
| 120 | + {/* Message */} |
| 121 | + <div> |
| 122 | + <label |
| 123 | + htmlFor="feedback-message" |
| 124 | + className="mb-1.5 block text-xs font-medium text-[var(--color-text-tertiary)]" |
| 125 | + > |
| 126 | + Your feedback |
| 127 | + </label> |
| 128 | + <textarea |
| 129 | + id="feedback-message" |
| 130 | + rows={5} |
| 131 | + autoFocus |
| 132 | + maxLength={MAX_LENGTH} |
| 133 | + value={message} |
| 134 | + onChange={(e) => setMessage(e.target.value)} |
| 135 | + placeholder="Tell us what's working, what's broken, or what you'd love to see..." |
| 136 | + className="w-full resize-none rounded-lg border border-[var(--color-border-default)] bg-[var(--color-bg-surface)] px-3 py-2.5 text-sm text-[var(--color-text-primary)] placeholder:text-[var(--color-text-tertiary)] focus:border-[var(--color-accent-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-accent-primary)]" |
| 137 | + /> |
| 138 | + <p className="mt-1 text-right text-[11px] text-[var(--color-text-tertiary)]"> |
| 139 | + {message.length}/{MAX_LENGTH} |
| 140 | + </p> |
| 141 | + </div> |
| 142 | + |
| 143 | + {/* Optional email — so the maintainers can reply */} |
| 144 | + <div> |
| 145 | + <label |
| 146 | + htmlFor="feedback-email" |
| 147 | + className="mb-1.5 block text-xs font-medium text-[var(--color-text-tertiary)]" |
| 148 | + > |
| 149 | + Email{" "} |
| 150 | + <span className="font-normal text-[var(--color-text-tertiary)]"> |
| 151 | + (optional, if you'd like a reply) |
| 152 | + </span> |
| 153 | + </label> |
| 154 | + <input |
| 155 | + id="feedback-email" |
| 156 | + type="email" |
| 157 | + value={email} |
| 158 | + onChange={(e) => setEmail(e.target.value)} |
| 159 | + placeholder="you@example.com" |
| 160 | + className="w-full rounded-lg border border-[var(--color-border-default)] bg-[var(--color-bg-surface)] px-3 py-2 text-sm text-[var(--color-text-primary)] placeholder:text-[var(--color-text-tertiary)] focus:border-[var(--color-accent-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-accent-primary)]" |
| 161 | + /> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + |
| 165 | + <DialogFooter> |
| 166 | + <Button variant="outline" onClick={() => handleOpenChange(false)} disabled={submitting}> |
| 167 | + Cancel |
| 168 | + </Button> |
| 169 | + <Button onClick={handleSubmit} disabled={submitting || !message.trim()}> |
| 170 | + {submitting ? "Sending…" : "Send feedback"} |
| 171 | + </Button> |
| 172 | + </DialogFooter> |
| 173 | + </DialogContent> |
| 174 | + </Dialog> |
| 175 | + </> |
| 176 | + ); |
| 177 | +} |
0 commit comments