Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Comment story performance issue #2048

Merged
merged 4 commits into from
Sep 16, 2022
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
5 changes: 5 additions & 0 deletions .changeset/curly-monkeys-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudfour/patterns': patch
---

Fix Comment story performance issue
42 changes: 24 additions & 18 deletions src/components/comment/comment.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const initCommentReplyForms = () => {
};
}
};
// Generate random comments for better performance.
// Before, the makeComment() function was called inline inside each <Story> but
// the randomness caused the stories to render hundreds or thousands of times.
// The page would freeze up. Now, we create a random set of comments ahead of
// time and just reference them in the stories.
const totalRandomComments = 5;
const randomComments = [];
for (let i = 0; i < totalRandomComments; i++) {
randomComments.push(makeComment());
}
const randomCommentWithReply = makeComment({ replies: 2 });
const randomNotApprovedComment = makeComment({ approved: false });

<Meta
title="Components/Comment"
Expand Down Expand Up @@ -75,7 +87,7 @@ This information may be passed to the component as a `comment` object matching t
docs: {
source: {
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
comment: makeComment(),
comment: randomComments[0],
}),
},
},
Expand All @@ -84,7 +96,7 @@ This information may be passed to the component as a `comment` object matching t
{(args) => {
useEffect(() => initCommentReplyForms());
return template({
comment: makeComment(),
comment: randomComments[0],
allow_replies: args.allowReplies,
logged_in_user: args.isLoggedIn ? tyler : null,
log_out_url: '/logout',
Expand All @@ -111,7 +123,7 @@ It is helpful for context within a discussion to know when a commentor is the or
{(args) => {
useEffect(() => initCommentReplyForms());
return authorDemo({
comment: makeComment(),
comment: randomComments[1],
allow_replies: args.allowReplies,
demo_post_author: true,
logged_in_user: args.isLoggedIn ? tyler : null,
Expand All @@ -135,7 +147,7 @@ It is helpful for context within a discussion to know when a commentor is the or
{(args) => {
useEffect(() => initCommentReplyForms());
return memberDemo({
comment: makeComment(),
comment: randomComments[2],
allow_replies: args.allowReplies,
demo_cloud_four_member: true,
logged_in_user: args.isLoggedIn ? tyler : null,
Expand All @@ -156,9 +168,7 @@ If `comment.approved` is not `true`, an [Alert](/docs/components-alert--basic) w
docs: {
source: {
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
comment: makeComment({
approved: false,
}),
comment: randomNotApprovedComment,
}),
},
},
Expand All @@ -167,9 +177,7 @@ If `comment.approved` is not `true`, an [Alert](/docs/components-alert--basic) w
{(args) => {
useEffect(() => initCommentReplyForms());
return template({
comment: makeComment({
approved: false,
}),
comment: randomNotApprovedComment,
allow_replies: args.allowReplies,
logged_in_user: args.isLoggedIn ? tyler : null,
log_out_url: '/logout',
Expand All @@ -189,7 +197,7 @@ Additionally, a `source` object may be passed to the template consisting of a `u
docs: {
source: {
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
comment: makeComment(),
comment: randomComments[3],
source: {
url: 'https://twitter.com/smashingmag/status/1371521325236416516',
name: 'twitter.com',
Expand All @@ -202,7 +210,7 @@ Additionally, a `source` object may be passed to the template consisting of a `u
{(args) => {
useEffect(() => initCommentReplyForms());
return template({
comment: makeComment(),
comment: randomComments[3],
source: {
url: 'https://twitter.com/smashingmag/status/1371521325236416516',
name: 'twitter.com',
Expand Down Expand Up @@ -231,7 +239,7 @@ If the user is logged in, you should pass in `logged_in_user` and `log_out_url`
docs: {
source: {
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
comment: makeComment(),
comment: randomComments[4],
allow_replies: true,
logged_in_user: tyler,
log_out_url: '/logout',
Expand All @@ -243,7 +251,7 @@ If the user is logged in, you should pass in `logged_in_user` and `log_out_url`
{(args) => {
useEffect(() => initCommentReplyForms());
return template({
comment: makeComment(),
comment: randomComments[4],
allow_replies: args.allowReplies,
logged_in_user: args.isLoggedIn ? tyler : null,
log_out_url: '/logout',
Expand All @@ -268,9 +276,7 @@ While it is theoretically possible to infinitely nest `children`, it's recommend
docs: {
source: {
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
comment: makeComment({
replies: 2,
}),
comment: randomCommentWithReply,
}),
},
},
Expand All @@ -279,7 +285,7 @@ While it is theoretically possible to infinitely nest `children`, it's recommend
{(args) => {
useEffect(() => initCommentReplyForms());
return template({
comment: makeComment({ replies: 2 }),
comment: randomCommentWithReply,
allow_replies: args.allowReplies,
logged_in_user: args.isLoggedIn ? tyler : null,
log_out_url: '/logout',
Expand Down