Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ interface ErrorFeedbackProps {
errorCode: string
}
export function ErrorFeedback({ errorCode }: ErrorFeedbackProps) {
const [voted, setVoted] = useState<boolean | null>(null)
const hasVoted = voted !== null
const [votedMap, setVotedMap] = useState<Record<string, boolean>>({})
const voted = votedMap[errorCode]
const hasVoted = voted !== undefined

const handleFeedback = useCallback(
async (wasHelpful: boolean) => {
// Optimistically set feedback state without loading/error states to keep implementation simple
setVoted(wasHelpful)
setVotedMap((prev) => ({
...prev,
[errorCode]: wasHelpful,
}))

try {
const response = await fetch(
`${process.env.__NEXT_ROUTER_BASEPATH || ''}/__nextjs_error_feedback?${new URLSearchParams(
Expand All @@ -36,35 +41,33 @@ export function ErrorFeedback({ errorCode }: ErrorFeedbackProps) {
)

return (
<>
<div className="error-feedback" role="region" aria-label="Error feedback">
{hasVoted ? (
<p className="error-feedback-thanks" role="status" aria-live="polite">
Thanks for your feedback!
</p>
) : (
<>
<p>Was this helpful?</p>
<button
aria-label="Mark as helpful"
onClick={() => handleFeedback(true)}
className={`feedback-button ${voted === true ? 'voted' : ''}`}
type="button"
>
<ThumbsUp aria-hidden="true" />
</button>
<button
aria-label="Mark as not helpful"
onClick={() => handleFeedback(false)}
className={`feedback-button ${voted === false ? 'voted' : ''}`}
type="button"
>
<ThumbsDown aria-hidden="true" />
</button>
</>
)}
</div>
</>
<div className="error-feedback" role="region" aria-label="Error feedback">
{hasVoted ? (
<p className="error-feedback-thanks" role="status" aria-live="polite">
Thanks for your feedback!
</p>
) : (
<>
<p>Was this helpful?</p>
<button
aria-label="Mark as helpful"
onClick={() => handleFeedback(true)}
className={`feedback-button ${voted === true ? 'voted' : ''}`}
type="button"
>
<ThumbsUp aria-hidden="true" />
</button>
<button
aria-label="Mark as not helpful"
onClick={() => handleFeedback(false)}
className={`feedback-button ${voted === false ? 'voted' : ''}`}
type="button"
>
<ThumbsDown aria-hidden="true" />
</button>
</>
)}
</div>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const Default: Story = {
id: 1,
event: {
type: ACTION_UNHANDLED_ERROR,
reason: new Error('First error message'),
reason: Object.assign(new Error('First error message'), {
__NEXT_ERROR_CODE: 'E001',
}),
componentStackFrames: [
{
file: 'app/page.tsx',
Expand All @@ -47,23 +49,29 @@ export const Default: Story = {
id: 2,
event: {
type: ACTION_UNHANDLED_ERROR,
reason: new Error('Second error message'),
reason: Object.assign(new Error('Second error message'), {
__NEXT_ERROR_CODE: 'E002',
}),
frames: [],
},
},
{
id: 3,
event: {
type: ACTION_UNHANDLED_ERROR,
reason: new Error('Third error message'),
reason: Object.assign(new Error('Third error message'), {
__NEXT_ERROR_CODE: 'E003',
}),
frames: [],
},
},
{
id: 4,
event: {
type: ACTION_UNHANDLED_ERROR,
reason: new Error('Fourth error message'),
reason: Object.assign(new Error('Fourth error message'), {
__NEXT_ERROR_CODE: 'E004',
}),
frames: [],
},
},
Expand Down