Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | 'use client'; |
| 2 | |
| 3 | import { useState, useRef, useEffect } from 'react'; |
| 4 | import { PenLine, X, Loader2 } from 'lucide-react'; |
| 5 | |
| 6 | type Sentiment = 'positive' | 'neutral' | 'negative' | null; |
| 7 | |
| 8 | interface FeedbackProps { |
| 9 | page: string; |
| 10 | } |
| 11 | |
| 12 | export function Feedback({ page }: FeedbackProps) { |
| 13 | const [isOpen, setIsOpen] = useState(false); |
| 14 | const [sentiment, setSentiment] = useState<Sentiment>(null); |
| 15 | const [message, setMessage] = useState(''); |
| 16 | const [email, setEmail] = useState(''); |
| 17 | const [state, setState] = useState<'idle' | 'loading' | 'success' | 'error'>('idle'); |
| 18 | const closeTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 19 | |
| 20 | useEffect(() => { |
| 21 | return () => { |
| 22 | if (closeTimeoutRef.current) { |
| 23 | clearTimeout(closeTimeoutRef.current); |
| 24 | } |
| 25 | }; |
| 26 | }, []); |
| 27 | |
| 28 | const handleSubmit = async (e: React.FormEvent) => { |
| 29 | e.preventDefault(); |
| 30 | if (!message.trim()) return; |
| 31 | |
| 32 | setState('loading'); |
| 33 | |
| 34 | try { |
| 35 | const response = await fetch('/api/feedback', { |
| 36 | method: 'POST', |
| 37 | headers: { 'Content-Type': 'application/json' }, |
| 38 | body: JSON.stringify({ |
| 39 | page, |
| 40 | pageTitle: document.title, |
| 41 | sentiment, |
| 42 | message: message.trim(), |
| 43 | email: email.trim() || undefined, |
| 44 | userAgent: navigator.userAgent, |
| 45 | referrer: document.referrer || undefined, |
| 46 | viewport: `${window.innerWidth}x${window.innerHeight}`, |
| 47 | timestamp: new Date().toISOString(), |
| 48 | }), |
| 49 | }); |
| 50 | |
| 51 | if (!response.ok) throw new Error('Failed to send'); |
| 52 | |
| 53 | setState('success'); |
| 54 | closeTimeoutRef.current = setTimeout(() => { |
| 55 | setIsOpen(false); |
| 56 | setState('idle'); |
| 57 | setSentiment(null); |
| 58 | setMessage(''); |
| 59 | setEmail(''); |
| 60 | }, 2000); |
| 61 | } catch { |
| 62 | setState('error'); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | const handleClose = () => { |
| 67 | if (closeTimeoutRef.current) { |
| 68 | clearTimeout(closeTimeoutRef.current); |
| 69 | closeTimeoutRef.current = null; |
| 70 | } |
| 71 | setIsOpen(false); |
| 72 | setState('idle'); |
| 73 | setSentiment(null); |
| 74 | setMessage(''); |
| 75 | setEmail(''); |
| 76 | }; |
| 77 | |
| 78 | return ( |
| 79 | <> |
| 80 | <button |
| 81 | type="button" |
| 82 | onClick={() => setIsOpen(true)} |
| 83 | className="inline-flex items-center gap-1.5 px-3 py-2 sm:px-2.5 sm:py-1.5 text-xs font-medium |
| 84 | text-fd-muted-foreground hover:text-fd-foreground |
| 85 | bg-fd-secondary/50 hover:bg-fd-secondary |
| 86 | rounded-md |
| 87 | transition-all duration-150 ease-out |
| 88 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring focus-visible:ring-offset-2 focus-visible:ring-offset-fd-background |
| 89 | active:scale-[0.98] |
| 90 | touch-manipulation |
| 91 | motion-reduce:transition-none motion-reduce:active:scale-100" |
| 92 | aria-label="Share feedback about this page" |
| 93 | > |
| 94 | <PenLine className="size-3.5" aria-hidden="true" /> |
| 95 | <span>Feedback</span> |
| 96 | </button> |
| 97 | |
| 98 | {isOpen && ( |
| 99 | <div |
| 100 | className="fixed inset-0 z-50 flex items-end sm:items-center justify-center |
| 101 | bg-black/60 backdrop-blur-sm |
| 102 | animate-in fade-in duration-150 |
| 103 | motion-reduce:animate-none |
| 104 | p-0 sm:p-4" |
| 105 | onClick={handleClose} |
| 106 | role="dialog" |
| 107 | aria-modal="true" |
| 108 | aria-labelledby="feedback-title" |
| 109 | > |
| 110 | <div |
| 111 | className="bg-fd-background border border-fd-border shadow-2xl |
| 112 | w-full max-w-md p-5 sm:p-6 |
| 113 | pb-[calc(1.25rem+env(safe-area-inset-bottom))] sm:pb-6 |
| 114 | rounded-t-xl sm:rounded-xl |
| 115 | animate-in slide-in-from-bottom duration-200 sm:zoom-in-95 |
| 116 | motion-reduce:animate-none |
| 117 | overscroll-contain |
| 118 | max-h-[90dvh] overflow-y-auto" |
| 119 | onClick={(e) => e.stopPropagation()} |
| 120 | > |
| 121 | <div className="flex items-center justify-between mb-4"> |
| 122 | <h2 id="feedback-title" className="text-lg font-semibold text-fd-foreground"> |
| 123 | Share feedback |
| 124 | </h2> |
| 125 | <button |
| 126 | type="button" |
| 127 | onClick={handleClose} |
| 128 | className="p-1.5 -m-1.5 rounded-md |
| 129 | text-fd-muted-foreground hover:text-fd-foreground hover:bg-fd-secondary |
| 130 | transition-all duration-150 ease-out |
| 131 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring |
| 132 | active:scale-95 |
| 133 | motion-reduce:transition-none motion-reduce:active:scale-100" |
| 134 | aria-label="Close feedback dialog" |
| 135 | > |
| 136 | <X className="size-5" aria-hidden="true" /> |
| 137 | </button> |
| 138 | </div> |
| 139 | |
| 140 | {state === 'success' ? ( |
| 141 | <div |
| 142 | className="text-center py-8 animate-in fade-in zoom-in-95 duration-200 motion-reduce:animate-none" |
| 143 | role="status" |
| 144 | aria-live="polite" |
| 145 | > |
| 146 | <div className="inline-flex items-center justify-center size-12 mb-3 rounded-full bg-emerald-100 dark:bg-emerald-900/30"> |
| 147 | <svg |
| 148 | className="size-6 text-emerald-600 dark:text-emerald-400" |
| 149 | fill="none" |
| 150 | viewBox="0 0 24 24" |
| 151 | stroke="currentColor" |
| 152 | strokeWidth={2} |
| 153 | aria-hidden="true" |
| 154 | > |
| 155 | <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" /> |
| 156 | </svg> |
| 157 | </div> |
| 158 | <p className="text-fd-foreground font-medium">Thank you!</p> |
| 159 | <p className="text-sm text-fd-muted-foreground mt-1">Your feedback helps us improve.</p> |
| 160 | </div> |
| 161 | ) : ( |
| 162 | <form onSubmit={handleSubmit}> |
| 163 | <p className="text-sm text-fd-muted-foreground mb-4"> |
| 164 | Help us improve our documentation by sharing your thoughts. |
| 165 | </p> |
| 166 | |
| 167 | <fieldset className="mb-4"> |
| 168 | <legend className="sr-only">How would you rate this page?</legend> |
| 169 | <div className="flex gap-2" role="radiogroup" aria-label="Sentiment"> |
| 170 | {[ |
| 171 | { value: 'positive', emoji: '😊', label: 'Positive' }, |
| 172 | { value: 'neutral', emoji: '😐', label: 'Neutral' }, |
| 173 | { value: 'negative', emoji: '😞', label: 'Negative' }, |
| 174 | ].map((option) => ( |
| 175 | <button |
| 176 | key={option.value} |
| 177 | type="button" |
| 178 | onClick={() => setSentiment(option.value as Sentiment)} |
| 179 | className={`flex-1 py-3 sm:py-2.5 px-3 rounded-lg border text-xl |
| 180 | transition-all duration-150 ease-out |
| 181 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring focus-visible:ring-offset-2 focus-visible:ring-offset-fd-background |
| 182 | active:scale-[0.98] |
| 183 | touch-manipulation |
| 184 | motion-reduce:transition-none motion-reduce:active:scale-100 |
| 185 | ${ |
| 186 | sentiment === option.value |
| 187 | ? 'border-fd-primary bg-fd-primary/10 shadow-sm ring-1 ring-fd-primary/20' |
| 188 | : 'border-fd-border hover:border-fd-muted-foreground hover:bg-fd-secondary/50' |
| 189 | }`} |
| 190 | role="radio" |
| 191 | aria-checked={sentiment === option.value} |
| 192 | aria-label={option.label} |
| 193 | > |
| 194 | <span className="block transform transition-transform duration-150 hover:scale-110 motion-reduce:hover:scale-100"> |
| 195 | {option.emoji} |
| 196 | </span> |
| 197 | </button> |
| 198 | ))} |
| 199 | </div> |
| 200 | </fieldset> |
| 201 | |
| 202 | <label className="block mb-4"> |
| 203 | <span className="text-sm font-medium text-fd-foreground"> |
| 204 | Your feedback <span className="text-red-500 dark:text-red-400">*</span> |
| 205 | </span> |
| 206 | <textarea |
| 207 | value={message} |
| 208 | onChange={(e) => setMessage(e.target.value)} |
| 209 | placeholder="Tell us what you think…" |
| 210 | required |
| 211 | rows={4} |
| 212 | className="mt-1.5 w-full px-3 py-3 sm:py-2.5 border border-fd-border rounded-lg |
| 213 | bg-fd-background text-fd-foreground text-base sm:text-sm |
| 214 | placeholder:text-fd-muted-foreground |
| 215 | transition-colors duration-150 |
| 216 | focus:outline-none focus:ring-2 focus:ring-fd-ring focus:border-transparent |
| 217 | resize-none" |
| 218 | /> |
| 219 | </label> |
| 220 | |
| 221 | <label className="block mb-6"> |
| 222 | <span className="text-sm font-medium text-fd-foreground"> |
| 223 | Email <span className="text-fd-muted-foreground font-normal">(optional)</span> |
| 224 | </span> |
| 225 | <input |
| 226 | type="email" |
| 227 | value={email} |
| 228 | onChange={(e) => setEmail(e.target.value)} |
| 229 | placeholder="you@example.com" |
| 230 | autoComplete="email" |
| 231 | className="mt-1.5 w-full px-3 py-3 sm:py-2.5 border border-fd-border rounded-lg |
| 232 | bg-fd-background text-fd-foreground text-base sm:text-sm |
| 233 | placeholder:text-fd-muted-foreground |
| 234 | transition-colors duration-150 |
| 235 | focus:outline-none focus:ring-2 focus:ring-fd-ring focus:border-transparent" |
| 236 | /> |
| 237 | </label> |
| 238 | |
| 239 | <div className="flex flex-col-reverse sm:flex-row gap-2 sm:gap-3 sm:justify-end pt-4 border-t border-fd-border/50"> |
| 240 | <button |
| 241 | type="button" |
| 242 | onClick={handleClose} |
| 243 | className="w-full sm:w-auto px-4 py-2.5 sm:py-2 text-sm font-medium rounded-lg |
| 244 | text-fd-muted-foreground hover:text-fd-foreground hover:bg-fd-secondary |
| 245 | transition-all duration-150 ease-out |
| 246 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring |
| 247 | active:scale-[0.98] |
| 248 | touch-manipulation |
| 249 | motion-reduce:transition-none motion-reduce:active:scale-100" |
| 250 | > |
| 251 | Cancel |
| 252 | </button> |
| 253 | <button |
| 254 | type="submit" |
| 255 | disabled={!message.trim() || state === 'loading'} |
| 256 | className="w-full sm:w-auto px-4 py-2.5 sm:py-2 text-sm font-medium rounded-lg |
| 257 | bg-fd-primary text-fd-primary-foreground |
| 258 | hover:bg-fd-primary/90 |
| 259 | disabled:opacity-50 disabled:pointer-events-none |
| 260 | transition-all duration-150 ease-out |
| 261 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring focus-visible:ring-offset-2 focus-visible:ring-offset-fd-background |
| 262 | active:scale-[0.98] |
| 263 | touch-manipulation |
| 264 | motion-reduce:transition-none motion-reduce:active:scale-100 |
| 265 | inline-flex items-center justify-center gap-2" |
| 266 | aria-live="polite" |
| 267 | > |
| 268 | {state === 'loading' && ( |
| 269 | <Loader2 className="size-4 animate-spin motion-reduce:animate-none" aria-hidden="true" /> |
| 270 | )} |
| 271 | <span>{state === 'loading' ? 'Sending…' : state === 'error' ? 'Try again' : 'Submit'}</span> |
| 272 | </button> |
| 273 | </div> |
| 274 | </form> |
| 275 | )} |
| 276 | </div> |
| 277 | </div> |
| 278 | )} |
| 279 | </> |
| 280 | ); |
| 281 | } |