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.

123 lines
2.9 KiB

  1. /*
  2. `reducers/local_settings`
  3. ========================
  4. > For more information on the contents of this file, please contact:
  5. >
  6. > - kibigo! [@kibi@glitch.social]
  7. This file provides our Redux reducers related to local settings. The
  8. associated actions are:
  9. - __`STORE_HYDRATE` :__
  10. Used to hydrate the store with its initial values.
  11. - __`LOCAL_SETTING_CHANGE` :__
  12. Used to change the value of a local setting in the store.
  13. */
  14. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  15. /*
  16. Imports:
  17. --------
  18. */
  19. // Package imports //
  20. import { Map as ImmutableMap } from 'immutable';
  21. // Mastodon imports //
  22. import { STORE_HYDRATE } from '../../mastodon/actions/store';
  23. // Our imports //
  24. import { LOCAL_SETTING_CHANGE } from '../actions/local_settings';
  25. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  26. /*
  27. initialState:
  28. -------------
  29. You can see the default values for all of our local settings here.
  30. These are only used if no previously-saved values exist.
  31. */
  32. const initialState = ImmutableMap({
  33. layout : 'auto',
  34. stretch : true,
  35. collapsed : ImmutableMap({
  36. enabled : true,
  37. auto : ImmutableMap({
  38. all : false,
  39. notifications : true,
  40. lengthy : true,
  41. replies : false,
  42. media : false,
  43. }),
  44. backgrounds : ImmutableMap({
  45. user_backgrounds : false,
  46. preview_images : false,
  47. }),
  48. }),
  49. media : ImmutableMap({
  50. letterbox : true,
  51. fullwidth : true,
  52. }),
  53. });
  54. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  55. /*
  56. Helper functions:
  57. -----------------
  58. ### `hydrate(state, localSettings)`
  59. `hydrate()` is used to hydrate the `local_settings` part of our store
  60. with its initial values. The `state` will probably just be the
  61. `initialState`, and the `localSettings` should be whatever we pulled
  62. from `localStorage`.
  63. */
  64. const hydrate = (state, localSettings) => state.mergeDeep(localSettings);
  65. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  66. /*
  67. `localSettings(state = initialState, action)`:
  68. ----------------------------------------------
  69. This function holds our actual reducer.
  70. If our action is `STORE_HYDRATE`, then we call `hydrate()` with the
  71. `local_settings` property of the provided `action.state`.
  72. If our action is `LOCAL_SETTING_CHANGE`, then we set `action.key` in
  73. our state to the provided `action.value`. Note that `action.key` MUST
  74. be an array, since we use `setIn()`.
  75. > __Note :__
  76. > We call this function `localSettings`, but its associated object
  77. > in the store is `local_settings`.
  78. */
  79. export default function localSettings(state = initialState, action) {
  80. switch(action.type) {
  81. case STORE_HYDRATE:
  82. return hydrate(state, action.state.get('local_settings'));
  83. case LOCAL_SETTING_CHANGE:
  84. return state.setIn(action.key, action.value);
  85. default:
  86. return state;
  87. }
  88. };