2022-08-16 13:35:06 -07:00
import React , { useRef } from 'react' ;
2022-10-26 10:08:02 -07:00
import { defineMessages , useIntl } from 'react-intl' ;
2022-09-30 07:22:55 -07:00
import { Link } from 'react-router-dom' ;
2022-06-17 12:39:53 -07:00
2022-11-07 08:53:40 -08:00
import { Avatar , HStack , Icon , Stack , Text , Tooltip } from 'soapbox/components/ui' ;
2022-08-10 05:38:49 -07:00
import VerificationBadge from 'soapbox/components/verification_badge' ;
2022-11-02 12:28:16 -07:00
import { ChatWidgetScreens , useChatContext } from 'soapbox/contexts/chat-context' ;
2022-10-26 10:08:02 -07:00
import { secondsToDays } from 'soapbox/utils/numbers' ;
2022-06-17 12:39:53 -07:00
2022-10-17 05:34:19 -07:00
import Chat from '../chat' ;
2022-08-10 05:38:49 -07:00
import ChatPaneHeader from './chat-pane-header' ;
2022-08-17 06:58:46 -07:00
import ChatSettings from './chat-settings' ;
2022-06-17 12:39:53 -07:00
2022-10-26 10:08:02 -07:00
const messages = defineMessages ( {
autoDeleteMessage : { id : 'chat_window.auto_delete_label' , defaultMessage : 'Auto-delete after {day} days' } ,
2022-11-07 08:53:40 -08:00
autoDeleteMessageTooltip : { id : 'chat_window.auto_delete_tooltip' , defaultMessage : 'Chat messages are set to auto-delete after {day} days days upon sending.' } ,
2022-10-26 10:08:02 -07:00
} ) ;
2022-09-30 07:22:55 -07:00
const LinkWrapper = ( { enabled , to , children } : { enabled : boolean , to : string , children : React.ReactNode } ) : JSX . Element = > {
if ( ! enabled ) {
return < > { children } < / > ;
}
return (
< Link to = { to } >
{ children }
< / Link >
) ;
} ;
2022-06-17 12:39:53 -07:00
/** Floating desktop chat window. */
2022-08-16 13:35:06 -07:00
const ChatWindow = ( ) = > {
2022-10-26 10:08:02 -07:00
const intl = useIntl ( ) ;
2022-11-02 12:28:16 -07:00
const { chat , currentChatId , screen , changeScreen , isOpen , needsAcceptance , toggleChatPane } = useChatContext ( ) ;
2022-08-16 13:35:06 -07:00
2022-10-17 08:50:33 -07:00
const inputRef = useRef < HTMLTextAreaElement | null > ( null ) ;
2022-08-16 13:35:06 -07:00
2022-11-02 12:28:16 -07:00
const closeChat = ( ) = > {
changeScreen ( ChatWidgetScreens . INBOX ) ;
} ;
2022-08-17 06:58:46 -07:00
2022-09-09 07:24:25 -07:00
const openSearch = ( ) = > {
2022-08-16 13:35:06 -07:00
toggleChatPane ( ) ;
2022-11-02 12:28:16 -07:00
changeScreen ( ChatWidgetScreens . SEARCH ) ;
2022-08-16 13:35:06 -07:00
} ;
2022-11-02 12:28:16 -07:00
const openChatSettings = ( ) = > {
changeScreen ( ChatWidgetScreens . CHAT_SETTINGS , currentChatId ) ;
} ;
2022-08-17 06:58:46 -07:00
2022-08-17 12:48:04 -07:00
const secondaryAction = ( ) = > {
if ( needsAcceptance ) {
return undefined ;
}
2022-09-09 07:24:25 -07:00
return isOpen ? openChatSettings : openSearch ;
2022-08-17 12:48:04 -07:00
} ;
2022-06-17 12:39:53 -07:00
if ( ! chat ) return null ;
2022-11-02 12:28:16 -07:00
if ( screen === ChatWidgetScreens . CHAT_SETTINGS ) {
2022-08-17 06:58:46 -07:00
return < ChatSettings / > ;
}
2022-06-17 12:39:53 -07:00
return (
2022-08-10 05:38:49 -07:00
< >
< ChatPaneHeader
title = {
< HStack alignItems = 'center' space = { 2 } >
2022-08-16 13:35:06 -07:00
{ isOpen && (
< button onClick = { closeChat } >
< Icon
src = { require ( '@tabler/icons/arrow-left.svg' ) }
className = 'h-6 w-6 text-gray-600 dark:text-gray-400'
/ >
< / button >
) }
2022-08-10 05:38:49 -07:00
< HStack alignItems = 'center' space = { 3 } >
2022-08-16 13:35:06 -07:00
{ isOpen && (
2022-11-07 07:04:02 -08:00
< Link to = { ` /@ ${ chat . account . acct } ` } >
2022-09-30 07:22:55 -07:00
< Avatar src = { chat . account . avatar } size = { 40 } / >
< / Link >
2022-08-16 13:35:06 -07:00
) }
2022-08-10 05:38:49 -07:00
2022-11-07 08:53:40 -08:00
< Stack alignItems = 'start' >
< LinkWrapper enabled = { isOpen } to = { ` /@ ${ chat . account . acct } ` } >
2022-09-30 07:22:55 -07:00
< div className = 'flex items-center space-x-1 flex-grow' >
2022-11-04 07:43:19 -07:00
< Text size = 'sm' weight = 'bold' truncate > { chat . account . display_name || ` @ ${ chat . account . acct } ` } < / Text >
2022-09-30 07:22:55 -07:00
{ chat . account . verified && < VerificationBadge / > }
< / div >
2022-11-07 08:53:40 -08:00
< / LinkWrapper >
{ chat . message_expiration && (
< Tooltip
text = { intl . formatMessage ( messages . autoDeleteMessageTooltip , { day : secondsToDays ( chat . message_expiration ) } ) }
>
< Text size = 'sm' weight = 'medium' theme = 'primary' truncate className = 'cursor-help' >
2022-11-02 11:53:41 -07:00
{ intl . formatMessage ( messages . autoDeleteMessage , { day : secondsToDays ( chat . message_expiration ) } ) }
< / Text >
2022-11-07 08:53:40 -08:00
< / Tooltip >
) }
< / Stack >
2022-08-10 05:38:49 -07:00
< / HStack >
< / HStack >
}
2022-08-17 12:48:04 -07:00
secondaryAction = { secondaryAction ( ) }
2022-08-17 06:58:46 -07:00
secondaryActionIcon = { isOpen ? require ( '@tabler/icons/info-circle.svg' ) : require ( '@tabler/icons/edit.svg' ) }
2022-08-16 13:35:06 -07:00
isToggleable = { ! isOpen }
isOpen = { isOpen }
onToggle = { toggleChatPane }
2022-08-10 05:38:49 -07:00
/ >
< Stack className = 'overflow-hidden flex-grow h-full' space = { 2 } >
2022-10-17 08:50:33 -07:00
< Chat chat = { chat } inputRef = { inputRef } / >
2022-08-10 05:38:49 -07:00
< / Stack >
< / >
2022-06-17 12:39:53 -07:00
) ;
} ;
export default ChatWindow ;