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.

78 lines
2.5 KiB

  1. import { connect } from 'react-redux';
  2. import ComposeForm from '../components/compose_form';
  3. import { uploadCompose } from '../../../actions/compose';
  4. import { createSelector } from 'reselect';
  5. import {
  6. changeCompose,
  7. submitCompose,
  8. clearComposeSuggestions,
  9. fetchComposeSuggestions,
  10. selectComposeSuggestion,
  11. changeComposeSpoilerText,
  12. insertEmojiCompose
  13. } from '../../../actions/compose';
  14. const getMentionedUsernames = createSelector(state => state.getIn(['compose', 'text']), text => text.match(/(?:^|[^\/\w])@([a-z0-9_]+@[a-z0-9\.\-]+)/ig));
  15. const getMentionedDomains = createSelector(getMentionedUsernames, mentionedUsernamesWithDomains => {
  16. return mentionedUsernamesWithDomains !== null ? [...new Set(mentionedUsernamesWithDomains.map(item => item.split('@')[2]))] : [];
  17. });
  18. const mapStateToProps = (state, props) => {
  19. const mentionedUsernames = getMentionedUsernames(state);
  20. const mentionedUsernamesWithDomains = getMentionedDomains(state);
  21. return {
  22. text: state.getIn(['compose', 'text']),
  23. suggestion_token: state.getIn(['compose', 'suggestion_token']),
  24. suggestions: state.getIn(['compose', 'suggestions']),
  25. spoiler: state.getIn(['compose', 'spoiler']),
  26. spoiler_text: state.getIn(['compose', 'spoiler_text']),
  27. privacy: state.getIn(['compose', 'privacy']),
  28. focusDate: state.getIn(['compose', 'focusDate']),
  29. preselectDate: state.getIn(['compose', 'preselectDate']),
  30. is_submitting: state.getIn(['compose', 'is_submitting']),
  31. is_uploading: state.getIn(['compose', 'is_uploading']),
  32. me: state.getIn(['compose', 'me']),
  33. needsPrivacyWarning: (state.getIn(['compose', 'privacy']) === 'private' || state.getIn(['compose', 'privacy']) === 'direct') && mentionedUsernames !== null,
  34. mentionedDomains: mentionedUsernamesWithDomains
  35. };
  36. };
  37. const mapDispatchToProps = (dispatch) => ({
  38. onChange (text) {
  39. dispatch(changeCompose(text));
  40. },
  41. onSubmit () {
  42. dispatch(submitCompose());
  43. },
  44. onClearSuggestions () {
  45. dispatch(clearComposeSuggestions());
  46. },
  47. onFetchSuggestions (token) {
  48. dispatch(fetchComposeSuggestions(token));
  49. },
  50. onSuggestionSelected (position, token, accountId) {
  51. dispatch(selectComposeSuggestion(position, token, accountId));
  52. },
  53. onChangeSpoilerText (checked) {
  54. dispatch(changeComposeSpoilerText(checked));
  55. },
  56. onPaste (files) {
  57. dispatch(uploadCompose(files));
  58. },
  59. onPickEmoji (position, data) {
  60. dispatch(insertEmojiCompose(position, data));
  61. },
  62. });
  63. export default connect(mapStateToProps, mapDispatchToProps)(ComposeForm);