2017-10-16 13:02:39 +09:00
import React from 'react' ;
import { connect } from 'react-redux' ;
import PropTypes from 'prop-types' ;
2017-12-04 16:26:40 +09:00
import StatusListContainer from 'flavours/glitch/features/ui/containers/status_list_container' ;
import Column from 'flavours/glitch/components/column' ;
import ColumnHeader from 'flavours/glitch/components/column_header' ;
2018-05-31 21:13:35 +09:00
import { expandDirectTimeline } from 'flavours/glitch/actions/timelines' ;
2019-06-09 19:07:23 +09:00
import { mountConversations , unmountConversations , expandConversations } from 'flavours/glitch/actions/conversations' ;
2017-12-04 16:26:40 +09:00
import { addColumn , removeColumn , moveColumn } from 'flavours/glitch/actions/columns' ;
2017-10-16 13:02:39 +09:00
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
import ColumnSettingsContainer from './containers/column_settings_container' ;
2017-12-04 16:26:40 +09:00
import { connectDirectStream } from 'flavours/glitch/actions/streaming' ;
2019-06-09 19:07:23 +09:00
import { changeSetting } from 'flavours/glitch/actions/settings' ;
import ConversationsListContainer from './containers/conversations_list_container' ;
2017-10-16 13:02:39 +09:00
const messages = defineMessages ( {
title : { id : 'column.direct' , defaultMessage : 'Direct messages' } ,
} ) ;
const mapStateToProps = state => ( {
hasUnread : state . getIn ( [ 'timelines' , 'direct' , 'unread' ] ) > 0 ,
2019-06-09 19:07:23 +09:00
conversationsMode : state . getIn ( [ 'settings' , 'direct' , 'conversations' ] ) ,
2017-10-16 13:02:39 +09:00
} ) ;
2019-09-09 22:16:08 +09:00
export default @ connect ( mapStateToProps )
2017-10-16 13:02:39 +09:00
@ injectIntl
2019-09-09 22:16:08 +09:00
class DirectTimeline extends React . PureComponent {
2017-10-16 13:02:39 +09:00
static propTypes = {
dispatch : PropTypes . func . isRequired ,
columnId : PropTypes . string ,
intl : PropTypes . object . isRequired ,
hasUnread : PropTypes . bool ,
multiColumn : PropTypes . bool ,
2019-06-09 19:07:23 +09:00
conversationsMode : PropTypes . bool ,
2017-10-16 13:02:39 +09:00
} ;
handlePin = ( ) => {
const { columnId , dispatch } = this . props ;
if ( columnId ) {
dispatch ( removeColumn ( columnId ) ) ;
} else {
dispatch ( addColumn ( 'DIRECT' , { } ) ) ;
}
}
handleMove = ( dir ) => {
const { columnId , dispatch } = this . props ;
dispatch ( moveColumn ( columnId , dir ) ) ;
}
handleHeaderClick = ( ) => {
this . column . scrollTop ( ) ;
}
componentDidMount ( ) {
2019-06-09 19:07:23 +09:00
const { dispatch , conversationsMode } = this . props ;
dispatch ( mountConversations ( ) ) ;
if ( conversationsMode ) {
dispatch ( expandConversations ( ) ) ;
} else {
dispatch ( expandDirectTimeline ( ) ) ;
}
2017-10-16 13:02:39 +09:00
this . disconnect = dispatch ( connectDirectStream ( ) ) ;
}
2019-06-09 19:07:23 +09:00
componentDidUpdate ( prevProps ) {
const { dispatch , conversationsMode } = this . props ;
if ( prevProps . conversationsMode && ! conversationsMode ) {
dispatch ( expandDirectTimeline ( ) ) ;
} else if ( ! prevProps . conversationsMode && conversationsMode ) {
dispatch ( expandConversations ( ) ) ;
}
}
2017-10-16 13:02:39 +09:00
componentWillUnmount ( ) {
2019-06-09 19:07:23 +09:00
this . props . dispatch ( unmountConversations ( ) ) ;
2017-10-16 13:02:39 +09:00
if ( this . disconnect ) {
this . disconnect ( ) ;
this . disconnect = null ;
}
}
setRef = c => {
this . column = c ;
}
2019-06-09 19:07:23 +09:00
handleLoadMoreTimeline = maxId => {
2018-05-31 21:13:35 +09:00
this . props . dispatch ( expandDirectTimeline ( { maxId } ) ) ;
2017-10-16 13:02:39 +09:00
}
2019-06-09 19:07:23 +09:00
handleLoadMoreConversations = maxId => {
this . props . dispatch ( expandConversations ( { maxId } ) ) ;
}
handleTimelineClick = ( ) => {
this . props . dispatch ( changeSetting ( [ 'direct' , 'conversations' ] , false ) ) ;
}
handleConversationsClick = ( ) => {
this . props . dispatch ( changeSetting ( [ 'direct' , 'conversations' ] , true ) ) ;
}
2017-10-16 13:02:39 +09:00
render ( ) {
2019-06-09 19:07:23 +09:00
const { intl , hasUnread , columnId , multiColumn , conversationsMode } = this . props ;
2017-10-16 13:02:39 +09:00
const pinned = ! ! columnId ;
2019-06-09 19:07:23 +09:00
let contents ;
if ( conversationsMode ) {
contents = (
< ConversationsListContainer
trackScroll = { ! pinned }
scrollKey = { ` direct_timeline- ${ columnId } ` }
timelineId = 'direct'
onLoadMore = { this . 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." / > }
/ >
) ;
} else {
contents = (
< StatusListContainer
trackScroll = { ! pinned }
scrollKey = { ` direct_timeline- ${ columnId } ` }
timelineId = 'direct'
onLoadMore = { this . handleLoadMoreTimeline }
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." / > }
/ >
) ;
}
2017-10-16 13:02:39 +09:00
return (
2019-08-02 02:17:17 +09:00
< Column bindToDocument = { ! multiColumn } ref = { this . setRef } label = { intl . formatMessage ( messages . title ) } >
2017-10-16 13:02:39 +09:00
< ColumnHeader
icon = 'envelope'
active = { hasUnread }
title = { intl . formatMessage ( messages . title ) }
onPin = { this . handlePin }
onMove = { this . handleMove }
onClick = { this . handleHeaderClick }
pinned = { pinned }
multiColumn = { multiColumn }
>
< ColumnSettingsContainer / >
< / C o l u m n H e a d e r >
2019-06-09 19:07:23 +09:00
< div className = 'notification__filter-bar' >
< button
className = { conversationsMode ? 'active' : '' }
onClick = { this . handleConversationsClick }
>
< FormattedMessage
id = 'direct.conversations_mode'
defaultMessage = 'Conversations'
/ >
< / b u t t o n >
< button
className = { conversationsMode ? '' : 'active' }
onClick = { this . handleTimelineClick }
>
< FormattedMessage
id = 'direct.timeline_mode'
defaultMessage = 'Timeline'
/ >
< / b u t t o n >
< / d i v >
{ contents }
2017-10-16 13:02:39 +09:00
< / C o l u m n >
) ;
}
}