|
| 1 | +/** |
| 2 | + * Feedback Widget for Email Templates (#410). |
| 3 | + * |
| 4 | + * A self-contained widget that can be embedded in an email template's |
| 5 | + * landing page or an in-app "How was this email?" prompt. Submissions are |
| 6 | + * forwarded to a consumer-provided onSubmit handler (validation handled via |
| 7 | + * FeedbackEntrySchema in src/schemas/feedback.schema.ts). |
| 8 | + */ |
| 9 | +'use client'; |
| 10 | + |
| 11 | +import { useCallback, useState } from 'react'; |
| 12 | +import { |
| 13 | + FeedbackEntrySchema, |
| 14 | + type FeedbackEntry, |
| 15 | + type FeedbackRating, |
| 16 | +} from '@/schemas/feedback.schema'; |
| 17 | + |
| 18 | +export interface FeedbackWidgetProps { |
| 19 | + templateId: string; |
| 20 | + recipientHash: string; |
| 21 | + urlParams?: Record<string, string>; |
| 22 | + onSubmit?: (entry: FeedbackEntry) => void | Promise<void>; |
| 23 | +} |
| 24 | + |
| 25 | +const RATING_LABEL: Record<FeedbackRating, string> = { |
| 26 | + love: '❤️ Loved it', |
| 27 | + ok: '👍 OK', |
| 28 | + dislike: '👎 Not for me', |
| 29 | +}; |
| 30 | + |
| 31 | +export default function FeedbackWidget({ |
| 32 | + templateId, |
| 33 | + recipientHash, |
| 34 | + urlParams, |
| 35 | + onSubmit, |
| 36 | +}: FeedbackWidgetProps) { |
| 37 | + const [rating, setRating] = useState<FeedbackRating | null>(null); |
| 38 | + const [comment, setComment] = useState(''); |
| 39 | + const [status, setStatus] = useState<'idle' | 'sending' | 'sent' | 'error'>('idle'); |
| 40 | + const [error, setError] = useState<string | null>(null); |
| 41 | + |
| 42 | + const submit = useCallback(async () => { |
| 43 | + if (!rating) return; |
| 44 | + const parsed = FeedbackEntrySchema.safeParse({ |
| 45 | + templateId, |
| 46 | + recipientHash, |
| 47 | + rating, |
| 48 | + comment: comment.trim() || undefined, |
| 49 | + urlParams, |
| 50 | + submittedAt: new Date().toISOString(), |
| 51 | + }); |
| 52 | + if (!parsed.success) { |
| 53 | + setError(parsed.error.issues[0]?.message ?? 'Invalid feedback'); |
| 54 | + setStatus('error'); |
| 55 | + return; |
| 56 | + } |
| 57 | + setStatus('sending'); |
| 58 | + setError(null); |
| 59 | + try { |
| 60 | + await onSubmit?.(parsed.data); |
| 61 | + setStatus('sent'); |
| 62 | + } catch (e) { |
| 63 | + setError(e instanceof Error ? e.message : 'Submission failed'); |
| 64 | + setStatus('error'); |
| 65 | + } |
| 66 | + }, [rating, comment, templateId, recipientHash, urlParams, onSubmit]); |
| 67 | + |
| 68 | + if (status === 'sent') { |
| 69 | + return ( |
| 70 | + <div role="status" aria-live="polite" className="feedback-widget feedback-widget--sent"> |
| 71 | + Thanks for the feedback! |
| 72 | + </div> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <section className="feedback-widget" aria-label="Email feedback"> |
| 78 | + <fieldset> |
| 79 | + <legend>How was this email?</legend> |
| 80 | + {(Object.keys(RATING_LABEL) as FeedbackRating[]).map((r) => ( |
| 81 | + <label key={r} className="feedback-widget__rating"> |
| 82 | + <input |
| 83 | + type="radio" |
| 84 | + name="rating" |
| 85 | + value={r} |
| 86 | + checked={rating === r} |
| 87 | + onChange={() => setRating(r)} |
| 88 | + /> |
| 89 | + {RATING_LABEL[r]} |
| 90 | + </label> |
| 91 | + ))} |
| 92 | + </fieldset> |
| 93 | + <label className="feedback-widget__comment"> |
| 94 | + <span>Comment (optional)</span> |
| 95 | + <textarea |
| 96 | + value={comment} |
| 97 | + onChange={(e) => setComment(e.target.value)} |
| 98 | + maxLength={2000} |
| 99 | + rows={3} |
| 100 | + /> |
| 101 | + </label> |
| 102 | + {error ? ( |
| 103 | + <p role="alert" className="feedback-widget__error"> |
| 104 | + {error} |
| 105 | + </p> |
| 106 | + ) : null} |
| 107 | + <button |
| 108 | + type="button" |
| 109 | + disabled={!rating || status === 'sending'} |
| 110 | + onClick={submit} |
| 111 | + > |
| 112 | + {status === 'sending' ? 'Sending…' : 'Send feedback'} |
| 113 | + </button> |
| 114 | + </section> |
| 115 | + ); |
| 116 | +} |
0 commit comments