aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/util/preferences.js
blob: 229b6f17ccb94d9645f41699f2f957a7e8b3f882 (plain)
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
import { useSelector } from 'react-redux';

const containsProperty = (object, key) => object.hasOwnProperty(key) && object[key] !== null;

export const usePreference = (key, defaultValue) => useSelector((state) => {
  if (state.session.server.forceSettings) {
    if (containsProperty(state.session.server, key)) {
      return state.session.server[key];
    }
    if (containsProperty(state.session.user, key)) {
      return state.session.user[key];
    }
    return defaultValue;
  }
  if (containsProperty(state.session.user, key)) {
    return state.session.user[key];
  }
  if (containsProperty(state.session.server, key)) {
    return state.session.server[key];
  }
  return defaultValue;
});

export const useAttributePreference = (key, defaultValue) => useSelector((state) => {
  if (state.session.server.forceSettings) {
    if (containsProperty(state.session.server.attributes, key)) {
      return state.session.server.attributes[key];
    }
    if (containsProperty(state.session.user.attributes, key)) {
      return state.session.user.attributes[key];
    }
    return defaultValue;
  }
  if (containsProperty(state.session.user.attributes, key)) {
    return state.session.user.attributes[key];
  }
  if (containsProperty(state.session.server.attributes, key)) {
    return state.session.server.attributes[key];
  }
  return defaultValue;
});