Skip to content

Commit

Permalink
collapse downvoted comments by default when `DownvotedCollapsedByDefa…
Browse files Browse the repository at this point in the history
…ult` is set
  • Loading branch information
jameslounds committed Feb 21, 2025
1 parent d3aabce commit 14b8df9
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/features/comment/inTree/CommentTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,28 @@ export default function CommentTree({
baseDepth,
}: CommentTreeProps) {
const dispatch = useAppDispatch();
const commentId = comment.comment_view.comment.id;
const hasUserCollapsed = useAppSelector((state) =>
Object.hasOwn(state.comment.commentCollapsedById, commentId),
);
const collapsed = useAppSelector(
(state) =>
state.comment.commentCollapsedById[comment.comment_view.comment.id],
(state) => state.comment.commentCollapsedById[commentId],
);
const tapToCollapse = useAppSelector(
(state) => state.settings.general.comments.tapToCollapse,
);
const { activePageRef } = useContext(AppContext);

const downvotedCollapsedByDefault = useAppSelector(
(state) => state.settings.general.comments.downvotedCollapsedByDefault,
);

const isDownvoted =
comment.comment_view.counts.upvotes < comment.comment_view.counts.downvotes;
const shouldBeCollapsed = hasUserCollapsed
? collapsed
: downvotedCollapsedByDefault && isDownvoted;

// Comment context chains don't show missing for parents
const showMissing = (() => {
if (!highlightedCommentId) return true;
Expand Down Expand Up @@ -68,7 +81,7 @@ export default function CommentTree({
depth={comment.absoluteDepth - baseDepth}
absoluteDepth={comment.absoluteDepth}
key={comment.comment_view.comment.id}
collapsed={collapsed || fullyCollapsed}
collapsed={shouldBeCollapsed || fullyCollapsed}
comment={comment}
/>
);
Expand Down Expand Up @@ -101,7 +114,7 @@ export default function CommentTree({

scrollCommentIntoViewIfNeeded(e.target, activePageRef);
}}
collapsed={collapsed}
collapsed={shouldBeCollapsed}
fullyCollapsed={!!fullyCollapsed}
rootIndex={rootIndex}
/>
Expand All @@ -111,7 +124,7 @@ export default function CommentTree({
key={comment.comment_view.comment.id}
highlightedCommentId={highlightedCommentId}
comment={comment}
fullyCollapsed={collapsed || fullyCollapsed}
fullyCollapsed={shouldBeCollapsed || fullyCollapsed}
rootIndex={rootIndex}
baseDepth={baseDepth}
/>
Expand All @@ -126,7 +139,7 @@ export default function CommentTree({
depth={comment.absoluteDepth - baseDepth}
absoluteDepth={comment.absoluteDepth}
missing={comment.missing}
collapsed={collapsed || fullyCollapsed}
collapsed={shouldBeCollapsed || fullyCollapsed}
/>,
);
}
Expand Down

0 comments on commit 14b8df9

Please sign in to comment.