Feature Request: Reply To Comments
Opened this issue ยท 9 comments
First of all, thank you so much for building and open sourcing use-comments
. I love how simple it is to setup!
I would love a way to add replies to a particular comment. If someone asks a question, then we can publicly answer them or say thanks maybe.
Happy to make a PR myself if you can guide me a bit & help me get started.
Hi @gupta-ji6,
I'm sorry for the late reply. I'm currently working on a hosted version of this project in which I plan to implement reply to comments functionality. The project itself should be released in 2-3 weeks, and the reply functionality in another 2-3 weeks.
As for this project, I definitely won't have time to work on it right now, so the contributions are most welcome! I've been thinking about this functionality while building it for the first time, and here what I've wanted to do:
- Add new column to the
comments
table:parentComment
orparentId
. - Create a foreign key relation: comments.parentId -> comments.id. A comment could have multiple comments that point to it.
- That said, a comment which is a reply to another one, would point to the "parent" comment.
Example:
Take this GitHub issue as an example. If your comment is the "main comment", and my response is a "reply to a comment", then the information would be stored somewhat this way:
id: 1
author: @gupta-ji6
content: ...
...
parentId: null
---
id: 2
author: @beerose
content: ...
...
parentId: 1
Hope it makes some sense ๐
Let me know if you have more questions!
Hey @beerose . Awesome work here. Were you able to implement reply? Is it available now?
Not yet, but I got some more messages about it, and plan to work on it this weekend. Would you be more interested in "one-level" replies or "multi-level" replies? I wrote a blog post about it here: https://www.aleksandra.codes/comments-db-model, not I just need to apply it ๐
I saw your blog earlier. Some good stuff but it might take me some time to absorb in my schedule.
I think it'd be easier to start with one-level. E.g. blog posts where readers drop comments and the author replies, or product pages where potential customers ask questions, and the admin can reply only (suits my use case too, lol).
Maybe there can be a prop or a CLI which asks the Dev which one they prefer?
Hmm, let's do one-level first (didn't have anyone asking for multi-level yet ๐ , and it'll take less time..), and later we can think about multi-level!
Sounds good. Any way I can help out?
@beerose single level replies are good enough for the start and supports most of the cases ๐ฏ
Let me also know if I can contribute in any way ๐๐ป
@SaadBazaz @gupta-ji6 thank you!
I guess there are a few things to do:
- Decide on the API (more below) and DB structure.
- Update the hook implementation.
- Update the metadata in this repo (should reflect new db structure).
- Update the docs.
As for 1., there are multiple options and questions:
- How should we send a reply? Should it be a part of
addComment
function, with, for example, additionalreplyTo
field, or should it be an additional method:addReply
? - How should users fetch the replies? Should they be fetched automatically for the comment? Or should we allow pagination? Or maybe there should be an
includeReplies
option andfetchReplies(parentCommentId)
?
Could you describe what would be the best API for you?
As for the DB, I suggest adding a new column called parent_id
or reply_to
, and then when fetching "base" comment we would exclude the ones with parent_id
being filled.
How should we send a reply? Should it be a part of
addComment
function, with, for example, additionalreplyTo
field, or should it be an additional method:addReply
?
Technically, a reply is a comment only so adding it to the addComment
function would be right. But, as an API, it might complicate addComment
function.
Adding an additional method addReply
would be a better API in my opinion as it's easier to understand, will split the functionality, and is easy to maintain. What do you think? ๐ค
How should users fetch the replies? Should they be fetched automatically for the comment? Or should we allow pagination? Or maybe there should be an
includeReplies
option andfetchReplies(parentCommentId)
?
For a starter, I think we can automatically fetch replies to the comment (or give a boolean param for it).
But to give more power to the developer and keep stage open for customization, there should be a includeReplies
option and an additional fetchReplies(parentCommentId)
.
If making these is not complex, we can start with fetching replies with comments at once and then provide customization options in later versions. โ๏ธ
As for the DB, I suggest adding a new column called
parent_id
orreply_to
, and then when fetching "base" comment we would exclude the ones withparent_id
being filled.
parent_id
sounds good to me ๐๐ป