mattermost-mobile/app/mm-redux/selectors/entities/groups.ts
Harrison Healey c7ef3fe1d3
MM-22155 ESLint configuration updates part 1 (#4311)
* MM-22155 Disable no-undefined

* MM-22155 Disable react-jsx-filename-extension

* MM-22155 Disable no-magic-numbers

* MM-22155 Re-enable camelcase

* MM-22155 Re-enable no-nested-ternary

* Update no-unused-vars to match other repos
2020-05-20 08:27:27 -07:00

92 lines
3.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import * as reselect from 'reselect';
import {GlobalState} from '@mm-redux/types/store';
const emptyList: any[] = [];
const emptySyncables = {
teams: [],
channels: [],
};
export function getAllGroups(state: GlobalState) {
return state.entities.groups.groups;
}
export function getGroup(state: GlobalState, id: string) {
return getAllGroups(state)[id];
}
export function getGroupMemberCount(state: GlobalState, id: string) {
const memberData = state.entities.groups.members;
const groupMemberData = memberData[id];
if (!groupMemberData) {
return 0;
}
return memberData[id].totalMemberCount;
}
function getGroupSyncables(state: GlobalState, id: string) {
return state.entities.groups.syncables[id] || emptySyncables;
}
export function getGroupTeams(state: GlobalState, id: string) {
return getGroupSyncables(state, id).teams;
}
export function getGroupChannels(state: GlobalState, id: string) {
return getGroupSyncables(state, id).channels;
}
export function getGroupMembers(state: GlobalState, id: string) {
const groupMemberData = state.entities.groups.members[id];
if (!groupMemberData) {
return emptyList;
}
return groupMemberData.members;
}
const teamGroupIDs = (state: GlobalState, teamID: string) => state.entities.teams.groupsAssociatedToTeam[teamID]?.ids || [];
const channelGroupIDs = (state: GlobalState, channelID: string) => state.entities.channels.groupsAssociatedToChannel[channelID]?.ids || [];
const getTeamGroupIDSet = reselect.createSelector(
teamGroupIDs,
(teamIDs) => new Set(teamIDs),
);
const getChannelGroupIDSet = reselect.createSelector(
channelGroupIDs,
(channelIDs) => new Set(channelIDs),
);
export const getGroupsNotAssociatedToTeam = reselect.createSelector(
getAllGroups,
(state: GlobalState, teamID: string) => getTeamGroupIDSet(state, teamID),
(allGroups, teamGroupIDSet) => {
return Object.entries(allGroups).filter(([groupID]) => !teamGroupIDSet.has(groupID)).map((entry) => entry[1]);
},
);
export const getGroupsAssociatedToTeam = reselect.createSelector(
getAllGroups,
(state: GlobalState, teamID: string) => getTeamGroupIDSet(state, teamID),
(allGroups, teamGroupIDSet) => {
return Object.entries(allGroups).filter(([groupID]) => teamGroupIDSet.has(groupID)).map((entry) => entry[1]);
},
);
export const getGroupsNotAssociatedToChannel = reselect.createSelector(
getAllGroups,
(state: GlobalState, channelID: string) => getChannelGroupIDSet(state, channelID),
(allGroups, channelGroupIDSet) => {
return Object.entries(allGroups).filter(([groupID]) => !channelGroupIDSet.has(groupID)).map((entry) => entry[1]);
},
);
export const getGroupsAssociatedToChannel = reselect.createSelector(
getAllGroups,
(state: GlobalState, channelID: string) => getChannelGroupIDSet(state, channelID),
(allGroups, channelGroupIDSet) => {
return Object.entries(allGroups).filter(([groupID]) => channelGroupIDSet.has(groupID)).map((entry) => entry[1]);
},
);