fix(use-replies): sort by timestamp and move pinned replies to the top

This commit is contained in:
Tom (plebeius.eth)
2024-06-02 20:51:15 +02:00
parent 53efdc1af2
commit 8b000a0bdb
+12 -8
View File
@@ -28,14 +28,18 @@ const useRepliesAndAccountReplies = (comment: Comment) => {
}, [flattenedReplies, accountComments]); }, [flattenedReplies, accountComments]);
const repliesAndNotYetPublishedReplies = useMemo(() => { const repliesAndNotYetPublishedReplies = useMemo(() => {
const repliesSortedByVotes = [ const repliesSortedByPinnedAndTimestamp = [...accountRepliesNotYetPublished.reverse(), ...(flattenedReplies || [])];
// put the author's unpublished replies at the top, latest first (reverse)
...accountRepliesNotYetPublished.reverse(), return repliesSortedByPinnedAndTimestamp.sort((a: Comment, b: Comment) => {
// put the published replies after, // Sort by pinned status first, with pinned comments at the top
...(flattenedReplies || []), if (a.pinned && !b.pinned) {
]; return -1;
// sort by timestamp } else if (!a.pinned && b.pinned) {
return repliesSortedByVotes.sort((a: Comment, b: Comment) => a.timestamp - b.timestamp); return 1;
}
// If both are pinned or both are not pinned, sort by timestamp
return a.timestamp - b.timestamp;
});
}, [flattenedReplies, accountRepliesNotYetPublished]); }, [flattenedReplies, accountRepliesNotYetPublished]);
return repliesAndNotYetPublishedReplies; return repliesAndNotYetPublishedReplies;