2022-08-31 10:24:01 -07:00
import classNames from 'clsx' ;
2022-08-17 12:48:04 -07:00
import React from 'react' ;
2022-09-14 07:35:32 -07:00
import { defineMessages , useIntl } from 'react-intl' ;
2022-08-17 12:48:04 -07:00
import { openModal } from 'soapbox/actions/modals' ;
2022-09-30 07:22:55 -07:00
import Link from 'soapbox/components/link' ;
2022-08-17 12:48:04 -07:00
import { Avatar , Button , HStack , Icon , Stack , Text } from 'soapbox/components/ui' ;
import { useChatContext } from 'soapbox/contexts/chat-context' ;
import { useAppDispatch } from 'soapbox/hooks' ;
2022-09-28 13:20:59 -07:00
import { useChatActions } from 'soapbox/queries/chats' ;
2022-08-17 12:48:04 -07:00
2022-09-14 07:35:32 -07:00
const messages = defineMessages ( {
leaveChatHeading : { id : 'chat_message_list_intro.leave_chat.heading' , defaultMessage : 'Leave Chat' } ,
2022-09-27 07:18:12 -07:00
leaveChatMessage : { id : 'chat_message_list_intro.leave_chat.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-14 07:35:32 -07:00
leaveChatConfirm : { id : 'chat_message_list_intro.leave_chat.confirm' , defaultMessage : 'Leave Chat' } ,
intro : { id : 'chat_message_list_intro.intro' , defaultMessage : 'wants to start a chat with you' } ,
accept : { id : 'chat_message_list_intro.actions.accept' , defaultMessage : 'Accept' } ,
leaveChat : { id : 'chat_message_list_intro.actions.leave_chat' , defaultMessage : 'Leave chat' } ,
report : { id : 'chat_message_list_intro.actions.report' , defaultMessage : 'Report' } ,
messageLifespan : { id : 'chat_message_list_intro.actions.message_lifespan' , defaultMessage : 'Messages older than 15 days are deleted.' } ,
} ) ;
2022-08-17 12:48:04 -07:00
const ChatMessageListIntro = ( ) = > {
const dispatch = useAppDispatch ( ) ;
2022-09-14 07:35:32 -07:00
const intl = useIntl ( ) ;
2022-08-17 12:48:04 -07:00
const { chat , needsAcceptance } = useChatContext ( ) ;
2022-09-28 13:20:59 -07:00
const { acceptChat , deleteChat } = useChatActions ( chat ? . id as string ) ;
2022-08-17 12:48:04 -07:00
const handleLeaveChat = ( ) = > {
dispatch ( openModal ( 'CONFIRM' , {
2022-09-14 07:35:32 -07:00
heading : intl.formatMessage ( messages . leaveChatHeading ) ,
message : intl.formatMessage ( messages . leaveChatMessage ) ,
confirm : intl.formatMessage ( messages . leaveChatConfirm ) ,
2022-08-17 12:48:04 -07:00
confirmationTheme : 'primary' ,
2022-09-14 07:35:32 -07:00
onConfirm : ( ) = > deleteChat . mutate ( ) ,
2022-08-17 12:48:04 -07:00
} ) ) ;
} ;
if ( ! chat ) {
return null ;
}
return (
< Stack
2022-09-12 11:42:15 -07:00
data - testid = 'chat-message-list-intro'
2022-08-17 12:48:04 -07:00
justifyContent = 'center'
alignItems = 'center'
space = { 4 }
className = {
classNames ( {
'w-3/4 mx-auto' : needsAcceptance ,
'mt-6' : true ,
} )
}
>
< Stack alignItems = 'center' space = { 2 } >
2022-09-30 07:22:55 -07:00
< Link to = { ` @ ${ chat . account . acct } ` } >
< Avatar src = { chat . account . avatar_static } size = { 75 } / >
< / Link >
2022-08-17 12:48:04 -07:00
< Text size = 'lg' align = 'center' >
{ needsAcceptance ? (
< >
< Text tag = 'span' weight = 'semibold' > @ { chat . account . acct } < / Text >
{ ' ' }
2022-09-14 07:35:32 -07:00
< Text tag = 'span' > { intl . formatMessage ( messages . intro ) } < / Text >
2022-08-17 12:48:04 -07:00
< / >
) : (
2022-09-30 07:22:55 -07:00
< Link to = { ` @ ${ chat . account . acct } ` } >
< Text tag = 'span' theme = 'inherit' weight = 'semibold' > @ { chat . account . acct } < / Text >
< / Link >
2022-08-17 12:48:04 -07:00
) }
< / Text >
< / Stack >
{ needsAcceptance ? (
2022-10-04 08:00:07 -07:00
< HStack alignItems = 'center' space = { 2 } className = 'w-full' >
2022-08-17 12:48:04 -07:00
< Button
theme = 'primary'
block
2022-09-14 07:35:32 -07:00
onClick = { ( ) = > acceptChat . mutate ( ) }
2022-08-17 12:48:04 -07:00
disabled = { acceptChat . isLoading }
>
2022-09-14 07:35:32 -07:00
{ intl . formatMessage ( messages . accept ) }
2022-08-17 12:48:04 -07:00
< / Button >
2022-10-04 08:00:07 -07:00
< Button
theme = 'danger'
block
onClick = { handleLeaveChat }
>
{ intl . formatMessage ( messages . leaveChat ) }
< / Button >
< / HStack >
2022-08-17 12:48:04 -07:00
) : (
< HStack justifyContent = 'center' alignItems = 'center' space = { 1 } className = 'flex-shrink-0' >
< Icon src = { require ( '@tabler/icons/clock.svg' ) } className = 'text-gray-600 w-4 h-4' / >
< Text size = 'sm' theme = 'muted' >
2022-09-14 07:35:32 -07:00
{ intl . formatMessage ( messages . messageLifespan ) }
2022-08-17 12:48:04 -07:00
< / Text >
< / HStack >
) }
< / Stack >
) ;
} ;
export default ChatMessageListIntro ;