Reactions and comments
Emoji reactions, a threaded comment section, and votes that update before the network does.
The writing side. Every write here is optimistic: the screen changes the moment the fan acts, and the SDK rolls it back if the platform refuses.
Reactions
Two things to render: the counts already on the post, and a picker for adding one.
import { useNewsfeedReactionTypes, useReactToPost } from '@xborglabs/fanbase-react'
function Reactions({ post }) {
const { data: types = [] } = useNewsfeedReactionTypes()
const { reactToPost } = useReactToPost()
const react = (reactionTypeId) => reactToPost({ newsId: post.newsId, reactionTypeId })
const unused = types.filter((type) => !post.reactions.some((r) => r.reactionTypeId === type.reactionTypeId))
return (
<div className="your-reactions">
{post.reactions.map((reaction) => (
<button
key={reaction.reactionTypeId}
aria-pressed={reaction.isSelected}
onClick={() => react(reaction.reactionTypeId)}
>
<img src={reaction.imageUrl} alt={reaction.name ?? ''} /> {reaction.count}
</button>
))}
{unused.length > 0 && <ReactionPicker types={unused} onPick={react} />}
</div>
)
}Counts come from post.reactions. Each one says whether this fan has already used it, in isSelected. Pressing one they have used takes it back.
The emoji are your community's own, defined in the Hub, not a fixed set. useNewsfeedReactionTypes returns the full list for the picker. Offer only the ones not already on the post, since the rest are right there as counts.
If the list of types has not loaded or comes back empty, still show the counts and leave the picker out. Do not build a picker from the counts: that offers only the emoji someone has already used.
The screen updates the moment the fan taps. Do not update your own copy of the post as well. The SDK already has, and doing it twice counts the tap twice.
The comment section
Check two things before rendering any of it.
import { useFanbaseSession } from '@xborglabs/fanbase-react'
function Comments({ post }) {
const session = useFanbaseSession()
if (!post.commentsEnabled) return null
if (!session) return <SignInToComment />
return <CommentThread newsId={post.newsId} />
}| Case | Show |
|---|---|
post.commentsEnabled is false | Nothing. Your community team switched comments off for this post. |
| Not signed in | A sign-in prompt, and no comment calls |
Reading comments
import { NEWSFEED_COMMENT_SORT, buildCommentTree, useNewsfeedComments } from '@xborglabs/fanbase-react'
function CommentThread({ newsId }) {
const [sort, setSort] = useState(NEWSFEED_COMMENT_SORT.NEW)
const { data, fetchNextPage, hasNextPage } = useNewsfeedComments({ newsId, sort })
const tree = buildCommentTree(data?.pages.flatMap((page) => page.results) ?? [])
return (
<section className="your-comments">
<SortPicker value={sort} onChange={setSort} />
<Composer newsId={newsId} />
{tree.map((node) => <CommentNode key={node.comment.commentId} node={node} newsId={newsId} depth={0} />)}
{hasNextPage && <button onClick={() => fetchNextPage()}>More comments</button>}
</section>
)
}Comments arrive as a flat list. buildCommentTree nests them: each node is a comment and its children.
| Sort | Order |
|---|---|
NEWSFEED_COMMENT_SORT.NEW | Newest first |
NEWSFEED_COMMENT_SORT.TOP | Highest score first |
NEWSFEED_COMMENT_SORT.BEST | The platform's best-first ranking |
NEWSFEED_COMMENT_SORT.CONTROVERSIAL | The platform's most-contested ranking |
The ranking happens on the server. Changing the sort refetches the thread in the new order.
Nesting
import { COMMENT_INDENT_CAP } from '@xborglabs/fanbase-react'
function CommentNode({ node, newsId, depth }) {
const indent = Math.min(depth, COMMENT_INDENT_CAP)
return (
<div style={{ marginInlineStart: indent * 16 }}>
<Comment comment={node.comment} newsId={newsId} />
{node.children.map((child) => (
<CommentNode key={child.comment.commentId} node={child} newsId={newsId} depth={depth + 1} />
))}
{node.comment.hasMoreReplies && <MoreReplies newsId={newsId} commentId={node.comment.commentId} />}
</div>
)
}Replies can go deeper than a screen is wide. COMMENT_INDENT_CAP is 5: stop indenting past that and let deeper replies line up at the fifth level. The thread stays readable on a phone.
When a comment has more replies than came back, hasMoreReplies is set. Load them with useNewsfeedCommentReplies({ newsId, commentId }).
A comment can arrive with isDeleted set. Render it as "Comment deleted" rather than dropping it, so any replies under it keep their context.
Writing a comment
import { usePostComment, validateCommentText } from '@xborglabs/fanbase-react'
function Composer({ newsId, parentId }) {
const [text, setText] = useState('')
const [error, setError] = useState(null)
const { postComment } = usePostComment()
async function submit() {
const checked = validateCommentText(text)
if (!checked.ok) return setError(checked.reason)
await postComment({ newsId, text: checked.text, parentId })
setText('')
}
return (
<form onSubmit={(e) => { e.preventDefault(); submit() }}>
<textarea value={text} onChange={(e) => setText(e.target.value)} maxLength={500} />
{error === 'COMMENT_TOO_LONG' && <p>Keep it under 500 characters.</p>}
<button>Post</button>
</form>
)
}Always run validateCommentText first. It trims the text and checks the length, and it returns the trimmed version for you to send.
| Reason | Means |
|---|---|
COMMENT_TOO_SHORT | Empty, or only spaces |
COMMENT_TOO_LONG | Over 500 characters |
Send checked.text, not what the fan typed. The platform stores what you send, so sending the raw input keeps the stray spaces the check just removed.
Pass parentId to reply to a comment. The comment count on the post goes up the moment the fan posts, on the feed card and on the post page.
A fan's own comments carry isOwn. Use it to show edit and delete, through useUpdateComment and useDeleteComment. Run validateCommentText on an edit too.
Votes
import { NEWSFEED_COMMENT_VOTE, useCommentVote } from '@xborglabs/fanbase-react'
function Votes({ comment, newsId }) {
const { vote } = useCommentVote()
const up = comment.userVote === NEWSFEED_COMMENT_VOTE.UP
const down = comment.userVote === NEWSFEED_COMMENT_VOTE.DOWN
const cast = (value) =>
vote({ newsId, commentId: comment.commentId, value: comment.userVote === value ? NEWSFEED_COMMENT_VOTE.NONE : value })
return (
<>
<button aria-pressed={up} onClick={() => cast(NEWSFEED_COMMENT_VOTE.UP)}>▲</button>
<span>{comment.score}</span>
<button aria-pressed={down} onClick={() => cast(NEWSFEED_COMMENT_VOTE.DOWN)}>▼</button>
</>
)
}| Vote | Value |
|---|---|
NEWSFEED_COMMENT_VOTE.UP | Upvote |
NEWSFEED_COMMENT_VOTE.DOWN | Downvote |
NEWSFEED_COMMENT_VOTE.NONE | Take the vote back |
Pressing the same vote twice should take it back, which is what cast above does. Comments take emoji reactions as well, through useReactToComment, the same way posts do.
Checklist
- Tapping a reaction updates the count straight away, and tapping it again takes it back
- The picker offers only the emoji not already on the post
- Comments are hidden on a post that has them switched off
- A signed-out fan sees a sign-in prompt and nothing else
- Replies nest, and stop indenting past five levels
- "More replies" loads the rest of a thread
- An empty or overlong comment never reaches the platform
- The trimmed text is what gets sent
- Pressing the same vote twice takes it back