~cytrogen/masto-fe

ref: dc35e2d7569a293bbda92ee3ecba8d2d4e6a7b64 masto-fe/app/javascript/flavours/glitch/selectors/index.js -rw-r--r-- 4.9 KiB
dc35e2d7 — Cytrogen Rename .scss → .css and remove sass dependency (Phase 9-10) a month 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
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
126
127
128
129
130
131
132
133
134
135
import { List as ImmutableList, Map as ImmutableMap, OrderedSet as ImmutableOrderedSet } from "immutable";
import { createSelector } from "reselect";

import { me } from "flavours/glitch/initial_state";
import { toServerSideType } from "flavours/glitch/utils/filters";

const getAccountBase         = (state, id) => state.getIn(["accounts", id], null);
const getAccountCounters     = (state, id) => state.getIn(["accounts_counters", id], null);
const getAccountRelationship = (state, id) => state.getIn(["relationships", id], null);
const getAccountMoved        = (state, id) => state.getIn(["accounts", state.getIn(["accounts", id, "moved"])]);

export const makeGetAccount = () => {
  return createSelector([getAccountBase, getAccountCounters, getAccountRelationship, getAccountMoved], (base, counters, relationship, moved) => {
    if (base === null) {
      return null;
    }

    return base.merge(counters).withMutations(map => {
      map.set("relationship", relationship);
      map.set("moved", moved);
    });
  });
};

const getFilters = (state, { contextType }) => {
  if (!contextType) {
    return null;
  }

  const serverSideType = toServerSideType(contextType);
  const now = new Date();

  return state.get("filters").filter((filter) => filter.get("context").includes(serverSideType) && (filter.get("expires_at") === null || filter.get("expires_at") > now));
};

export const makeGetStatus = () => {
  return createSelector(
    [
      (state, { id }) => state.getIn(["statuses", id]),
      (state, { id }) => state.getIn(["statuses", state.getIn(["statuses", id, "reblog"])]),
      (state, { id }) => state.getIn(["accounts", state.getIn(["statuses", id, "account"])]),
      (state, { id }) => state.getIn(["accounts", state.getIn(["statuses", state.getIn(["statuses", id, "reblog"]), "account"])]),
      getFilters,
    ],

    (statusBase, statusReblog, accountBase, accountReblog, filters) => {
      if (!statusBase || statusBase.get("isLoading")) {
        return null;
      }

      let filtered = false;
      if ((accountReblog || accountBase).get("id") !== me && filters) {
        let filterResults = statusReblog?.get("filtered") || statusBase.get("filtered") || ImmutableList();
        if (filterResults.some((result) => filters.getIn([result.get("filter"), "filter_action"]) === "hide")) {
          return null;
        }
        filterResults = filterResults.filter(result => filters.has(result.get("filter")));
        if (!filterResults.isEmpty()) {
          filtered = filterResults.map(result => filters.getIn([result.get("filter"), "title"]));
        }
      }

      if (statusReblog) {
        statusReblog = statusReblog.set("account", accountReblog);
        statusReblog = statusReblog.set("matched_filters", filtered);
      } else {
        statusReblog = null;
      }

      return statusBase.withMutations(map => {
        map.set("reblog", statusReblog);
        map.set("account", accountBase);
        map.set("matched_filters", filtered);
      });
    },
  );
};

export const makeGetPictureInPicture = () => {
  return createSelector([
    (state, { id }) => state.get("picture_in_picture").statusId === id,
    (state) => state.getIn(["meta", "layout"]) !== "mobile",
  ], (inUse, available) => ImmutableMap({
    inUse: inUse && available,
    available,
  }));
};

const ALERT_DEFAULTS = {
  dismissAfter: 5000,
  style: false,
};

export const getAlerts = createSelector(state => state.get("alerts"), alerts =>
  alerts.map(item => ({
    ...ALERT_DEFAULTS,
    ...item,
  })).toArray());

export const makeGetNotification = () => createSelector([
  (_, base)             => base,
  (state, _, accountId) => state.getIn(["accounts", accountId]),
], (base, account) => base.set("account", account));

export const makeGetReport = () => createSelector([
  (_, base) => base,
  (state, _, targetAccountId) => state.getIn(["accounts", targetAccountId]),
], (base, targetAccount) => base.set("target_account", targetAccount));

export const getAccountGallery = createSelector([
  (state, id) => state.getIn(["timelines", `account:${id}:media`, "items"], ImmutableList()),
  state       => state.get("statuses"),
  (state, id) => state.getIn(["accounts", id]),
], (statusIds, statuses, account) => {
  let medias = ImmutableList();

  statusIds.forEach(statusId => {
    const status = statuses.get(statusId);
    medias = medias.concat(status.get("media_attachments").map(media => media.set("status", status).set("account", account)));
  });

  return medias;
});

export const getAccountHidden = createSelector([
  (state, id) => state.getIn(["accounts", id, "hidden"]),
  (state, id) => state.getIn(["relationships", id, "following"]) || state.getIn(["relationships", id, "requested"]),
  (state, id) => id === me,
], (hidden, followingOrRequested, isSelf) => {
  return hidden && !(isSelf || followingOrRequested);
});

export const getStatusList = createSelector([
  (state, type) => state.getIn(["status_lists", type, "items"], ImmutableOrderedSet()),
], (items) => items.toList());