How to use the @reduxjs/toolkit.createSelector function in @reduxjs/toolkit

To help you get started, we’ve selected a few @reduxjs/toolkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cockroachdb / cockroach-gen / pkg / ui / cluster-ui / src / store / liveness / liveness.selectors.ts View on Github external
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

import { createSelector } from "@reduxjs/toolkit";
import { AppState } from "../reducers";

const livenessesSelector = (state: AppState) => state.adminUI.liveness.data;

export const livenessStatusByNodeIDSelector = createSelector(
  livenessesSelector,
  livenesses => livenesses?.statuses || {},
);
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
const getMessagesByRoom = state => state.messages.byRoom;
const getLocalUserPresent = state => state.localUser.present;
export const getAuthorUser = (state, props) => state.users[props.authorId];
export const getMessageAuthor = (state, props) => state.users[props.message.author];
export const getLastMessage = state => state.messages.lastMessage;
export const getNick = state => state.localUser.nick;
export const getDesktopMentionNotifications = state => state.userSettings?.desktopMentionNotifications;
export const getShowDesktopNotification = state => state.notifications.showDesktop;

export const getRoomIds = createSelector([getRooms], (rooms = {}) => Object.keys(rooms));

export const getActiveRoom = createSelector([getRooms], rooms => _.find(rooms, { current: true }));

export const getActiveRoomId = createSelector([getActiveRoom], (activeRoom = {}) => activeRoom._id);

export const getTotalUnreadMessageCount = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_.reduce(localRoomMembersByRoom, (count, roomMember) => count + (roomMember.unreadMessageCount || 0), 0)
);

export const hasAnyUnreadMention = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_.some(localRoomMembersByRoom, roomMember => roomMember.unreadMessageCount > 0 && roomMember.unreadMention)
);

export const getRoomMembers = createSelector(
	[getActiveRoomId, getRooms],
	(activeRoomId, rooms = {}) => (rooms[activeRoomId] || {}).$members
);

export const getRoomTopic = createSelector([getActiveRoom], room => {
	if (!room) return;
	return {
		tokens: room.topicTokens,
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
export const getActiveRoomId = createSelector([getActiveRoom], (activeRoom = {}) => activeRoom._id);

export const getTotalUnreadMessageCount = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_.reduce(localRoomMembersByRoom, (count, roomMember) => count + (roomMember.unreadMessageCount || 0), 0)
);

export const hasAnyUnreadMention = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_.some(localRoomMembersByRoom, roomMember => roomMember.unreadMessageCount > 0 && roomMember.unreadMention)
);

export const getRoomMembers = createSelector(
	[getActiveRoomId, getRooms],
	(activeRoomId, rooms = {}) => (rooms[activeRoomId] || {}).$members
);

export const getRoomTopic = createSelector([getActiveRoom], room => {
	if (!room) return;
	return {
		tokens: room.topicTokens,
		text: room.topic
	};
});

const getChatByRoom = state => state.chatInput.byRoom;

export const getLocalMessages = createSelector(
	[getActiveRoomId, getMessagesByRoom, getLocalUser],
	(activeRoomId, messagesByRoom, localUser) => {
		if (!activeRoomId) return [];
		return _.filter(messagesByRoom[activeRoomId], { author: localUser._id });
	}
);
github pipe-cd / pipe / pkg / app / web / src / pages / settings / piped / index.tsx View on Github external
fetchPipeds,
  Piped,
  RegisteredPiped,
  selectAllPipeds,
} from "../../../modules/pipeds";
import { AppState } from "../../../store";
import { useSettingsStyles } from "../styles";
import { PipedTableRow } from "./piped-table-row";

const useStyles = makeStyles(() => ({
  toolbarSpacer: {
    flexGrow: 1,
  },
}));

const selectFilteredPipeds = createSelector<
  AppState,
  boolean | undefined,
  Piped.AsObject[],
  boolean | undefined,
  Piped.AsObject[]
>(
  selectAllPipeds,
  (_, enabled) => enabled,
  (pipeds, enabled) => {
    switch (enabled) {
      case true:
        return pipeds.filter((piped) => piped.disabled === false);
      case false:
        return pipeds.filter((piped) => piped.disabled);
      default:
        return pipeds;
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
}
);

const getChatForCurrentRoom = createSelector(
	[getActiveRoomId, getChatByRoom],
	(activeRoomId, chatByRoomId) => chatByRoomId[activeRoomId]
);

export const getAppendTextForCurrentRoom = createSelector(
	[getChatForCurrentRoom],
	(getChatForCurrentRoom = {}) => getChatForCurrentRoom.appendText
);

export const getCurrentRoomTextEmpty = createSelector([getAppendTextForCurrentRoom], (text = "") => text.length === 0);

export const getEditedMessageForCurrentRoom = createSelector(
	[getChatForCurrentRoom],
	(chatForCurrentRoom = {}) => chatForCurrentRoom.editedMessage
);

export const getSection = state => {
	const sectionMatch = /2\/(\w+)/.exec(state.router.location.pathname);
	return sectionMatch ? sectionMatch[1] : null;
};

export const getDocumentTitle = createSelector(
	[getTotalUnreadMessageCount, hasAnyUnreadMention, getSection, getActiveRoom],
	(totalUnreadMessageCount, anyUnreadMention, section, activeRoom = {}) => {
		const unread = totalUnreadMessageCount > 0 ? `${anyUnreadMention ? "*" : ""}(${totalUnreadMessageCount}) ` : "";
		const roomName = activeRoom.name || (section === "settings" && "Settings") || "";
		const leading = `${unread}${roomName}`;
		const leadingBreak = leading ? " - " : "";
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
const getChatByRoom = state => state.chatInput.byRoom;

export const getLocalMessages = createSelector(
	[getActiveRoomId, getMessagesByRoom, getLocalUser],
	(activeRoomId, messagesByRoom, localUser) => {
		if (!activeRoomId) return [];
		return _.filter(messagesByRoom[activeRoomId], { author: localUser._id });
	}
);

const getChatForCurrentRoom = createSelector(
	[getActiveRoomId, getChatByRoom],
	(activeRoomId, chatByRoomId) => chatByRoomId[activeRoomId]
);

export const getAppendTextForCurrentRoom = createSelector(
	[getChatForCurrentRoom],
	(getChatForCurrentRoom = {}) => getChatForCurrentRoom.appendText
);

export const getCurrentRoomTextEmpty = createSelector([getAppendTextForCurrentRoom], (text = "") => text.length === 0);

export const getEditedMessageForCurrentRoom = createSelector(
	[getChatForCurrentRoom],
	(chatForCurrentRoom = {}) => chatForCurrentRoom.editedMessage
);

export const getSection = state => {
	const sectionMatch = /2\/(\w+)/.exec(state.router.location.pathname);
	return sectionMatch ? sectionMatch[1] : null;
};
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
export const getRoomIds = createSelector([getRooms], (rooms = {}) => Object.keys(rooms));

export const getActiveRoom = createSelector([getRooms], rooms => _.find(rooms, { current: true }));

export const getActiveRoomId = createSelector([getActiveRoom], (activeRoom = {}) => activeRoom._id);

export const getTotalUnreadMessageCount = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_.reduce(localRoomMembersByRoom, (count, roomMember) => count + (roomMember.unreadMessageCount || 0), 0)
);

export const hasAnyUnreadMention = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_.some(localRoomMembersByRoom, roomMember => roomMember.unreadMessageCount > 0 && roomMember.unreadMention)
);

export const getRoomMembers = createSelector(
	[getActiveRoomId, getRooms],
	(activeRoomId, rooms = {}) => (rooms[activeRoomId] || {}).$members
);

export const getRoomTopic = createSelector([getActiveRoom], room => {
	if (!room) return;
	return {
		tokens: room.topicTokens,
		text: room.topic
	};
});

const getChatByRoom = state => state.chatInput.byRoom;

export const getLocalMessages = createSelector(
	[getActiveRoomId, getMessagesByRoom, getLocalUser],
github cockroachdb / cockroach-gen / pkg / ui / cluster-ui / src / store / nodes / nodes.selectors.ts View on Github external
import { AppState } from "../reducers";
import { getDisplayName } from "../../nodes";
import { livenessStatusByNodeIDSelector } from "../liveness";
import { accumulateMetrics } from "../../util";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
type ILocality = cockroach.roachpb.ILocality;

export const nodeStatusesSelector = (state: AppState) =>
  state.adminUI.nodes.data || [];

export const nodesSelector = createSelector(
  nodeStatusesSelector,
  accumulateMetrics,
);

export const nodeDisplayNameByIDSelector = createSelector(
  nodesSelector,
  livenessStatusByNodeIDSelector,
  (nodeStatuses, livenessStatusByNodeID) => {
    const result: { [key: string]: string } = {};
    if (!_.isEmpty(nodeStatuses)) {
      nodeStatuses.forEach(ns => {
        result[ns.desc.node_id] = getDisplayName(
          ns,
          livenessStatusByNodeID[ns.desc.node_id],
        );
      });
    }
    return result;
  },
);
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
export const getShowNotification = createSelector(
	[getLastMessageContainsMention, getLocalUserPresent],
	(lastMessage, localUserPresent) => !!lastMessage && !localUserPresent
);

export const getLastMentionRoomName = createSelector(
	[getLastMessageContainsMention, getRooms],
	(lastMessageMention = {}, rooms = {}) => rooms[lastMessageMention.room]?.name
);

export const getLastMentionAuthorNick = createSelector(
	[getLastMessageContainsMention, getUsers],
	(lastMessageMention = {}, users = {}) => users[lastMessageMention.author]?.nick
);

export const getLastMentionText = createSelector(
	[getLastMessageContainsMention],
	(lastMessageMention = {}) => lastMessageMention.text
);

export const getRoomIsCurrent = roomId => createSelector([getActiveRoomId], activeRoomId => activeRoomId === roomId);

export const getRoomName = roomId => state => getRooms(state)[roomId]?.name;

export const getUnreadMention = roomId => state => getLocalRoomMembersByRoom(state)[roomId]?.unreadMention;

export const getUnreadMessageCount = roomId => state => getLocalRoomMembersByRoom(state)[roomId]?.unreadMessageCount;

export const getUnreadRoomIds = createSelector([getLocalRoomMembersByRoom], localRoomMembersByRoom =>
	_(localRoomMembersByRoom)
		.filter(roomMember => roomMember.unreadMessageCount > 0)
		.orderBy(["unreadStart"])
github bunkerchat / bunker / src / selectors / selectors.js View on Github external
["desc", "desc", "asc"]
	);
});

export const getSortedRoomMemberUserIds = createSelector([getSortedRoomMemberUsers], roomMemberUsers =>
	roomMemberUsers.map(roomMemberUser => roomMemberUser.user)
);

export const getRoomMembersForCurrentRoomHash = createSelector([getRoomMembers], roomMembers =>
	_.keyBy(roomMembers, "user")
);

export const getRoomMemberRoleForCurrentRoomByUserId = userId =>
	createSelector([getRoomMembersForCurrentRoomHash], roomMembersHash => roomMembersHash[userId]?.role);

export const getLastMessageContainsMention = createSelector([getLastMessage, getNick], (lastMessage, nick) => {
	if (!lastMessage || !lastMessage.tokens) return;

	const hasMention = lastMessage.tokens.some(token => {
		if (token.type !== "word") return false;
		return token.value.toUpperCase().includes(nick.toUpperCase()) || token.value.includes("@all");
	});

	if (hasMention) return lastMessage;
});

export const getShowNotification = createSelector(
	[getLastMessageContainsMention, getLocalUserPresent],
	(lastMessage, localUserPresent) => !!lastMessage && !localUserPresent
);

export const getLastMentionRoomName = createSelector(