about summary refs log tree commit diff
path: root/app/javascript/glitch/reducers/local_settings.js
blob: 813e130ca24e94631a2eb0861f2fac41eb350f8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*

`reducers/local_settings`
========================

>   For more information on the contents of this file, please contact:
>
>   - kibigo! [@kibi@glitch.social]

This file provides our Redux reducers related to local settings. The
associated actions are:

 -  __`STORE_HYDRATE` :__
    Used to hydrate the store with its initial values.

 -  __`LOCAL_SETTING_CHANGE` :__
    Used to change the value of a local setting in the store.

*/

//  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/*

Imports:
--------

*/

//  Package imports  //
import { Map as ImmutableMap } from 'immutable';

//  Mastodon imports  //
import { STORE_HYDRATE } from '../../mastodon/actions/store';

//  Our imports  //
import { LOCAL_SETTING_CHANGE } from '../actions/local_settings';

//  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/*

initialState:
-------------

You can see the default values for all of our local settings here.
These are only used if no previously-saved values exist.

*/

const initialState = ImmutableMap({
  layout    : 'auto',
  stretch   : true,
  navbar_under : false,
  side_arm  : 'none',
  collapsed : ImmutableMap({
    enabled     : true,
    auto        : ImmutableMap({
      all              : false,
      notifications    : true,
      lengthy          : true,
      replies          : false,
      media            : false,
    }),
    backgrounds : ImmutableMap({
      user_backgrounds : false,
      preview_images   : false,
    }),
  }),
  media     : ImmutableMap({
    letterbox   : true,
    fullwidth   : true,
  }),
});

//  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/*

Helper functions:
-----------------

###  `hydrate(state, localSettings)`

`hydrate()` is used to hydrate the `local_settings` part of our store
with its initial values. The `state` will probably just be the
`initialState`, and the `localSettings` should be whatever we pulled
from `localStorage`.

*/

const hydrate = (state, localSettings) => state.mergeDeep(localSettings);

//  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/*

`localSettings(state = initialState, action)`:
----------------------------------------------

This function holds our actual reducer.

If our action is `STORE_HYDRATE`, then we call `hydrate()` with the
`local_settings` property of the provided `action.state`.

If our action is `LOCAL_SETTING_CHANGE`, then we set `action.key` in
our state to the provided `action.value`. Note that `action.key` MUST
be an array, since we use `setIn()`.

>   __Note :__
>   We call this function `localSettings`, but its associated object
>   in the store is `local_settings`.

*/

export default function localSettings(state = initialState, action) {
  switch(action.type) {
  case STORE_HYDRATE:
    return hydrate(state, action.state.get('local_settings'));
  case LOCAL_SETTING_CHANGE:
    return state.setIn(action.key, action.value);
  default:
    return state;
  }
};