Optimize how statuses are re-rendered and relative time intervals

This commit is contained in:
Eugen Rochko
2016-11-04 12:48:53 +01:00
parent 6d26bfd147
commit 98c3a5e9c3
6 changed files with 91 additions and 33 deletions

View File

@ -21,35 +21,28 @@ moment.updateLocale('en', {
const RelativeTimestamp = React.createClass({
getInitialState () {
return {
text: ''
};
},
propTypes: {
timestamp: React.PropTypes.string.isRequired
timestamp: React.PropTypes.string.isRequired,
now: React.PropTypes.any
},
mixins: [PureRenderMixin],
componentWillMount () {
this._updateMomentText();
this.interval = setInterval(this._updateMomentText, 60000);
},
componentWillUnmount () {
clearInterval(this.interval);
},
_updateMomentText () {
this.setState({ text: moment(this.props.timestamp).fromNow() });
},
render () {
const timestamp = moment(this.props.timestamp);
const now = this.props.now;
let string = '';
if (timestamp.isAfter(now)) {
string = 'Just now';
} else {
string = timestamp.from(now);
}
return (
<span>
{this.state.text}
{string}
</span>
);
}

View File

@ -22,7 +22,8 @@ const Status = React.createClass({
onReblog: React.PropTypes.func,
onDelete: React.PropTypes.func,
onOpenMedia: React.PropTypes.func,
me: React.PropTypes.number
me: React.PropTypes.number,
now: React.PropTypes.any
},
mixins: [PureRenderMixin],
@ -43,7 +44,7 @@ const Status = React.createClass({
render () {
let media = '';
let { status, ...other } = this.props;
const { status, now, ...other } = this.props;
if (status === null) {
return <div />;
@ -80,7 +81,7 @@ const Status = React.createClass({
<div style={{ padding: '8px 10px', paddingLeft: '68px', position: 'relative', minHeight: '48px', borderBottom: '1px solid #363c4b', cursor: 'pointer' }} onClick={this.handleClick}>
<div style={{ fontSize: '15px' }}>
<div style={{ float: 'right', fontSize: '14px' }}>
<a href={status.get('url')} className='status__relative-time' style={{ color: '#616b86' }} target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} /></a>
<a href={status.get('url')} className='status__relative-time' style={{ color: '#616b86' }} target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} now={now} /></a>
</div>
<a onClick={this.handleAccountClick.bind(this, status.getIn(['account', 'id']))} href={status.getIn(['account', 'url'])} className='status__display-name' style={{ display: 'block', maxWidth: '100%', paddingRight: '25px', color: '#616b86' }}>

View File

@ -3,6 +3,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import { ScrollContainer } from 'react-router-scroll';
import StatusContainer from '../containers/status_container';
import moment from 'moment';
const StatusList = React.createClass({
@ -18,8 +19,22 @@ const StatusList = React.createClass({
};
},
getInitialState () {
return {
now: moment()
};
},
mixins: [PureRenderMixin],
componentDidMount () {
this._interval = setInterval(() => this.setState({ now: moment() }), 60000);
},
componentWillUnmount () {
clearInterval(this._interval);
},
handleScroll (e) {
const { scrollTop, scrollHeight, clientHeight } = e.target;
@ -35,7 +50,7 @@ const StatusList = React.createClass({
<div style={{ overflowY: 'scroll', flex: '1 1 auto', overflowX: 'hidden' }} className='scrollable' onScroll={this.handleScroll}>
<div>
{statusIds.map((statusId) => {
return <StatusContainer key={statusId} id={statusId} />;
return <StatusContainer key={statusId} id={statusId} now={this.state.now} />;
})}
</div>
</div>