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.

123 lines
3.8 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  5. import Toggle from 'react-toggle';
  6. import AsyncSelect from 'react-select/async';
  7. import { NonceProvider } from 'react-select';
  8. import SettingToggle from '../../notifications/components/setting_toggle';
  9. const messages = defineMessages({
  10. placeholder: { id: 'hashtag.column_settings.select.placeholder', defaultMessage: 'Enter hashtags…' },
  11. noOptions: { id: 'hashtag.column_settings.select.no_options_message', defaultMessage: 'No suggestions found' },
  12. });
  13. export default @injectIntl
  14. class ColumnSettings extends React.PureComponent {
  15. static propTypes = {
  16. settings: ImmutablePropTypes.map.isRequired,
  17. onChange: PropTypes.func.isRequired,
  18. onLoad: PropTypes.func.isRequired,
  19. intl: PropTypes.object.isRequired,
  20. };
  21. state = {
  22. open: this.hasTags(),
  23. };
  24. hasTags () {
  25. return ['all', 'any', 'none'].map(mode => this.tags(mode).length > 0).includes(true);
  26. }
  27. tags (mode) {
  28. let tags = this.props.settings.getIn(['tags', mode]) || [];
  29. if (tags.toJSON) {
  30. return tags.toJSON();
  31. } else {
  32. return tags;
  33. }
  34. };
  35. onSelect = mode => value => this.props.onChange(['tags', mode], value);
  36. onToggle = () => {
  37. if (this.state.open && this.hasTags()) {
  38. this.props.onChange('tags', {});
  39. }
  40. this.setState({ open: !this.state.open });
  41. };
  42. noOptionsMessage = () => this.props.intl.formatMessage(messages.noOptions);
  43. modeSelect (mode) {
  44. return (
  45. <div className='column-settings__row'>
  46. <span className='column-settings__section'>
  47. {this.modeLabel(mode)}
  48. </span>
  49. <NonceProvider nonce={document.querySelector('meta[name=style-nonce]').content} cacheKey='tags'>
  50. <AsyncSelect
  51. isMulti
  52. autoFocus
  53. value={this.tags(mode)}
  54. onChange={this.onSelect(mode)}
  55. loadOptions={this.props.onLoad}
  56. className='column-select__container'
  57. classNamePrefix='column-select'
  58. name='tags'
  59. placeholder={this.props.intl.formatMessage(messages.placeholder)}
  60. noOptionsMessage={this.noOptionsMessage}
  61. />
  62. </NonceProvider>
  63. </div>
  64. );
  65. }
  66. modeLabel (mode) {
  67. switch(mode) {
  68. case 'any':
  69. return <FormattedMessage id='hashtag.column_settings.tag_mode.any' defaultMessage='Any of these' />;
  70. case 'all':
  71. return <FormattedMessage id='hashtag.column_settings.tag_mode.all' defaultMessage='All of these' />;
  72. case 'none':
  73. return <FormattedMessage id='hashtag.column_settings.tag_mode.none' defaultMessage='None of these' />;
  74. default:
  75. return '';
  76. }
  77. };
  78. render () {
  79. const { settings, onChange } = this.props;
  80. return (
  81. <div>
  82. <div className='column-settings__row'>
  83. <div className='setting-toggle'>
  84. <Toggle id='hashtag.column_settings.tag_toggle' onChange={this.onToggle} checked={this.state.open} />
  85. <span className='setting-toggle__label'>
  86. <FormattedMessage id='hashtag.column_settings.tag_toggle' defaultMessage='Include additional tags in this column' />
  87. </span>
  88. </div>
  89. </div>
  90. {this.state.open && (
  91. <div className='column-settings__hashtags'>
  92. {this.modeSelect('any')}
  93. {this.modeSelect('all')}
  94. {this.modeSelect('none')}
  95. </div>
  96. )}
  97. <div className='column-settings__row'>
  98. <SettingToggle settings={settings} settingPath={['local']} onChange={onChange} label={<FormattedMessage id='community.column_settings.local_only' defaultMessage='Local only' />} />
  99. </div>
  100. </div>
  101. );
  102. }
  103. }