2020-03-27 13:59:38 -07:00
import React from 'react' ;
import { connect } from 'react-redux' ;
import { expandHomeTimeline } from '../../actions/timelines' ;
import PropTypes from 'prop-types' ;
import StatusListContainer from '../ui/containers/status_list_container' ;
import Column from '../../components/column' ;
2021-09-17 15:09:37 -07:00
import BundleContainer from 'soapbox/features/ui/containers/bundle_container' ;
2020-03-27 13:59:38 -07:00
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
import { Link } from 'react-router-dom' ;
2021-09-17 15:09:37 -07:00
import { OrderedSet as ImmutableOrderedSet } from 'immutable' ;
import { getFeatures } from 'soapbox/utils/features' ;
2021-09-18 15:05:34 -07:00
function FollowRecommendationsContainer ( ) {
return import ( /* webpackChunkName: "features/follow_recommendations" */ 'soapbox/features/follow_recommendations/components/follow_recommendations_container' ) ;
2021-09-17 15:09:37 -07:00
}
2020-03-27 13:59:38 -07:00
const messages = defineMessages ( {
title : { id : 'column.home' , defaultMessage : 'Home' } ,
} ) ;
2021-09-17 15:09:37 -07:00
const mapStateToProps = state => {
const instance = state . get ( 'instance' ) ;
const features = getFeatures ( instance ) ;
return {
hasUnread : state . getIn ( [ 'timelines' , 'home' , 'unread' ] ) > 0 ,
isPartial : state . getIn ( [ 'timelines' , 'home' , 'isPartial' ] ) ,
siteTitle : state . getIn ( [ 'instance' , 'title' ] ) ,
2021-09-18 15:05:34 -07:00
isLoading : state . getIn ( [ 'timelines' , 'home' , 'isLoading' ] , true ) ,
2021-12-13 11:25:06 -08:00
loadingFailed : state . getIn ( [ 'timelines' , 'home' , 'loadingFailed' ] , false ) ,
2021-09-17 15:09:37 -07:00
isEmpty : state . getIn ( [ 'timelines' , 'home' , 'items' ] , ImmutableOrderedSet ( ) ) . isEmpty ( ) ,
features ,
} ;
} ;
2020-03-27 13:59:38 -07:00
export default @ connect ( mapStateToProps )
@ injectIntl
class HomeTimeline extends React . PureComponent {
static propTypes = {
dispatch : PropTypes . func . isRequired ,
intl : PropTypes . object . isRequired ,
hasUnread : PropTypes . bool ,
isPartial : PropTypes . bool ,
2020-04-14 14:37:17 -07:00
siteTitle : PropTypes . string ,
2021-09-18 15:05:34 -07:00
isLoading : PropTypes . bool ,
2021-12-13 11:25:06 -08:00
loadingFailed : PropTypes . bool ,
2021-09-17 15:09:37 -07:00
isEmpty : PropTypes . bool ,
features : PropTypes . object . isRequired ,
2020-03-27 13:59:38 -07:00
} ;
2021-09-17 15:09:37 -07:00
state = {
done : false ,
}
2020-03-27 13:59:38 -07:00
handleLoadMore = maxId => {
this . props . dispatch ( expandHomeTimeline ( { maxId } ) ) ;
}
2020-04-14 14:47:35 -07:00
componentDidMount ( ) {
2020-03-27 13:59:38 -07:00
this . _checkIfReloadNeeded ( false , this . props . isPartial ) ;
}
2020-04-14 14:47:35 -07:00
componentDidUpdate ( prevProps ) {
2020-03-27 13:59:38 -07:00
this . _checkIfReloadNeeded ( prevProps . isPartial , this . props . isPartial ) ;
}
2020-04-14 14:47:35 -07:00
componentWillUnmount ( ) {
2020-03-27 13:59:38 -07:00
this . _stopPolling ( ) ;
}
2020-04-14 14:47:35 -07:00
_checkIfReloadNeeded ( wasPartial , isPartial ) {
2020-03-27 13:59:38 -07:00
const { dispatch } = this . props ;
if ( wasPartial === isPartial ) {
return ;
} else if ( ! wasPartial && isPartial ) {
this . polling = setInterval ( ( ) => {
dispatch ( expandHomeTimeline ( ) ) ;
} , 3000 ) ;
} else if ( wasPartial && ! isPartial ) {
this . _stopPolling ( ) ;
}
}
2020-04-14 14:47:35 -07:00
_stopPolling ( ) {
2020-03-27 13:59:38 -07:00
if ( this . polling ) {
clearInterval ( this . polling ) ;
this . polling = null ;
}
}
2021-09-17 15:09:37 -07:00
handleDone = e => {
this . props . dispatch ( expandHomeTimeline ( ) ) ;
this . setState ( { done : true } ) ;
}
2021-11-04 11:30:54 -07:00
handleRefresh = ( ) => {
const { dispatch } = this . props ;
return dispatch ( expandHomeTimeline ( ) ) ;
}
2020-04-14 14:47:35 -07:00
render ( ) {
2021-12-13 11:25:06 -08:00
const { intl , siteTitle , isLoading , loadingFailed , isEmpty , features } = this . props ;
2021-09-17 15:09:37 -07:00
const { done } = this . state ;
2021-12-13 11:25:06 -08:00
const showSuggestions = features . suggestions && isEmpty && ! isLoading && ! loadingFailed && ! done ;
2020-03-27 13:59:38 -07:00
return (
2021-10-15 20:05:59 -07:00
< Column label = { intl . formatMessage ( messages . title ) } transparent = { ! showSuggestions } >
2021-10-08 09:59:59 -07:00
{ showSuggestions ? (
2021-09-18 15:05:34 -07:00
< BundleContainer fetchComponent = { FollowRecommendationsContainer } >
{ Component => < Component onDone = { this . handleDone } / > }
< / B u n d l e C o n t a i n e r >
2021-10-15 20:05:59 -07:00
) : (
2021-09-17 15:09:37 -07:00
< StatusListContainer
scrollKey = 'home_timeline'
onLoadMore = { this . handleLoadMore }
2021-11-04 11:30:54 -07:00
onRefresh = { this . handleRefresh }
2021-09-17 15:09:37 -07:00
timelineId = 'home'
emptyMessage = { < FormattedMessage id = 'empty_column.home' defaultMessage = 'Your home timeline is empty! Visit {public} to get started and meet other users.' values = { { public : < Link to = '/timeline/local' > < FormattedMessage id = 'empty_column.home.local_tab' defaultMessage = 'the {site_title} tab' values = { { site _title : siteTitle } } / > < /Link> }} / > }
/ >
2021-10-15 20:05:59 -07:00
) }
2020-03-27 13:59:38 -07:00
< / C o l u m n >
) ;
}
}