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.

148 lines
4.1 KiB

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