Skip to content

Commit

Permalink
Updated comment filters
Browse files Browse the repository at this point in the history
  • Loading branch information
mzparacha committed Dec 26, 2024
1 parent cc41703 commit 4aaa10f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
11 changes: 10 additions & 1 deletion libs/model/src/comment/GetComments.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ export function GetComments(): Query<typeof schemas.GetComments> {
include_reactions,
limit,
cursor,
order_by,
include_spam_comments,
} = payload;
const offset = (cursor - 1) * limit;

const orderByQueries = {
newest: '"created_at" DESC',
oldest: '"created_at" ASC',
mostLikes: '"reaction_count" DESC',
};

const sql = `
SELECT
C.id,
Expand Down Expand Up @@ -89,6 +97,7 @@ export function GetComments(): Query<typeof schemas.GetComments> {
C."thread_id" = :thread_id
AND C."parent_id" ${parent_id ? '= :parent_id' : 'IS NULL'}
${comment_id ? ' AND C."id" = :comment_id' : ''}
${!include_spam_comments ? 'AND C."marked_as_spam_at" IS NULL' : ''}
${
include_reactions
? `
Expand All @@ -108,7 +117,7 @@ export function GetComments(): Query<typeof schemas.GetComments> {
: ''
}
ORDER BY
C."created_at"
C.${orderByQueries[order_by || 'newest']}
LIMIT :limit OFFSET :offset;
`;

Expand Down
6 changes: 6 additions & 0 deletions libs/schemas/src/queries/comment.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ export const CommentsView = CommentView.extend({
reactions: z.array(ReactionView).nullish(),
});

export const GetCommentsOrderBy = z.enum(['newest', 'oldest', 'mostLikes']);

export const GetComments = {
input: PaginationParamsSchema.extend({
thread_id: PG_INT,
comment_id: PG_INT.optional(),
parent_id: PG_INT.optional(),
include_reactions: zBoolean.default(false),
include_spam_comments: zBoolean.optional().default(false),
order_by: GetCommentsOrderBy.optional().default('newest'),
}).omit({
order_direction: true,
}),
output: PaginatedResultSchema.extend({
// TODO: fix return types, they break for
Expand Down
1 change: 1 addition & 0 deletions packages/commonwealth/client/scripts/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export enum ThreadViewFilterTypes {
export enum CommentsFeaturedFilterTypes {
Newest = 'newest',
Oldest = 'oldest',
MostLikes = 'mostLikes',
}

export enum ThreadTimelineFilterTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ const useFetchCommentsQuery = ({
comment_id,
parent_id,
include_reactions,
include_spam_comments,
order_by,
apiEnabled = true,
}: FetchCommentsProps) => {
return trpc.comment.getComments.useInfiniteQuery(
{
thread_id,
comment_id,
parent_id,
order_by,
include_reactions,
include_spam_comments,
},
{
staleTime: COMMENTS_STALE_TIME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const CommentTree = ({
const urlParams = new URLSearchParams(location.search);
const focusCommentsParam = urlParams.get('focusComments') === 'true';
// TODO: add these params to API
const [includeSpamThreads, setIncludeSpamThreads] = useState<boolean>(false);
const [includeSpam, setIncludeSpam] = useState<boolean>(false);
const [commentSortType, setCommentSortType] =
useState<CommentsFeaturedFilterTypes>(CommentsFeaturedFilterTypes.Newest);

Expand Down Expand Up @@ -105,6 +105,8 @@ export const CommentTree = ({
} = useFetchCommentsQuery({
thread_id: parseInt(`${thread.id}`) || 0,
include_reactions: true,
include_spam_comments: includeSpam,
order_by: commentSortType,
cursor: 1,
limit: 10,
apiEnabled: !!communityId && !!thread.id,
Expand Down Expand Up @@ -502,12 +504,18 @@ export const CommentTree = ({
label: 'Oldest',
iconLeft: 'clockCounterClockwise',
},
{
id: 3,
value: CommentsFeaturedFilterTypes.MostLikes,
label: 'Upvotes',
iconLeft: 'upvote',
},
]}
/>
<CWCheckbox
checked={includeSpamThreads}
checked={includeSpam}
label="Include comments flagged as spam"
onChange={(e) => setIncludeSpamThreads(e?.target?.checked || false)}
onChange={(e) => setIncludeSpam(e?.target?.checked || false)}
/>
</div>
<div className="CommentsTree">
Expand Down

0 comments on commit 4aaa10f

Please sign in to comment.