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.

153 lines
4.1 KiB

  1. import { SETTING_CHANGE, SETTING_SAVE } from '../actions/settings';
  2. import { NOTIFICATIONS_FILTER_SET } from '../actions/notifications';
  3. import { COLUMN_ADD, COLUMN_REMOVE, COLUMN_MOVE, COLUMN_PARAMS_CHANGE } from '../actions/columns';
  4. import { STORE_HYDRATE } from '../actions/store';
  5. import { EMOJI_USE } from '../actions/emojis';
  6. import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists';
  7. import { Map as ImmutableMap, fromJS } from 'immutable';
  8. import uuid from '../uuid';
  9. const initialState = ImmutableMap({
  10. saved: true,
  11. onboarded: false,
  12. skinTone: 1,
  13. home: ImmutableMap({
  14. shows: ImmutableMap({
  15. reblog: true,
  16. reply: true,
  17. }),
  18. regex: ImmutableMap({
  19. body: '',
  20. }),
  21. }),
  22. notifications: ImmutableMap({
  23. alerts: ImmutableMap({
  24. follow: true,
  25. favourite: true,
  26. reblog: true,
  27. mention: true,
  28. poll: true,
  29. }),
  30. quickFilter: ImmutableMap({
  31. active: 'all',
  32. show: true,
  33. advanced: false,
  34. }),
  35. shows: ImmutableMap({
  36. follow: true,
  37. favourite: true,
  38. reblog: true,
  39. mention: true,
  40. poll: true,
  41. }),
  42. sounds: ImmutableMap({
  43. follow: true,
  44. favourite: true,
  45. reblog: true,
  46. mention: true,
  47. poll: true,
  48. }),
  49. }),
  50. community: ImmutableMap({
  51. regex: ImmutableMap({
  52. body: '',
  53. }),
  54. }),
  55. public: ImmutableMap({
  56. regex: ImmutableMap({
  57. body: '',
  58. }),
  59. }),
  60. direct: ImmutableMap({
  61. regex: ImmutableMap({
  62. body: '',
  63. }),
  64. }),
  65. trends: ImmutableMap({
  66. show: true,
  67. }),
  68. });
  69. const defaultColumns = fromJS([
  70. { id: 'COMPOSE', uuid: uuid(), params: {} },
  71. { id: 'HOME', uuid: uuid(), params: {} },
  72. { id: 'NOTIFICATIONS', uuid: uuid(), params: {} },
  73. ]);
  74. const hydrate = (state, settings) => state.mergeDeep(settings).update('columns', (val = defaultColumns) => val);
  75. const moveColumn = (state, uuid, direction) => {
  76. const columns = state.get('columns');
  77. const index = columns.findIndex(item => item.get('uuid') === uuid);
  78. const newIndex = index + direction;
  79. let newColumns;
  80. newColumns = columns.splice(index, 1);
  81. newColumns = newColumns.splice(newIndex, 0, columns.get(index));
  82. return state
  83. .set('columns', newColumns)
  84. .set('saved', false);
  85. };
  86. const changeColumnParams = (state, uuid, path, value) => {
  87. const columns = state.get('columns');
  88. const index = columns.findIndex(item => item.get('uuid') === uuid);
  89. const newColumns = columns.update(index, column => column.updateIn(['params', ...path], () => value));
  90. return state
  91. .set('columns', newColumns)
  92. .set('saved', false);
  93. };
  94. const updateFrequentEmojis = (state, emoji) => state.update('frequentlyUsedEmojis', ImmutableMap(), map => map.update(emoji.id, 0, count => count + 1)).set('saved', false);
  95. const filterDeadListColumns = (state, listId) => state.update('columns', columns => columns.filterNot(column => column.get('id') === 'LIST' && column.get('params').get('id') === listId));
  96. export default function settings(state = initialState, action) {
  97. switch(action.type) {
  98. case STORE_HYDRATE:
  99. return hydrate(state, action.state.get('settings'));
  100. case NOTIFICATIONS_FILTER_SET:
  101. case SETTING_CHANGE:
  102. return state
  103. .setIn(action.path, action.value)
  104. .set('saved', false);
  105. case COLUMN_ADD:
  106. return state
  107. .update('columns', list => list.push(fromJS({ id: action.id, uuid: uuid(), params: action.params })))
  108. .set('saved', false);
  109. case COLUMN_REMOVE:
  110. return state
  111. .update('columns', list => list.filterNot(item => item.get('uuid') === action.uuid))
  112. .set('saved', false);
  113. case COLUMN_MOVE:
  114. return moveColumn(state, action.uuid, action.direction);
  115. case COLUMN_PARAMS_CHANGE:
  116. return changeColumnParams(state, action.uuid, action.path, action.value);
  117. case EMOJI_USE:
  118. return updateFrequentEmojis(state, action.emoji);
  119. case SETTING_SAVE:
  120. return state.set('saved', true);
  121. case LIST_FETCH_FAIL:
  122. return action.error.response.status === 404 ? filterDeadListColumns(state, action.id) : state;
  123. case LIST_DELETE_SUCCESS:
  124. return filterDeadListColumns(state, action.id);
  125. default:
  126. return state;
  127. }
  128. };