闭社主体 forked from https://github.com/tootsuite/mastodon
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.

60 lines
1.3 KiB

  1. import {
  2. SEARCH_CHANGE,
  3. SEARCH_SUGGESTIONS_READY,
  4. SEARCH_RESET
  5. } from '../actions/search';
  6. import Immutable from 'immutable';
  7. const initialState = Immutable.Map({
  8. value: '',
  9. loaded_value: '',
  10. suggestions: []
  11. });
  12. const normalizeSuggestions = (state, value, accounts) => {
  13. let newSuggestions = [
  14. {
  15. title: 'account',
  16. items: accounts.map(item => ({
  17. type: 'account',
  18. id: item.id,
  19. value: item.acct
  20. }))
  21. }
  22. ];
  23. if (value.indexOf('@') === -1) {
  24. newSuggestions.push({
  25. title: 'hashtag',
  26. items: [
  27. {
  28. type: 'hashtag',
  29. id: value,
  30. value: `#${value}`
  31. }
  32. ]
  33. });
  34. }
  35. return state.withMutations(map => {
  36. map.set('suggestions', newSuggestions);
  37. map.set('loaded_value', value);
  38. });
  39. };
  40. export default function search(state = initialState, action) {
  41. switch(action.type) {
  42. case SEARCH_CHANGE:
  43. return state.set('value', action.value);
  44. case SEARCH_SUGGESTIONS_READY:
  45. return normalizeSuggestions(state, action.value, action.accounts);
  46. case SEARCH_RESET:
  47. return state.withMutations(map => {
  48. map.set('suggestions', []);
  49. map.set('value', '');
  50. map.set('loaded_value', '');
  51. });
  52. default:
  53. return state;
  54. }
  55. };