Skip to content

Commit a4826ae

Browse files
authored
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.
1 parent 16c49b2 commit a4826ae

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/discussions/common/AuthorLabel.jsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,22 @@ const AuthorLabel = ({
101101
</>
102102
), [author, authorLabelMessage, authorToolTip, icon, isRetiredUser, postCreatedAt, showTextPrimary, alert]);
103103

104-
const learnerPostsLink = (
105-
<Link
106-
data-testid="learner-posts-link"
107-
id="learner-posts-link"
108-
to={generatePath(Routes.LEARNERS.POSTS, { learnerUsername: author, courseId })}
109-
className="text-decoration-none text-reset"
110-
style={{ width: 'fit-content' }}
111-
>
112-
{!alert && authorName}
113-
</Link>
114-
);
104+
const learnerPostsLink = useMemo(() => {
105+
if (!showUserNameAsLink) {
106+
return null;
107+
}
108+
return (
109+
<Link
110+
data-testid="learner-posts-link"
111+
id="learner-posts-link"
112+
to={generatePath(Routes.LEARNERS.POSTS, { learnerUsername: author, courseId })}
113+
className="text-decoration-none text-reset"
114+
style={{ width: 'fit-content' }}
115+
>
116+
{!alert && authorName}
117+
</Link>
118+
);
119+
}, [showUserNameAsLink, author, courseId, alert, authorName]);
115120

116121
return showUserNameAsLink
117122
? (

0 commit comments

Comments
 (0)