~cytrogen/masto-fe

ref: 20a89f1c8eefa0f766a4650be4d2e5f1fa92ee71 masto-fe/app/javascript/flavours/glitch/actions/local_settings.js -rw-r--r-- 2.0 KiB
20a89f1c — Cytrogen [feature] Bookmark folders UI 8 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
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
import { disableSwiping } from "flavours/glitch/initial_state";

import { openModal } from "./modal";

export const LOCAL_SETTING_CHANGE = "LOCAL_SETTING_CHANGE";
export const LOCAL_SETTING_DELETE = "LOCAL_SETTING_DELETE";

export function checkDeprecatedLocalSettings() {
  return (dispatch, getState) => {
    const local_swipe_to_change_columns = getState().getIn(["local_settings", "swipe_to_change_columns"]);
    let changed_settings = [];

    if (local_swipe_to_change_columns !== null && local_swipe_to_change_columns !== undefined) {
      if (local_swipe_to_change_columns === !disableSwiping) {
        dispatch(deleteLocalSetting(["swipe_to_change_columns"]));
      } else {
        changed_settings.push("user_setting_disable_swiping");
      }
    }

    if (changed_settings.length > 0) {
      dispatch(openModal({
        modalType: "DEPRECATED_SETTINGS",
        modalProps: {
          settings: changed_settings,
          onConfirm: () => dispatch(clearDeprecatedLocalSettings()),
        },
      }));
    }
  };
}

export function clearDeprecatedLocalSettings() {
  return (dispatch) => {
    dispatch(deleteLocalSetting(["content_warnings", "auto_unfold"]));
    dispatch(deleteLocalSetting(["swipe_to_change_columns"]));
  };
}

export function changeLocalSetting(key, value) {
  return dispatch => {
    dispatch({
      type: LOCAL_SETTING_CHANGE,
      key,
      value,
    });

    dispatch(saveLocalSettings());
  };
}

export function deleteLocalSetting(key) {
  return dispatch => {
    dispatch({
      type: LOCAL_SETTING_DELETE,
      key,
    });

    dispatch(saveLocalSettings());
  };
}

//  __TODO :__
//  Right now `saveLocalSettings()` doesn't keep track of which user
//  is currently signed in, but it might be better to give each user
//  their *own* local settings.
export function saveLocalSettings() {
  return (_, getState) => {
    const localSettings = getState().get("local_settings").toJS();
    localStorage.setItem("mastodon-settings", JSON.stringify(localSettings));
  };
}