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.

111 lines
2.7 KiB

  1. import { connect } from 'react-redux';
  2. import Status from '../components/status';
  3. import { makeGetStatus } from '../selectors';
  4. import {
  5. replyCompose,
  6. mentionCompose
  7. } from '../actions/compose';
  8. import {
  9. reblog,
  10. favourite,
  11. unreblog,
  12. unfavourite
  13. } from '../actions/interactions';
  14. import { blockAccount } from '../actions/accounts';
  15. import { deleteStatus } from '../actions/statuses';
  16. import { openMedia } from '../actions/modal';
  17. import { createSelector } from 'reselect'
  18. import { isMobile } from '../is_mobile'
  19. const mapStateToProps = (state, props) => ({
  20. statusBase: state.getIn(['statuses', props.id]),
  21. me: state.getIn(['meta', 'me'])
  22. });
  23. const makeMapStateToPropsInner = () => {
  24. const getStatus = (() => {
  25. return createSelector(
  26. [
  27. (_, base) => base,
  28. (state, base) => (base ? state.getIn(['accounts', base.get('account')]) : null),
  29. (state, base) => (base ? state.getIn(['statuses', base.get('reblog')], null) : null)
  30. ],
  31. (base, account, reblog) => (base ? base.set('account', account).set('reblog', reblog) : null)
  32. );
  33. })();
  34. const mapStateToProps = (state, { statusBase }) => ({
  35. status: getStatus(state, statusBase)
  36. });
  37. return mapStateToProps;
  38. };
  39. const makeMapStateToPropsLast = () => {
  40. const getStatus = (() => {
  41. return createSelector(
  42. [
  43. (_, status) => status,
  44. (state, status) => (status ? state.getIn(['accounts', status.getIn(['reblog', 'account'])], null) : null)
  45. ],
  46. (status, reblogAccount) => (status && status.get('reblog') ? status.setIn(['reblog', 'account'], reblogAccount) : status)
  47. );
  48. })();
  49. const mapStateToProps = (state, { status }) => ({
  50. status: getStatus(state, status)
  51. });
  52. return mapStateToProps;
  53. };
  54. const mapDispatchToProps = (dispatch) => ({
  55. onReply (status, router) {
  56. dispatch(replyCompose(status, router));
  57. },
  58. onReblog (status) {
  59. if (status.get('reblogged')) {
  60. dispatch(unreblog(status));
  61. } else {
  62. dispatch(reblog(status));
  63. }
  64. },
  65. onFavourite (status) {
  66. if (status.get('favourited')) {
  67. dispatch(unfavourite(status));
  68. } else {
  69. dispatch(favourite(status));
  70. }
  71. },
  72. onDelete (status) {
  73. dispatch(deleteStatus(status.get('id')));
  74. },
  75. onMention (account, router) {
  76. dispatch(mentionCompose(account));
  77. if (isMobile(window.innerWidth)) {
  78. router.push('/statuses/new');
  79. }
  80. },
  81. onOpenMedia (url) {
  82. dispatch(openMedia(url));
  83. },
  84. onBlock (account) {
  85. dispatch(blockAccount(account.get('id')));
  86. }
  87. });
  88. export default connect(mapStateToProps, mapDispatchToProps)(
  89. connect(makeMapStateToPropsInner)(
  90. connect(makeMapStateToPropsLast)(Status)
  91. )
  92. );