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.

85 lines
2.5 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import ImmutablePureComponent from 'react-immutable-pure-component';
  4. import StatusContainer from 'flavours/glitch/containers/status_container';
  5. import LoadMore from 'flavours/glitch/components/load_more';
  6. const mapStateToProps = (state, { id }) => {
  7. let status = state.getIn(['statuses', id]);
  8. let reblogId = status.get('reblog'); // reblog here is a id
  9. return {
  10. childrenWithGrandchildren: state.getIn(['contexts', 'replies', reblogId || id], []).map((cid) => ({
  11. cid,
  12. grandchildrenIds: state.getIn(['contexts', 'replies', cid], []),
  13. })),
  14. quotedId: reblogId && state.getIn(['statuses', reblogId, 'in_reply_to_id']),
  15. };
  16. };
  17. const N = 3;
  18. export default @connect(mapStateToProps)
  19. class StatusWithComments extends ImmutablePureComponent {
  20. state = {
  21. showAll: false,
  22. };
  23. show = () => this.setState({ showAll: true });
  24. render() {
  25. const { id, childrenWithGrandchildren, quotedId, ...other } = this.props;
  26. const { showAll } = this.state;
  27. const loadMore = (
  28. <LoadMore
  29. visible
  30. onClick={this.show}
  31. />
  32. );
  33. return (
  34. <div className='status-with-comments'>
  35. {quotedId ? (
  36. <div className='status__quoted-status'>
  37. <StatusContainer
  38. id={quotedId}
  39. />
  40. </div>
  41. ) : null}
  42. <StatusContainer
  43. id={id}
  44. {...other}
  45. />
  46. {childrenWithGrandchildren.size > 0 && (
  47. <div className='status__comments'>
  48. {childrenWithGrandchildren
  49. .filter((_, idx) => showAll || idx < N)
  50. .map(({ cid, grandchildrenIds }) => (
  51. <>
  52. <StatusContainer
  53. key={`comment-${cid}`}
  54. id={cid}
  55. />
  56. {grandchildrenIds.size > 0 && (
  57. <div className='subcomments'>
  58. {grandchildrenIds
  59. .filter((_, idx) => showAll || idx < N)
  60. .map((gid) => (
  61. <StatusContainer
  62. key={`subcomment-${gid}`}
  63. id={gid}
  64. />
  65. ))}
  66. {!showAll && grandchildrenIds.size > N && loadMore}
  67. </div>
  68. )}
  69. </>
  70. ))}
  71. {!showAll && childrenWithGrandchildren.size > N && loadMore}
  72. </div>
  73. ) }
  74. </div>
  75. );
  76. }
  77. }