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.

127 lines
3.4 KiB

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