Skip to content

Commit 7bae9d1

Browse files
committed
docs(readme.md): new use replies hook
1 parent 070e057 commit 7bae9d1

File tree

2 files changed

+27
-71
lines changed

2 files changed

+27
-71
lines changed

README.md

Lines changed: 25 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -927,90 +927,44 @@ const filter = useCallback((edit) => edit.subplebbitAddress === subplebbitAddres
927927
const {accountEdits} = useAccountEdits({filter})
928928
```
929929
930-
#### Get replies to a post (nested)
930+
#### Get replies to a post (nested or flat)
931931
932932
```jsx
933-
import {useComment, useAccountComments} from '@plebbit/plebbit-react-hooks'
934-
935-
const useRepliesAndAccountReplies = (comment) => {
936-
// filter only the parent cid
937-
const filter = useCallback((accountComment) => accountComment.parentCid === (comment?.cid || 'n/a'), [comment?.cid])
938-
const {accountComments} = useAccountComments({filter})
939-
940-
// the account's replies have a delay before getting published, so get them locally from accountComments instead
941-
const accountRepliesNotYetPublished = useMemo(() => {
942-
const replies = comment?.replies?.pages?.topAll?.comments || []
943-
const replyCids = new Set(replies.map(reply => reply.cid))
944-
// filter out the account comments already in comment.replies, so they don't appear twice
945-
return accountComments.filter(accountReply => !replyCids.has(accountReply.cid))
946-
}, [comment?.replies?.pages?.topAll?.comments, accountComments])
947-
948-
const repliesAndNotYetPublishedReplies = useMemo(() => {
949-
return [
950-
// put the author's unpublished replies at the top, latest first (reverse)
951-
...accountRepliesNotYetPublished.reverse(),
952-
// put the published replies after,
953-
...comment?.replies?.pages?.topAll?.comments || []
954-
]
955-
}, [comment?.replies?.pages?.topAll?.comments, accountRepliesNotYetPublished])
956-
957-
return repliesAndNotYetPublishedReplies
958-
}
933+
import {useReplies, useComment, useAccountComments} from '@plebbit/plebbit-react-hooks'
934+
935+
// NOTE: recommended to use the same replies options for all depths, or will load slower
936+
const useRepliesOptions = {flat: false, repliesPerPage: 20, accountComments: {newerThan: Infinity, append: false}}
937+
938+
const Reply = ({reply, updatedReply}) => {
939+
const {replies, updatedReplies, bufferedReplies, hasMore, loadMore} = useReplies({...useRepliesOptions, sortType: 'best', comment: reply})
940+
941+
// updatedReply updates values in real time, reply does not
942+
const score = (updatedReply?.upvoteCount || 0) - (updatedReply?.downvoteCount || 0)
943+
944+
// bufferedReplies updates in real time, can show new replies count in real time
945+
const moreReplies = hasMore && bufferedReplies?.length !== 0 ? `(${bufferedReplies.length} more replies)` : ''
946+
947+
// publishing states exist only on account comment
948+
const accountReply = useAccountComment({commentIndex: reply.index})
949+
const state = accountReply?.state
950+
const publishingStateString = useStateString(accountReply)
959951

960-
const Reply = ({reply}) => {
961-
const replies = useRepliesAndAccountReplies(reply)
962952
return (
963953
<div>
964-
<div>{reply.author.address} {reply.timestamp}</div>
965-
{reply.state === 'pending' && <div>PENDING</div>}
966-
{reply.state === 'failed' && <div>FAILED</div>}
954+
<div>{score} {reply.author.address} {reply.timestamp} {moreReplies}</div>
955+
{state === 'pending' && <div>PENDING ({publishingStateString})</div>}
956+
{state === 'failed' && <div>FAILED</div>}
967957
<div>{reply.content}</div>
968958
<div style={{marginLeft: 4}}>
969-
{replies.map(reply => <Reply reply={reply}/>))}
959+
{replies.map((reply, index) => <Reply key={reply?.index || reply?.cid} reply={reply} updatedReply={updatedReplies[index]}/>)}
970960
</div>
971961
</div>
972962
)
973963
}
974964

975-
// account replies can have reply.state 'pending' or 'failed' when they are not published yet
976-
// it is recommended to add a 'pending' or 'failed' label to these replies
977-
const comment = useComment({commentCid})
978-
const replies = useRepliesAndAccountReplies(comment)
979-
const repliesComponents = replies.map(reply => <Reply reply={reply}/>)
980-
```
981-
982-
#### Get replies to a post flattened (not nested)
983-
984-
```jsx
985-
import {useComment, useAccountComments} from '@plebbit/plebbit-react-hooks'
986-
import {flattenCommentsPages} from '@plebbit/plebbit-react-hooks/dist/lib/utils'
987-
988-
// the post
989965
const comment = useComment({commentCid})
990-
991-
// the default way to display replies is nested, flatten (unnest) them instead
992-
const flattenedReplies = useMemo(() => flattenCommentsPages(comment.replies), [comment.replies])
993-
994-
// the account replies to the post (commentCid) and account replies to all the post's replies
995-
const postAndRepliesCids = useMemo(() => new Set([(commentCid || 'n/a'), ...flattenedReplies.map(reply => reply.cid)]), [commentCid, flattenedReplies])
996-
const filter = useCallback((accountComment) => postAndRepliesCids.has(accountComment.parentCid), [postAndRepliesCids])
997-
const {accountComments} = useAccountComments({filter})
998-
999-
// the account's replies have a delay before getting published, so get them locally from accountComments instead
1000-
const accountRepliesNotYetInCommentReplies = useMemo(() => {
1001-
const commentReplyCids = new Set(flattenedReplies.map(reply => reply.cid))
1002-
// filter out the account comments already in comment.replies, so they don't appear twice
1003-
return accountComments.filter(accountReply => !commentReplyCids.has(accountReply.cid))
1004-
}, [flattenedReplies, accountComments])
1005-
1006-
// merge the not yet published account replies and published replies, and sort them by timestamp
1007-
const sortedReplies = useMemo(() => [...accountRepliesNotYetInCommentReplies, ...flattenedReplies].sort((a, b) => a.timestamp - b.timestamp), [accountRepliesNotYetInCommentReplies, flattenedReplies])
1008-
1009-
for (const reply of sortedReplies) {
1010-
// account replies can have reply.state 'pending' or 'failed' when they are not published yet
1011-
// it is recommended to add a 'pending' or 'failed' label to these replies
1012-
console.log(reply)
1013-
}
966+
const {replies, updatedReplies, hasMore, loadMore} = useReplies({...useRepliesOptions, sortType: 'best', comment: reply})
967+
const repliesComponents = replies.map((reply, index) => <Reply key={reply?.index || reply?.cid} reply={reply} updatedReply={updatedReplies[index]}/>)
1014968
```
1015969
1016970
#### Get a shortCid or shortAddress

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ export type CommentsFilter = {
607607
/**
608608
* Replies store
609609
*/
610+
// NOTE: to have different replies options for different depth, eg smaller repliesPerPage for depth > 1,
611+
// we will need another prop, eg `{depth: '>1': {repliesPerPage: 3}}`, because of how react renders work
610612
export type RepliesFeedOptions = {
611613
commentCid: string
612614
commentDepth: number

0 commit comments

Comments
 (0)