~cytrogen/masto-fe

ref: 20a89f1c8eefa0f766a4650be4d2e5f1fa92ee71 masto-fe/app/javascript/flavours/glitch/actions/store.js -rw-r--r-- 1.4 KiB
20a89f1c — Cytrogen [feature] Bookmark folders UI 9 days ago
                                                                                
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
import { Iterable, fromJS } from "immutable";

import { hydrateCompose } from "./compose";
import { importFetchedAccounts } from "./importer";
import { hydrateSearch } from "./search";
import { saveSettings } from "./settings";

export const STORE_HYDRATE = "STORE_HYDRATE";
export const STORE_HYDRATE_LAZY = "STORE_HYDRATE_LAZY";

const convertState = rawState =>
  fromJS(rawState, (k, v) =>
    Iterable.isIndexed(v) ? v.toList() : v.toMap());

const applyMigrations = (state) => {
  return state.withMutations(state => {
    // Migrate glitch-soc local-only “Show unread marker” setting to Mastodon's setting
    if (state.getIn(["local_settings", "notifications", "show_unread"]) !== undefined) {
      // Only change if the Mastodon setting does not deviate from default
      if (state.getIn(["settings", "notifications", "showUnread"]) !== false) {
        state.setIn(["settings", "notifications", "showUnread"], state.getIn(["local_settings", "notifications", "show_unread"]));
      }
      state.removeIn(["local_settings", "notifications", "show_unread"]);
    }
  });
};

export function hydrateStore(rawState) {
  return dispatch => {
    const state = applyMigrations(convertState(rawState));

    dispatch({
      type: STORE_HYDRATE,
      state,
    });

    dispatch(hydrateCompose());
    dispatch(hydrateSearch());
    dispatch(importFetchedAccounts(Object.values(rawState.accounts)));
    dispatch(saveSettings());
  };
}