2022-05-30 09:18:31 -07:00
import React , { useEffect } from 'react' ;
import { defineMessages , FormattedMessage , useIntl } from 'react-intl' ;
import { directComposeById } from 'soapbox/actions/compose' ;
import { connectDirectStream } from 'soapbox/actions/streaming' ;
import { expandDirectTimeline } from 'soapbox/actions/timelines' ;
2022-11-15 06:10:14 -08:00
import AccountSearch from 'soapbox/components/account-search' ;
import ColumnHeader from 'soapbox/components/column-header' ;
2022-05-30 09:18:31 -07:00
import { Column } from 'soapbox/components/ui' ;
import { useAppDispatch , useAppSelector } from 'soapbox/hooks' ;
2022-06-03 10:31:23 -07:00
import Timeline from '../ui/components/timeline' ;
2022-05-30 09:18:31 -07:00
const messages = defineMessages ( {
title : { id : 'column.direct' , defaultMessage : 'Direct messages' } ,
searchPlaceholder : { id : 'direct.search_placeholder' , defaultMessage : 'Send a message to…' } ,
} ) ;
const DirectTimeline = ( ) = > {
const intl = useIntl ( ) ;
const dispatch = useAppDispatch ( ) ;
2022-06-23 15:33:23 -07:00
const hasUnread = useAppSelector ( ( state ) = > ( state . timelines . get ( 'direct' ) ? . unread || 0 ) > 0 ) ;
2022-05-30 09:18:31 -07:00
useEffect ( ( ) = > {
dispatch ( expandDirectTimeline ( ) ) ;
const disconnect = dispatch ( connectDirectStream ( ) ) ;
return ( ( ) = > {
disconnect ( ) ;
} ) ;
} , [ ] ) ;
const handleSuggestion = ( accountId : string ) = > {
dispatch ( directComposeById ( accountId ) ) ;
} ;
const handleLoadMore = ( maxId : string ) = > {
dispatch ( expandDirectTimeline ( { maxId } ) ) ;
} ;
return (
2022-07-15 15:03:21 -07:00
< Column label = { intl . formatMessage ( messages . title ) } transparent withHeader = { false } >
2022-05-30 09:18:31 -07:00
< ColumnHeader
icon = 'envelope'
active = { hasUnread }
title = { intl . formatMessage ( messages . title ) }
/ >
< AccountSearch
placeholder = { intl . formatMessage ( messages . searchPlaceholder ) }
onSelected = { handleSuggestion }
/ >
2022-06-03 10:31:23 -07:00
< Timeline
2022-05-30 09:18:31 -07:00
scrollKey = 'direct_timeline'
timelineId = 'direct'
onLoadMore = { handleLoadMore }
emptyMessage = { < FormattedMessage id = 'empty_column.direct' defaultMessage = "You don't have any direct messages yet. When you send or receive one, it will show up here." / > }
divideType = 'space'
/ >
< / Column >
) ;
} ;
export default DirectTimeline ;