pleroma/app/soapbox/features/home_timeline/index.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

import { OrderedSet as ImmutableOrderedSet } from 'immutable';
2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
2020-03-27 13:59:38 -07:00
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
2020-03-27 13:59:38 -07:00
import { Link } from 'react-router-dom';
import { getFeatures } from 'soapbox/utils/features';
import { expandHomeTimeline } from '../../actions/timelines';
2022-03-21 11:09:01 -07:00
import { Column } from '../../components/ui';
2022-06-03 10:31:23 -07:00
import Timeline from '../ui/components/timeline';
2020-03-27 13:59:38 -07:00
const messages = defineMessages({
title: { id: 'column.home', defaultMessage: 'Home' },
});
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']),
isLoading: state.getIn(['timelines', 'home', 'isLoading'], true),
loadingFailed: state.getIn(['timelines', 'home', 'loadingFailed'], false),
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,
isLoading: PropTypes.bool,
loadingFailed: PropTypes.bool,
isEmpty: PropTypes.bool,
features: PropTypes.object.isRequired,
2020-03-27 13:59:38 -07:00
};
state = {
done: false,
}
2020-03-27 13:59:38 -07:00
handleLoadMore = maxId => {
this.props.dispatch(expandHomeTimeline({ maxId }));
}
componentDidMount() {
2020-03-27 13:59:38 -07:00
this._checkIfReloadNeeded(false, this.props.isPartial);
}
componentDidUpdate(prevProps) {
2020-03-27 13:59:38 -07:00
this._checkIfReloadNeeded(prevProps.isPartial, this.props.isPartial);
}
componentWillUnmount() {
2020-03-27 13:59:38 -07:00
this._stopPolling();
}
_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();
}
}
_stopPolling() {
2020-03-27 13:59:38 -07:00
if (this.polling) {
clearInterval(this.polling);
this.polling = null;
}
}
2021-11-04 11:30:54 -07:00
handleRefresh = () => {
const { dispatch } = this.props;
return dispatch(expandHomeTimeline());
}
render() {
2022-06-04 13:31:24 -07:00
const { intl, siteTitle } = this.props;
2020-03-27 13:59:38 -07:00
return (
2022-06-04 13:31:24 -07:00
<Column label={intl.formatMessage(messages.title)}>
<Timeline
scrollKey='home_timeline'
onLoadMore={this.handleLoadMore}
onRefresh={this.handleRefresh}
timelineId='home'
divideType='space'
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
</Column>
);
}
}