import type { FC } from '../../lib/teact/teact'; import React, { memo, useCallback, useState } from '../../lib/teact/teact'; import { withGlobal, getActions } from '../../global'; import type { ApiChat, ApiChatSettings, ApiUser } from '../../api/types'; import { selectChat, selectUser } from '../../global/selectors'; import { getChatTitle, getUserFirstOrLastName, getUserFullName, isChatBasicGroup, isUserId, } from '../../global/helpers'; import buildClassName from '../../util/buildClassName'; import useLang from '../../hooks/useLang'; import useFlag from '../../hooks/useFlag'; import Button from '../ui/Button'; import ConfirmDialog from '../ui/ConfirmDialog'; import Checkbox from '../ui/Checkbox'; import './ChatReportPanel.scss'; type OwnProps = { chatId: string; className?: string; settings?: ApiChatSettings; }; type StateProps = { currentUserId?: string; chat?: ApiChat; user?: ApiUser; }; const ChatReportPanel: FC = ({ chatId, className, chat, user, settings, currentUserId, }) => { const { openAddContactDialog, blockContact, reportSpam, deleteChat, leaveChannel, deleteChatUser, deleteHistory, toggleChatArchived, hideChatReportPanel, } = getActions(); const lang = useLang(); const [isBlockUserModalOpen, openBlockUserModal, closeBlockUserModal] = useFlag(); const [shouldReportSpam, setShouldReportSpam] = useState(true); const [shouldDeleteChat, setShouldDeleteChat] = useState(true); const { accessHash } = chat || {}; const { isAutoArchived, canReportSpam, canAddContact, canBlockContact, } = settings || {}; const isBasicGroup = chat && isChatBasicGroup(chat); const handleAddContact = useCallback(() => { openAddContactDialog({ userId: chatId }); if (isAutoArchived) { toggleChatArchived({ id: chatId }); } }, [openAddContactDialog, isAutoArchived, toggleChatArchived, chatId]); const handleConfirmBlock = useCallback(() => { closeBlockUserModal(); blockContact({ contactId: chatId, accessHash: accessHash! }); if (canReportSpam && shouldReportSpam) { reportSpam({ chatId }); } if (shouldDeleteChat) { deleteChat({ chatId }); } }, [ accessHash, blockContact, closeBlockUserModal, deleteChat, reportSpam, canReportSpam, shouldDeleteChat, shouldReportSpam, chatId, ]); const handleCloseReportPanel = useCallback(() => { hideChatReportPanel({ chatId }); }, [chatId, hideChatReportPanel]); const handleChatReportSpam = useCallback(() => { closeBlockUserModal(); reportSpam({ chatId }); if (isBasicGroup) { deleteChatUser({ chatId, userId: currentUserId! }); deleteHistory({ chatId, shouldDeleteForAll: false }); } else { leaveChannel({ chatId }); } }, [ chatId, closeBlockUserModal, currentUserId, deleteChatUser, deleteHistory, isBasicGroup, leaveChannel, reportSpam, ]); if (!settings) { return undefined; } return (
{canAddContact && ( )} {canBlockContact && ( )} {canReportSpam && !canBlockContact && ( )} {user && ( )} {user && canReportSpam && ( )}
); }; export default memo(withGlobal( (global, { chatId }): StateProps => ({ currentUserId: global.currentUserId, chat: selectChat(global, chatId), user: isUserId(chatId) ? selectUser(global, chatId) : undefined, }), )(ChatReportPanel));