You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.3 KiB

  1. import { connect } from 'react-redux';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import { fetchStatus } from '../../actions/statuses';
  5. import Immutable from 'immutable';
  6. import EmbeddedStatus from '../../components/status';
  7. function selectStatus(state, id) {
  8. let status = state.getIn(['timelines', 'statuses', id]);
  9. status = status.set('account', state.getIn(['timelines', 'accounts', status.get('account')]));
  10. if (status.get('reblog') !== null) {
  11. status = status.set('reblog', selectStatus(state, status.get('reblog')));
  12. }
  13. return status;
  14. };
  15. function selectStatuses(state, ids) {
  16. return ids.map(id => selectStatus(state, id));
  17. };
  18. const mapStateToProps = (state, props) => ({
  19. status: selectStatus(state, Number(props.params.statusId)),
  20. ancestors: selectStatuses(state, state.getIn(['timelines', 'ancestors', Number(props.params.statusId)], Immutable.List())),
  21. descendants: selectStatuses(state, state.getIn(['timelines', 'descendants', Number(props.params.statusId)], Immutable.List()))
  22. });
  23. const Status = React.createClass({
  24. propTypes: {
  25. params: React.PropTypes.object.isRequired,
  26. dispatch: React.PropTypes.func.isRequired,
  27. status: ImmutablePropTypes.map,
  28. ancestors: ImmutablePropTypes.list.isRequired,
  29. descendants: ImmutablePropTypes.list.isRequired
  30. },
  31. mixins: [PureRenderMixin],
  32. componentWillMount () {
  33. this.props.dispatch(fetchStatus(this.props.params.statusId));
  34. },
  35. componentWillReceiveProps (nextProps) {
  36. if (nextProps.params.statusId !== this.props.params.statusId && nextProps.params.statusId) {
  37. this.props.dispatch(fetchStatus(nextProps.params.statusId));
  38. }
  39. },
  40. renderChildren (list) {
  41. return list.map(s => <EmbeddedStatus status={s} key={s.get('id')} />);
  42. },
  43. render () {
  44. const { status, ancestors, descendants } = this.props;
  45. if (status === null) {
  46. return <div>Loading {this.props.params.statusId}...</div>;
  47. }
  48. return (
  49. <div>
  50. {this.renderChildren(ancestors)}
  51. <EmbeddedStatus status={status} />
  52. {this.renderChildren(descendants)}
  53. </div>
  54. );
  55. }
  56. });
  57. export default connect(mapStateToProps)(Status);