fix: prevent generatePath error when author is invalid in AuthorLabel (#821)

Wrap learnerPostsLink creation in useMemo with guard to prevent
'Missing :learnerUsername param' error. The generatePath function
was being called unconditionally during render even when the link
wouldn't be displayed, causing errors when author was null, undefined,
or the 'anonymous' string.

The fix ensures generatePath is only called when showUserNameAsLink
is true, which validates that author is a valid username.
This commit is contained in:
Tobias Macey
2025-11-13 16:08:51 -05:00
committed by GitHub
parent 16c49b2404
commit a4826ae62d

View File

@@ -101,17 +101,22 @@ const AuthorLabel = ({
</>
), [author, authorLabelMessage, authorToolTip, icon, isRetiredUser, postCreatedAt, showTextPrimary, alert]);
const learnerPostsLink = (
<Link
data-testid="learner-posts-link"
id="learner-posts-link"
to={generatePath(Routes.LEARNERS.POSTS, { learnerUsername: author, courseId })}
className="text-decoration-none text-reset"
style={{ width: 'fit-content' }}
>
{!alert && authorName}
</Link>
);
const learnerPostsLink = useMemo(() => {
if (!showUserNameAsLink) {
return null;
}
return (
<Link
data-testid="learner-posts-link"
id="learner-posts-link"
to={generatePath(Routes.LEARNERS.POSTS, { learnerUsername: author, courseId })}
className="text-decoration-none text-reset"
style={{ width: 'fit-content' }}
>
{!alert && authorName}
</Link>
);
}, [showUserNameAsLink, author, courseId, alert, authorName]);
return showUserNameAsLink
? (