2022-09-23 06:04:01 -07:00
import React , { useMemo } from 'react' ;
import { defineMessages , useIntl } from 'react-intl' ;
2022-11-03 12:29:53 -07:00
import { useHistory } from 'react-router-dom' ;
2022-09-12 13:46:19 -07:00
2022-09-23 06:04:01 -07:00
import { openModal } from 'soapbox/actions/modals' ;
2022-09-12 13:46:19 -07:00
import RelativeTimestamp from 'soapbox/components/relative-timestamp' ;
2022-10-25 05:23:33 -07:00
import { Avatar , HStack , Stack , Text } from 'soapbox/components/ui' ;
2022-09-12 13:46:19 -07:00
import VerificationBadge from 'soapbox/components/verification_badge' ;
2022-09-23 06:04:01 -07:00
import DropdownMenuContainer from 'soapbox/containers/dropdown_menu_container' ;
2022-11-03 12:29:53 -07:00
import { useChatContext } from 'soapbox/contexts/chat-context' ;
2022-11-02 12:14:16 -07:00
import { useAppDispatch , useFeatures } from 'soapbox/hooks' ;
2022-10-25 05:23:33 -07:00
import { IChat , useChatActions } from 'soapbox/queries/chats' ;
2022-09-12 13:46:19 -07:00
2022-09-23 06:04:01 -07:00
import type { Menu } from 'soapbox/components/dropdown_menu' ;
const messages = defineMessages ( {
2022-09-27 07:18:12 -07:00
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.' } ,
2022-09-23 06:04:01 -07:00
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' } ,
} ) ;
2022-09-12 13:46:19 -07:00
interface IChatListItemInterface {
chat : IChat ,
onClick : ( chat : any ) = > void ,
}
2022-10-25 05:23:33 -07:00
const ChatListItem : React.FC < IChatListItemInterface > = ( { chat , onClick } ) = > {
2022-09-23 06:04:01 -07:00
const dispatch = useAppDispatch ( ) ;
const intl = useIntl ( ) ;
2022-11-02 12:14:16 -07:00
const features = useFeatures ( ) ;
2022-11-03 12:29:53 -07:00
const history = useHistory ( ) ;
2022-09-23 06:04:01 -07:00
2022-11-03 12:29:53 -07:00
const { isUsingMainChatPage } = useChatContext ( ) ;
2022-09-28 13:20:59 -07:00
const { deleteChat } = useChatActions ( chat ? . id as string ) ;
2022-09-23 06:04:01 -07:00
2022-10-25 05:23:33 -07:00
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' ,
2022-11-03 12:29:53 -07:00
onConfirm : ( ) = > {
deleteChat . mutate ( undefined , {
onSuccess() {
if ( isUsingMainChatPage ) {
history . push ( '/chats' ) ;
}
} ,
} ) ;
} ,
2022-10-25 05:23:33 -07:00
} ) ) ;
} ,
icon : require ( '@tabler/icons/logout.svg' ) ,
} ] , [ ] ) ;
2022-09-23 06:04:01 -07:00
2022-09-12 13:46:19 -07:00
return (
2022-09-29 10:54:18 -07:00
// eslint-disable-next-line jsx-a11y/interactive-supports-focus
< div
role = 'button'
2022-09-12 13:46:19 -07:00
key = { chat . id }
onClick = { ( ) = > onClick ( chat ) }
2022-09-23 06:04:01 -07:00
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'
2022-10-03 08:03:43 -07:00
data - testid = 'chat-list-item'
2022-09-12 13:46:19 -07:00
>
< HStack alignItems = 'center' justifyContent = 'between' space = { 2 } className = 'w-full' >
< HStack alignItems = 'center' space = { 2 } className = 'overflow-hidden' >
< Avatar src = { chat . account ? . avatar } size = { 40 } className = 'flex-none' / >
< Stack alignItems = 'start' className = 'overflow-hidden' >
< div className = 'flex items-center space-x-1 flex-grow w-full' >
< Text weight = 'bold' size = 'sm' align = 'left' truncate > { chat . account ? . display_name || ` @ ${ chat . account . username } ` } < / Text >
{ chat . account ? . verified && < VerificationBadge / > }
< / div >
{ chat . last_message ? . content && (
< Text
align = 'left'
size = 'sm'
weight = 'medium'
2022-09-23 06:08:46 -07:00
theme = { chat . last_message . unread ? 'default' : 'muted' }
2022-09-12 13:46:19 -07:00
truncate
2022-09-22 12:58:31 -07:00
className = 'w-full h-5 truncate-child pointer-events-none'
2022-09-12 13:46:19 -07:00
data - testid = 'chat-last-message'
dangerouslySetInnerHTML = { { __html : chat.last_message?.content } }
/ >
) }
< / Stack >
< / HStack >
2022-09-16 05:57:09 -07:00
< HStack alignItems = 'center' space = { 2 } >
2022-11-02 12:14:16 -07:00
{ features . chatsDelete && (
< div className = 'text-gray-600 hidden group-hover:block hover:text-gray-100' >
{ /* TODO: fix nested buttons here */ }
< DropdownMenuContainer
items = { menu }
src = { require ( '@tabler/icons/dots.svg' ) }
title = 'Settings'
/ >
< / div >
) }
2022-09-23 06:04:01 -07:00
2022-09-16 05:57:09 -07:00
{ chat . last_message && (
< >
{ chat . last_message . unread && (
< div
className = 'w-2 h-2 rounded-full bg-secondary-500'
data - testid = 'chat-unread-indicator'
/ >
) }
2022-09-12 13:46:19 -07:00
2022-09-16 05:57:09 -07:00
< RelativeTimestamp
timestamp = { chat . last_message . created_at }
align = 'right'
size = 'xs'
truncate
/ >
< / >
) }
< / HStack >
2022-09-12 13:46:19 -07:00
< / HStack >
2022-09-29 10:54:18 -07:00
< / div >
2022-09-12 13:46:19 -07:00
) ;
} ;
export default ChatListItem ;