Skip to content

Commit

Permalink
Merge branch 'preview'
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelfan committed Dec 16, 2023
2 parents 144c5a9 + 5c79fc8 commit c8733a1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
65 changes: 37 additions & 28 deletions src/containers/thread/PostThreadContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) ||
Expand All @@ -67,7 +70,7 @@ export default function PostThreadContainer(props: Props) {
)}
{AppBskyFeedDefs.isBlockedAuthor(thread) && (
<BlockedEmbed depth={0} />
)}
)}
{isError && (
<FeedAlert
variant="badResponse"
Expand Down Expand Up @@ -124,35 +127,41 @@ export default function PostThreadContainer(props: Props) {
)}

{contentFilter &&
threadPreferences &&
replyChains &&
replyChains.map((replyArr, i) => (
<div
className="p-3 border border-x-0 md:border-x first:border-t-0 last:border-b md:last:rounded-b-2xl even:[&:not(:last-child)]:border-b-0 odd:[&:not(:last-child)]:border-b-0"
key={i}
>
{replyArr.map((reply, j) => (
<div className={reply.post.uri} key={reply.post.uri}>
{AppBskyFeedDefs.isBlockedPost(reply) && (
<BlockedEmbed depth={0} />
)}
{AppBskyFeedDefs.isNotFoundPost(reply) && (
<NotFoundEmbed depth={0} />
)}
{AppBskyFeedDefs.isBlockedAuthor(reply) && (
<BlockedEmbed depth={0} />
)}
replyChains
.sort((a, b) => sortThread(a[0], b[0], threadPreferences))
.map((replyArr, i) => (
<div
className="p-3 border border-x-0 md:border-x first:border-t-0 last:border-b md:last:rounded-b-2xl even:[&:not(:last-child)]:border-b-0 odd:[&:not(:last-child)]:border-b-0"
key={i}
>
{replyArr.map((reply, j) => (
<div className={reply.post.uri} key={reply.post.uri}>
{AppBskyFeedDefs.isBlockedPost(reply) && (
<BlockedEmbed depth={0} />
)}
{AppBskyFeedDefs.isNotFoundPost(reply) && (
<NotFoundEmbed depth={0} />
)}
{AppBskyFeedDefs.isBlockedAuthor(reply) && (
<BlockedEmbed depth={0} />
)}

{AppBskyFeedDefs.isThreadViewPost(reply) && j < MAX_REPLIES && (
<FeedPost
post={reply}
filter={contentFilter}
isParent={j < replyArr.length - 1 && j < MAX_REPLIES - 1}
/>
)}
</div>
))}
</div>
))}
{AppBskyFeedDefs.isThreadViewPost(reply) &&
j < MAX_REPLIES && (
<FeedPost
post={reply}
filter={contentFilter}
isParent={
j < replyArr.length - 1 && j < MAX_REPLIES - 1
}
/>
)}
</div>
))}
</div>
))}
</>
);
}
6 changes: 3 additions & 3 deletions src/lib/hooks/bsky/feed/useOrganizeThread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 32 additions & 0 deletions src/lib/utils/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit c8733a1

Please sign in to comment.