Skip to content

Commit

Permalink
Add memoized selector
Browse files Browse the repository at this point in the history
  • Loading branch information
metastasio committed Oct 10, 2023
1 parent 9e1a445 commit 045a2ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
8 changes: 3 additions & 5 deletions frontend/src/components/Chat/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import {
} from 'react-bootstrap';

import MessageItem from './MessageItem';
import { selectChatContent } from '../../store/stateSelectors';
import { selectChatContent, selectCurrentChatMessages } from '../../store/stateSelectors';
import { useAuthContext, useSocketContext } from '../../hooks';

const Chat = () => {
const { t } = useTranslation();
const { handleEmit } = useSocketContext();
const { authData } = useAuthContext();
const { entities, currentChannel, messages } = useSelector(selectChatContent);
const { entities, currentChannel } = useSelector(selectChatContent);
const focus = useRef();
const formRef = useRef();

const getActiveChannel = (element) => element.id === currentChannel;
const chat = entities.find(getActiveChannel);
const currentChatMessages = messages.filter(
(message) => message.channelId === currentChannel,
);
const currentChatMessages = useSelector(selectCurrentChatMessages(currentChannel));
const messagesInChat = currentChatMessages.length;

useEffect(() => focus.current && focus.current.focus(), [chat]);
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/components/Modals/ModalAddChannel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import { toast } from 'react-toastify';
import { closeModal } from '../../store/modal.slice';
import { changeActiveChannel } from '../../store/content.slice';
import { schemaChannel } from '../../services/yupSchemas';
import { selectModal, selectChatContent } from '../../store/stateSelectors';
import { selectModal, selectNames } from '../../store/stateSelectors';
import { useSocketContext } from '../../hooks';

const ModalAddChannel = () => {
const { Formik } = formik;
const { handleEmit } = useSocketContext();
const { t } = useTranslation();
const { open } = useSelector(selectModal);
const { entities } = useSelector(selectChatContent);
const names = entities.map((entity) => entity.name);
const names = useSelector(selectNames);
const schema = schemaChannel(names);
const focus = useRef();
const dispatch = useDispatch();
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/store/stateSelectors.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { createSelector } from '@reduxjs/toolkit';

const selectChatContent = (state) => state.content;
const selectModal = (state) => state.modal;
const selectToastContent = (state) => state.modal.toast;

const selectCurrentChatMessages = (currentChannel) => createSelector(
(state) => selectChatContent(state),
(content) => content.messages.filter((message) => message.channelId === currentChannel),
);

const selectNames = createSelector(
(state) => selectChatContent(state),
(content) => content.entities.map((entity) => entity.name),
);

export {
selectChatContent,
selectModal,
selectToastContent,
selectCurrentChatMessages,
selectNames,
};

0 comments on commit 045a2ba

Please sign in to comment.