-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Comments Query Loop] Show placeholder comments on site editor #38072
[Comments Query Loop] Show placeholder comments on site editor #38072
Conversation
Size Change: +188 B (0%) Total Size: 1.14 MB
ℹ️ View Unchanged
|
As simple as that? 😄 I'll have a closer look tomorrow. |
It seems so, but I don't know if is a good approach or we usually do setting empty objects as default in the project 😅 |
Hm.., interesting! It would require a bit more changes though, for example here as it throws a React warning right now. |
Using null as id would remove the warning. Anyway, the "options" are not working, and of course, if you set more than 1 item per page. It crashes (not very badly, though). I will iterate it more. |
afe35ac
to
95b42ad
Compare
@@ -97,7 +102,7 @@ const CommentsList = ( { | |||
{ comments && | |||
comments.map( ( comment ) => ( | |||
<BlockContextProvider | |||
key={ comment.commentId } | |||
key={ comment.commentId || uniqueId( 'comment-default' ) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, if we have a null id (required for loading placeholders and not calling REST API for comments), we don't have an error or warning regarding the unique key inside a list. Not sure if it is the best approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this approach is preventing the block to be selected.
Edited: it was a lack of a parent div
with the blockprops
, now it works fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to apply a fallback key as part of comment
object to ensure those keys don't change for the individual items. Otherwise, we will have unnecessary rerenders happening too often.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the comments.map()
's index
as a fallback?
key={ comment.commentId || uniqueId( 'comment-default' ) } | |
key={ comment.commentId || index } |
It's only considered an antipattern if individual items can be removed or moved around, which shouldn't be the case for placeholders IIUC.
// to inner blocks to render the default placeholders. | ||
if ( ! postId ) { | ||
// We set a limit in order not to overload the editor of empty comments. | ||
const defaultCommentsToShow = perPage <= 3 ? perPage : 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any recommended number of comments placeholders to show at maximum?
Ping design @kjellr @jameskoster @karmatosed 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. My initial thought is to check thread_comments_depth
and display enough placeholders to demonstrate maximum nesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thread_comments_depth
can go up to 10 though so that might be overkill 😅
I've settled on a somewhat arbitrary maximum nesting of 3 for the placeholders. I think that should make the threading clear without overwhelming the UI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, I think that can be important. For example if you want to support a depth of 10, you may need to adjust the margins in order for that level of nesting to work. If you don't see a preview then you wouldn't be aware of such an issue.
bb5ba90
to
4e2dbce
Compare
4e2dbce
to
e5127d7
Compare
rawComments: Array( defaultCommentsToShow ).fill( { | ||
id: null, | ||
} ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was quite sure that this would fail if defaultCommentsToShow
were larger than 1, but I was wrong.
My concern was that convertToTree
would fail in that case, but it handles it just fine 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see how will work with nested default comments (as it searches for a parent id) 😅
0967fd2
to
f4fd761
Compare
I added again the background color so no deprecations are needed 😄 I think the PR is ready to land. I will now work on an Avatar block (working wherever we put them). |
That is already happening before the PR 😓 I added an issue, but it also was mentioned in this comment I will work on that as soon as possible. |
I left the background option for not having to deprecate anything 😄 |
1ca9bd1
to
d193676
Compare
'pageComments' => get_option( 'page_comments' ), | ||
'threadComments' => get_option( 'thread_comments' ), | ||
'threadCommentsDepth' => get_option( 'thread_comments_depth' ), | ||
'avatarURL' => get_avatar_url( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the Discussion Settings page and it looks like it has a ton of setting including the default avatar. That was surprising 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, most surprising is that the default avatar of discussion settings is the same default avatar selected for post authors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we are good. I left some non-blocking comments to consider. This enhancement is really exciting. Thank you for finding such a creative and simple solution 👍🏻
]; | ||
} | ||
} | ||
|
||
if ( ! rawComments ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could move this check just after the useSelect
call that returns rawComments
to avoid unnecessary computations. I also see useMemo
hook call, so it would need to be after that.
// In case that `threadCommentsDepth` is falsy, we default to a somewhat | ||
// arbitrary value of 3. | ||
// In case that the value is set but larger than 3 we truncate it to 3. | ||
const commentsDepth = Math.min( threadCommentsDepth || 3, 3 ); | ||
|
||
// We set a limit in order not to overload the editor of empty comments. | ||
const defaultCommentsToShow = | ||
perPage <= commentsDepth ? perPage : commentsDepth; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be moved to the scope of if
section as it's used only when postId
is falsy. I commented about that earlier, all the logic for generating the tree of comment placeholders could be in their own function so it documents better.
This implementation improves the previous one in that it does not call the REST API with non-existent comment IDs
d193676
to
d012330
Compare
Description
This is a quick approach to #37143. We are using the default placeholders' fallback of all components of a comment template.
A better solution may be to update all fallbacks to not depend on getting an empty comment object, feel free to comment or discuss if we should work on a better approach.
How has this been tested?
Screenshots
Screenshots were done on site editor.
Before:
After:
Types of changes
New possible feature discussed on #37143
Checklist: