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.

124 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. navbar_under : false,
  36. collapsed : ImmutableMap({
  37. enabled : true,
  38. auto : ImmutableMap({
  39. all : false,
  40. notifications : true,
  41. lengthy : true,
  42. replies : false,
  43. media : false,
  44. }),
  45. backgrounds : ImmutableMap({
  46. user_backgrounds : false,
  47. preview_images : false,
  48. }),
  49. }),
  50. media : ImmutableMap({
  51. letterbox : true,
  52. fullwidth : true,
  53. }),
  54. });
  55. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  56. /*
  57. Helper functions:
  58. -----------------
  59. ### `hydrate(state, localSettings)`
  60. `hydrate()` is used to hydrate the `local_settings` part of our store
  61. with its initial values. The `state` will probably just be the
  62. `initialState`, and the `localSettings` should be whatever we pulled
  63. from `localStorage`.
  64. */
  65. const hydrate = (state, localSettings) => state.mergeDeep(localSettings);
  66. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  67. /*
  68. `localSettings(state = initialState, action)`:
  69. ----------------------------------------------
  70. This function holds our actual reducer.
  71. If our action is `STORE_HYDRATE`, then we call `hydrate()` with the
  72. `local_settings` property of the provided `action.state`.
  73. If our action is `LOCAL_SETTING_CHANGE`, then we set `action.key` in
  74. our state to the provided `action.value`. Note that `action.key` MUST
  75. be an array, since we use `setIn()`.
  76. > __Note :__
  77. > We call this function `localSettings`, but its associated object
  78. > in the store is `local_settings`.
  79. */
  80. export default function localSettings(state = initialState, action) {
  81. switch(action.type) {
  82. case STORE_HYDRATE:
  83. return hydrate(state, action.state.get('local_settings'));
  84. case LOCAL_SETTING_CHANGE:
  85. return state.setIn(action.key, action.value);
  86. default:
  87. return state;
  88. }
  89. };