Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 4x 40x 40x 40x 40x 40x 40x 40x 40x 40x 17x 40x 40x 5x 5x 5x 5x 5x 5x 5x 40x 22x 40x 30x 8x 2x 8x 8x 40x 5x 35x 5x 1x 2x 1x 2x | 'use client';
import { useCallback, useEffect, useRef, useState } from 'react';
import { usePathname } from 'next/navigation';
import { gatherClientSignals, submitFeedback, type FeedbackRating } from '@/lib/feedback';
import { ThumbsDownIcon, ThumbsUpIcon } from './icons';
import { Button } from './ui/Button';
import { Textarea } from './ui/Input';
import { Popover } from './ui/Popover';
/**
* "Help us improve" feedback control.
*
* Clicking a thumb picks a rating and opens a popover for an optional comment.
* The submission is sent once, when the popover closes: **Submit** sends the
* rating + comment, **Cancel** (or clicking away) sends the rating alone, so
* the vote still counts. Everything sent is anonymous (see `src/lib/feedback.ts`).
*/
export function PageFeedback() {
const pathname = usePathname();
const [rating, setRating] = useState<FeedbackRating | null>(null);
const [comment, setComment] = useState('');
const [submitted, setSubmitted] = useState(false);
const wrapRef = useRef<HTMLDivElement>(null);
const confirmRef = useRef<HTMLSpanElement>(null);
// Cancel / Submit remove the focused button, so focus moves to the confirmation.
const focusConfirmRef = useRef(false);
// Coarse time-on-page: from when the control mounts (with the page) to submit.
const startRef = useRef(0);
useEffect(() => {
startRef.current = performance.now();
}, []);
const isOpen = rating !== null && !submitted;
const finish = useCallback(
(withComment: boolean, moveFocus: boolean) => {
Iif (rating === null) return;
focusConfirmRef.current = moveFocus;
const signals = gatherClientSignals();
void submitFeedback({
page: pathname || window.location.pathname,
title: signals.title,
timestamp: new Date().toISOString(),
rating,
comment: withComment ? comment.trim() : '',
timeOnPageSeconds: Math.round((performance.now() - startRef.current) / 1000),
theme: signals.theme,
locale: signals.locale,
viewport: signals.viewport,
deviceClass: signals.deviceClass,
referrer: signals.referrer,
});
setRating(null);
setComment('');
setSubmitted(true);
},
[rating, comment, pathname],
);
useEffect(() => {
if (submitted && focusConfirmRef.current) confirmRef.current?.focus();
}, [submitted]);
// Clicking outside the control while the popover is open = Cancel; focus stays where the user clicked.
useEffect(() => {
if (!isOpen) return;
const handler = (e: MouseEvent) => {
Eif (wrapRef.current && !wrapRef.current.contains(e.target as Node)) finish(false, false);
};
document.addEventListener('mousedown', handler);
return () => document.removeEventListener('mousedown', handler);
}, [isOpen, finish]);
if (submitted) {
return (
<span ref={confirmRef} className="page-feedback__label" role="status" tabIndex={-1}>
Thanks for your feedback!
</span>
);
}
return (
<div className="page-feedback" ref={wrapRef}>
<span className="page-feedback__label">Help us improve</span>
<Button
variant="ghost"
size="sm"
iconOnly
aria-label="This page was helpful"
aria-pressed={rating === 'up'}
onClick={() => setRating('up')}
>
<ThumbsUpIcon />
</Button>
<Button
variant="ghost"
size="sm"
iconOnly
aria-label="This page was not helpful"
aria-pressed={rating === 'down'}
onClick={() => setRating('down')}
>
<ThumbsDownIcon />
</Button>
{isOpen && (
<Popover className="page-feedback__popover" aria-label="Leave a comment">
<Textarea
autoFocus
value={comment}
onChange={(e) => setComment(e.target.value)}
placeholder="Thanks for your feedback! Leave any specifics in a comment."
supportingText="Please don't include personal information."
aria-label="Feedback comment"
/>
<div className="page-feedback__actions">
<Button variant="outline" size="sm" onClick={() => finish(false, true)}>
Cancel
</Button>
<Button variant="brand" size="sm" onClick={() => finish(true, true)}>
Submit
</Button>
</div>
</Popover>
)}
</div>
);
}
|