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 Button from 'soapbox/components/button' ;
import { OrderedSet as ImmutableOrderedSet } from 'immutable' ;
import { getFeatures } from 'soapbox/utils/features' ;
function FollowRecommendationsList ( ) {
return import ( /* webpackChunkName: "features/follow_recommendations" */ 'soapbox/features/follow_recommendations/components/follow_recommendations_list' ) ;
}
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' ] ) ,
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-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 } ) ;
}
renderFollowRecommendations = ( ) => {
return (
< div className = 'scrollable follow-recommendations-container' >
< div className = 'column-title' >
< h3 > < FormattedMessage id = 'follow_recommendations.heading' defaultMessage = "Follow people you'd like to see posts from! Here are some suggestions." / > < / h 3 >
< p > < FormattedMessage id = 'follow_recommendations.lead' defaultMessage = "Posts from people you follow will show up in chronological order on your home feed. Don't be afraid to make mistakes, you can unfollow people just as easily any time!" / > < / p >
< / d i v >
< BundleContainer fetchComponent = { FollowRecommendationsList } >
{ Component => < Component / > }
< / B u n d l e C o n t a i n e r >
< div className = 'column-actions' >
< Button onClick = { this . handleDone } >
< FormattedMessage id = 'follow_recommendations.done' defaultMessage = 'Done' / >
< / B u t t o n >
< / d i v >
< / d i v >
) ;
}
2020-04-14 14:47:35 -07:00
render ( ) {
2021-09-17 15:26:01 -07:00
const { intl , siteTitle , isEmpty , features } = this . props ;
2021-09-17 15:09:37 -07:00
const { done } = this . state ;
2020-03-27 13:59:38 -07:00
return (
< Column label = { intl . formatMessage ( messages . title ) } >
2021-09-17 15:09:37 -07:00
{ ( features . suggestions && isEmpty && ! done ) ? (
this . renderFollowRecommendations ( )
) : (
< StatusListContainer
scrollKey = 'home_timeline'
onLoadMore = { this . handleLoadMore }
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> }} / > }
/ >
) }
2020-03-27 13:59:38 -07:00
< / C o l u m n >
) ;
}
}