Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ReportUserModal #4051

Merged
merged 7 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/shared/src/components/modals/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ const AddToCustomFeedModal = dynamic(
),
);

const ReportUserModal = dynamic(
() =>
import(
/* webpackChunkName: "reportUserModal" */ './report/ReportUserModal'
),
);

export const modals = {
[LazyModal.SquadMember]: SquadMemberModal,
[LazyModal.UpvotedPopup]: UpvotedPopupModal,
Expand Down Expand Up @@ -250,6 +257,7 @@ export const modals = {
[LazyModal.ClickbaitShield]: ClickbaitShieldModal,
[LazyModal.MoveBookmark]: MoveBookmarkModal,
[LazyModal.AddToCustomFeed]: AddToCustomFeedModal,
[LazyModal.ReportUser]: ReportUserModal,
};

type GetComponentProps<T> = T extends
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/components/modals/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export enum LazyModal {
ClickbaitShield = 'clickbaitShield',
MoveBookmark = 'moveBookmark',
AddToCustomFeed = 'addToCustomFeed',
ReportUser = 'reportUser',
}

export type ModalTabItem = {
Expand Down
117 changes: 117 additions & 0 deletions packages/shared/src/components/modals/report/ReportUserModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import type { ReactElement } from 'react';
import React, { useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import { ReasonSelectionModal } from './ReasonSelectionModal';
import type { ReportReason } from '../../../report';
import { ReportEntity, SEND_REPORT_MUTATION } from '../../../report';
import { Checkbox } from '../../fields/Checkbox';
import type { UserShortProfile } from '../../../lib/user';
import { gqlClient } from '../../../graphql/common';
import {
CONTENT_PREFERENCE_BLOCK_MUTATION,
ContentPreferenceType,
} from '../../../graphql/contentPreference';
import { useAuthContext } from '../../../contexts/AuthContext';
import { useToastNotification } from '../../../hooks';
import { useLazyModal } from '../../../hooks/useLazyModal';

const reportReasons: { value: string; label: string }[] = [
{ value: 'INAPPROPRIATE', label: 'Inappropriate or NSFW Content' },
{ value: 'TROLLING', label: 'Trolling or Disruptive Behavior' },
{ value: 'HARASSMENT', label: 'Harassment or Bullying' },
{ value: 'IMPERSONATION', label: 'Impersonation or False Identity' },
{ value: 'SPAM', label: 'Spam or Unsolicited Advertising' },
{ value: 'MISINFORMATION', label: 'Misinformation or False Claims' },
{ value: 'HATE_SPEECH', label: 'Hate Speech or Discrimination' },
{ value: 'PRIVACY', label: 'Privacy or Copyright Violation' },
{ value: 'PLAGIARISM', label: 'Plagiarism or Content Theft' },
{ value: 'OTHER', label: 'Other' },
];

type ReportUserModalProps = {
offendingUser: Pick<UserShortProfile, 'id' | 'username'>;
defaultBlockUser?: boolean;
};

export const ReportUserModal = ({
offendingUser,
defaultBlockUser,
}: ReportUserModalProps): ReactElement => {
const { closeModal: onClose } = useLazyModal();
const { displayToast } = useToastNotification();
const { user } = useAuthContext();
const [blockUser, setBlockUser] = useState(defaultBlockUser);
const { isPending: isBlockPending, mutateAsync: blockUserMutation } =
useMutation({
mutationFn: () =>
gqlClient.request(CONTENT_PREFERENCE_BLOCK_MUTATION, {
id: offendingUser.id,
entity: ContentPreferenceType.User,
feedId: user?.id,
}),
onSuccess: () => {
displayToast(`🚫 ${offendingUser.username} has been blocked`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with Nimrod to change the message to this

Comment on lines +52 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: Do we need to invalidate some query (like the current feed) on success?

Copy link
Contributor Author

@AmarTrebinjac AmarTrebinjac Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Not entirely sure. IIRC we don't invalidate the current feed when blocking sources, tags etc, just the selected post.

I guess adding optional callbacks wouldn't be a bad idea. I can check with product

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we do refresh/remove the post from the feed relevant to the action, but we can maybe check with the product team on this one.

onClose();
},
onError: () => {
displayToast(`❌ Failed to block ${offendingUser.username}`);
},
});
const { isPending: isReportPending, mutateAsync: reportUserMutation } =
useMutation({
mutationFn: ({ reason, text }: { reason: ReportReason; text: string }) =>
gqlClient.request(SEND_REPORT_MUTATION, {
id: offendingUser.id,
type: ReportEntity.User,
reason,
comment: text,
}),
onSuccess: () => {
if (!blockUser) {
displayToast(`🗒️ ${offendingUser.username} has been reported`);
onClose();
}
},
onError: () => {
displayToast(`❌ Failed to report ${offendingUser.username}`);
},
});

const onReportUser = (
e: React.MouseEvent,
reason: ReportReason,
text: string,
) => {
e.preventDefault();
reportUserMutation({ reason, text });
if (blockUser) {
blockUserMutation();
}
};

const isPending = isBlockPending || isReportPending;
const checkboxDisabled = defaultBlockUser || isPending;
return (
<ReasonSelectionModal
isOpen
onReport={onReportUser}
disabled={isPending}
reasons={reportReasons}
onRequestClose={onClose}
heading="Select reason for reporting"
footer={
<Checkbox
name="blockUser"
className="font-normal"
disabled={checkboxDisabled}
onChange={(e) => setBlockUser(e.target.checked)}
checked={blockUser}
>
Block {offendingUser.username}
</Checkbox>
}
/>
);
};

export default ReportUserModal;
1 change: 1 addition & 0 deletions packages/shared/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum ReportEntity {
Post = 'post',
Source = 'source',
Comment = 'comment',
User = 'user',
}

export enum ReportReason {
Expand Down
Loading