import React, { useMemo } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useHistory } from 'react-router-dom'; import { openModal } from 'soapbox/actions/modals'; import RelativeTimestamp from 'soapbox/components/relative-timestamp'; import { Avatar, HStack, Stack, Text } from 'soapbox/components/ui'; import VerificationBadge from 'soapbox/components/verification_badge'; import DropdownMenuContainer from 'soapbox/containers/dropdown_menu_container'; import { useChatContext } from 'soapbox/contexts/chat-context'; import { useAppDispatch, useFeatures } from 'soapbox/hooks'; import { IChat, useChatActions } from 'soapbox/queries/chats'; import type { Menu } from 'soapbox/components/dropdown_menu'; const messages = defineMessages({ leaveMessage: { id: 'chat_settings.leave.message', defaultMessage: 'Are you sure you want to leave this chat? Messages will be deleted for you and this chat will be removed from your inbox.' }, leaveHeading: { id: 'chat_settings.leave.heading', defaultMessage: 'Leave Chat' }, leaveConfirm: { id: 'chat_settings.leave.confirm', defaultMessage: 'Leave Chat' }, leaveChat: { id: 'chat_settings.options.leave_chat', defaultMessage: 'Leave Chat' }, }); interface IChatListItemInterface { chat: IChat, onClick: (chat: any) => void, } const ChatListItem: React.FC = ({ chat, onClick }) => { const dispatch = useAppDispatch(); const intl = useIntl(); const features = useFeatures(); const history = useHistory(); const { isUsingMainChatPage } = useChatContext(); const { deleteChat } = useChatActions(chat?.id as string); const menu = useMemo((): Menu => [{ text: intl.formatMessage(messages.leaveChat), action: (event) => { event.stopPropagation(); dispatch(openModal('CONFIRM', { heading: intl.formatMessage(messages.leaveHeading), message: intl.formatMessage(messages.leaveMessage), confirm: intl.formatMessage(messages.leaveConfirm), confirmationTheme: 'primary', onConfirm: () => { deleteChat.mutate(undefined, { onSuccess() { if (isUsingMainChatPage) { history.push('/chats'); } }, }); }, })); }, icon: require('@tabler/icons/logout.svg'), }], []); return ( // eslint-disable-next-line jsx-a11y/interactive-supports-focus
onClick(chat)} className='group px-2 py-3 w-full flex flex-col rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 focus:shadow-inset-ring' data-testid='chat-list-item' >
{chat.account?.display_name || `@${chat.account.username}`} {chat.account?.verified && }
{chat.last_message?.content && ( )}
{features.chatsDelete && (
{/* TODO: fix nested buttons here */}
)} {chat.last_message && ( <> {chat.last_message.unread && (
)} )}
); }; export default ChatListItem;