diff --git a/src/containers/thread/PostThreadContainer.tsx b/src/containers/thread/PostThreadContainer.tsx index 2ad84a49..f3d041d7 100644 --- a/src/containers/thread/PostThreadContainer.tsx +++ b/src/containers/thread/PostThreadContainer.tsx @@ -15,6 +15,8 @@ import Button from "@/components/actions/button/Button"; import { useRouter } from "next/navigation"; import FeedPostSkeleton from "@/components/contentDisplay/feedPost/FeedPostSkeleton"; import FeedAlert from "@/components/feedback/feedAlert/FeedAlert"; +import { ThreadViewResult } from "../../../types/feed"; +import { sortThread } from "@/lib/utils/feed"; interface Props { id: string; @@ -48,6 +50,7 @@ export default function PostThreadContainer(props: Props) { const { preferences } = usePreferences(); const contentFilter = preferences?.contentFilter; + const threadPreferences = preferences?.threadPreferences; if ( AppBskyFeedDefs.isBlockedPost(thread) || @@ -67,7 +70,7 @@ export default function PostThreadContainer(props: Props) { )} {AppBskyFeedDefs.isBlockedAuthor(thread) && ( - )} + )} {isError && ( ( -
- {replyArr.map((reply, j) => ( -
- {AppBskyFeedDefs.isBlockedPost(reply) && ( - - )} - {AppBskyFeedDefs.isNotFoundPost(reply) && ( - - )} - {AppBskyFeedDefs.isBlockedAuthor(reply) && ( - - )} + replyChains + .sort((a, b) => sortThread(a[0], b[0], threadPreferences)) + .map((replyArr, i) => ( +
+ {replyArr.map((reply, j) => ( +
+ {AppBskyFeedDefs.isBlockedPost(reply) && ( + + )} + {AppBskyFeedDefs.isNotFoundPost(reply) && ( + + )} + {AppBskyFeedDefs.isBlockedAuthor(reply) && ( + + )} - {AppBskyFeedDefs.isThreadViewPost(reply) && j < MAX_REPLIES && ( - - )} -
- ))} -
- ))} + {AppBskyFeedDefs.isThreadViewPost(reply) && + j < MAX_REPLIES && ( + + )} +
+ ))} +
+ ))} ); } diff --git a/src/lib/hooks/bsky/feed/useOrganizeThread.tsx b/src/lib/hooks/bsky/feed/useOrganizeThread.tsx index 2a9d2b6a..50908403 100644 --- a/src/lib/hooks/bsky/feed/useOrganizeThread.tsx +++ b/src/lib/hooks/bsky/feed/useOrganizeThread.tsx @@ -23,16 +23,16 @@ export default function useOrganizeThread(props: Props) { ) => { currentChain.push(post); - // Check if the post has replies + // check if the post has replies if (post.replies && post.replies.length > 0) { let longestChain = currentChain; - // Iterate through replies and call the function recursively + // iterate through replies and call the function recursively for (const reply of post.replies) { if (AppBskyFeedDefs.isThreadViewPost(reply)) { const chain = getConnectedReplies(reply, currentChain.slice()); - // Update the longest chain if the new one is longer + // update the longest chain if the new one is longer if (chain.length > longestChain.length) { longestChain = chain; } diff --git a/src/lib/utils/feed.ts b/src/lib/utils/feed.ts index e8b876d6..d4d2a333 100644 --- a/src/lib/utils/feed.ts +++ b/src/lib/utils/feed.ts @@ -185,3 +185,35 @@ export default function getThreadPreferences( return filters; } + +export const sortThread = ( + a: AppBskyFeedDefs.ThreadViewPost, + b: AppBskyFeedDefs.ThreadViewPost, + threadPrefs: ThreadViewResult +) => { + if (threadPrefs.sort === "oldest") { + const aDate = new Date(a.post.indexedAt); + const bDate = new Date(b.post.indexedAt); + return aDate.getTime() - bDate.getTime(); + } else if (threadPrefs.sort === "newest") { + const aDate = new Date(a.post.indexedAt); + const bDate = new Date(b.post.indexedAt); + return bDate.getTime() - aDate.getTime(); + } else if (threadPrefs.sort === "most-likes") { + const aLikes = a.post.likeCount || 0; + const bLikes = b.post.likeCount || 0; + return bLikes - aLikes; + } else if (threadPrefs.sort === "random") { + return Math.random() - 0.5; + } + + if (threadPrefs.prioritizeFollowedUsers) { + const aIsFollowed = a.post.author.viewer?.following; + const bIsFollowed = b.post.author.viewer?.following; + if (aIsFollowed && !bIsFollowed) return -1; + if (!aIsFollowed && bIsFollowed) return 1; + return 0; + } + + return 0; +};