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.

96 lines
2.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import {
  2. SEARCH_CHANGE,
  3. SEARCH_CLEAR,
  4. SEARCH_FETCH_SUCCESS,
  5. SEARCH_SHOW
  6. } from '../actions/search';
  7. import { COMPOSE_MENTION, COMPOSE_REPLY } from '../actions/compose';
  8. import Immutable from 'immutable';
  9. const initialState = Immutable.Map({
  10. value: '',
  11. submitted: false,
  12. hidden: false,
  13. results: Immutable.Map()
  14. });
  15. const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => {
  16. let newSuggestions = [];
  17. if (accounts.length > 0) {
  18. newSuggestions.push({
  19. title: 'account',
  20. items: accounts.map(item => ({
  21. type: 'account',
  22. id: item.id,
  23. value: item.acct
  24. }))
  25. });
  26. }
  27. if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 || hashtags.length > 0) {
  28. let hashtagItems = hashtags.map(item => ({
  29. type: 'hashtag',
  30. id: item,
  31. value: `#${item}`
  32. }));
  33. if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 && !value.startsWith('http://') && !value.startsWith('https://') && hashtags.indexOf(value) === -1) {
  34. hashtagItems.unshift({
  35. type: 'hashtag',
  36. id: value,
  37. value: `#${value}`
  38. });
  39. }
  40. if (hashtagItems.length > 0) {
  41. newSuggestions.push({
  42. title: 'hashtag',
  43. items: hashtagItems
  44. });
  45. }
  46. }
  47. if (statuses.length > 0) {
  48. newSuggestions.push({
  49. title: 'status',
  50. items: statuses.map(item => ({
  51. type: 'status',
  52. id: item.id,
  53. value: item.id
  54. }))
  55. });
  56. }
  57. return state.withMutations(map => {
  58. map.set('suggestions', newSuggestions);
  59. map.set('loaded_value', value);
  60. });
  61. };
  62. export default function search(state = initialState, action) {
  63. switch(action.type) {
  64. case SEARCH_CHANGE:
  65. return state.set('value', action.value);
  66. case SEARCH_CLEAR:
  67. return state.withMutations(map => {
  68. map.set('value', '');
  69. map.set('results', Immutable.Map());
  70. map.set('submitted', false);
  71. map.set('hidden', false);
  72. });
  73. case SEARCH_SHOW:
  74. return state.set('hidden', false);
  75. case COMPOSE_REPLY:
  76. case COMPOSE_MENTION:
  77. return state.set('hidden', true);
  78. case SEARCH_FETCH_SUCCESS:
  79. return state.set('results', Immutable.Map({
  80. accounts: Immutable.List(action.results.accounts.map(item => item.id)),
  81. statuses: Immutable.List(action.results.statuses.map(item => item.id)),
  82. hashtags: Immutable.List(action.results.hashtags)
  83. })).set('submitted', true);
  84. default:
  85. return state;
  86. }
  87. };