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:
@@ -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
|
||||
? (
|
||||
|
||||
Reference in New Issue
Block a user