Skip to content

Commit

Permalink
refactor(components): implement memoized selectors for denormalized c…
Browse files Browse the repository at this point in the history
…hannel (#2742)

* refactor(components): implement memoized selectors

* refactor: remove logs
  • Loading branch information
domw30 authored and colbr committed Mar 6, 2025
1 parent 5927ee2 commit 5efeff3
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/apps/feed/components/feed-chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RootState } from '../../../../store/reducer';
import { connectContainer } from '../../../../store/redux-container';
import { ChatViewContainer } from '../../../../components/chat-view-container/chat-view-container';
import { validateFeedChat } from '../../../../store/chat';
import { Channel, denormalize, onRemoveReply } from '../../../../store/channels';
import { Channel, onRemoveReply } from '../../../../store/channels';
import { MessageInput } from '../../../../components/message-input/container';
import { send as sendMessage } from '../../../../store/messages';
import { SendPayload as PayloadSendMessage } from '../../../../store/messages/saga';
Expand All @@ -18,6 +18,7 @@ import { toggleSecondarySidekick } from '../../../../store/group-management';
import { MembersSidekick } from '../../../../components/sidekick/variants/members-sidekick';
import { Spinner } from '@zero-tech/zui/components/LoadingIndicator';
import { ConversationActionsContainer } from '../../../../components/messenger/conversation-actions/container';
import { denormalizedChannelSelector } from '../../../../store/channels/selectors';

import classNames from 'classnames';
import styles from './styles.module.scss';
Expand Down Expand Up @@ -54,7 +55,7 @@ export class Container extends React.Component<Properties> {
groupManagement,
} = state;

const channel = denormalize(activeConversationId, state);
const channel = denormalizedChannelSelector(state, activeConversationId);
const rawChannel = rawChannelSelector(activeConversationId)(state);

return {
Expand Down
4 changes: 2 additions & 2 deletions src/apps/messenger/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { MessengerChat } from '../../components/messenger/chat';
import { MessengerFeed } from '../../components/messenger/feed';
import { DevPanelContainer } from '../../components/dev-panel/container';
import { FeatureFlag } from '../../components/feature-flag';
import { denormalize } from '../../store/channels';
import { JoiningConversationDialog } from '../../components/joining-conversation-dialog';
import { ConversationsSidekick } from '../../components/sidekick/variants/conversations-sidekick';
import { MembersSidekick } from '../../components/sidekick/variants/members-sidekick';
import { denormalizedChannelSelector } from '../../store/channels/selectors';

import styles from './Main.module.scss';

Expand All @@ -30,7 +30,7 @@ export class Container extends React.Component<Properties> {
chat: { activeConversationId, isJoiningConversation, isConversationsLoaded },
} = state;

const currentChannel = denormalize(activeConversationId, state) || null;
const currentChannel = denormalizedChannelSelector(state, activeConversationId) || null;

return {
isValidConversation: !!activeConversationId,
Expand Down
5 changes: 3 additions & 2 deletions src/components/chat-view-container/chat-view-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
AdminMessageType,
sendEmojiReaction,
} from '../../store/messages';
import { Channel, ConversationStatus, denormalize, onReply } from '../../store/channels';
import { Channel, ConversationStatus, onReply } from '../../store/channels';
import { ChatView } from './chat-view';
import { AuthenticationState } from '../../store/authentication/types';
import { EditPayload, Payload as PayloadFetchMessages } from '../../store/messages/saga';
Expand All @@ -25,6 +25,7 @@ import { openMessageInfo } from '../../store/message-info';
import { toggleSecondarySidekick } from '../../store/group-management';
import { linkMessages, mapMessagesById, mapMessagesByRootId } from './utils';
import { openReportUserModal } from '../../store/report-user';
import { denormalizedChannelSelector } from '../../store/channels/selectors';

export interface Properties extends PublicProperties {
channel: Channel;
Expand Down Expand Up @@ -63,7 +64,7 @@ export class Container extends React.Component<Properties> {
}

static mapState(state: RootState, props: PublicProperties): Partial<Properties> {
const channel = denormalize(props.channelId, state) || null;
const channel = denormalizedChannelSelector(state, props.channelId) || null;
const {
authentication: { user },
chat: { activeConversationId },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { RootState } from '../../../store/reducer';
import { ConfirmationDefinition, MemberManagementDialog } from '.';
import { denormalize as denormalizeUser } from '../../../store/users';
import { displayName } from '../../../lib/user';
import { denormalize as denormalizeChannel } from '../../../store/channels';
import {
cancelMemberManagement,
removeMember,
Expand All @@ -14,6 +13,7 @@ import {
setMemberAsModerator,
removeMemberAsModerator,
} from '../../../store/group-management';
import { denormalizedChannelSelector } from '../../../store/channels/selectors';

export interface PublicProperties {}

Expand All @@ -38,7 +38,7 @@ export class Container extends React.Component<Properties> {
groupManagement: { memberManagement },
} = state;
const user = denormalizeUser(memberManagement.userId, state);
const channel = denormalizeChannel(memberManagement.roomId, state);
const channel = denormalizedChannelSelector(state, memberManagement.roomId);

return {
type: memberManagement.type,
Expand Down
9 changes: 4 additions & 5 deletions src/components/messenger/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import classNames from 'classnames';
import { RootState } from '../../../store/reducer';
import { connectContainer } from '../../../store/redux-container';
import { Channel, denormalize, onAddLabel, onRemoveLabel, onRemoveReply } from '../../../store/channels';
import { Channel, onAddLabel, onRemoveLabel, onRemoveReply } from '../../../store/channels';
import { ChatViewContainer } from '../../chat-view-container/chat-view-container';
import { send as sendMessage } from '../../../store/messages';
import { SendPayload as PayloadSendMessage } from '../../../store/messages/saga';
Expand All @@ -21,9 +21,9 @@ import { Media } from '../../message-input/utils';
import { ConversationHeaderContainer as ConversationHeader } from '../conversation-header/container';

import './styles.scss';
import { rawChannelSelector } from '../../../store/channels/saga';
import { getOtherMembersTypingDisplayJSX } from '../lib/utils';
import { Panel, PanelBody } from '../../layout/panel';
import { denormalizedChannelSelector } from '../../../store/channels/selectors';

export interface PublicProperties {}

Expand Down Expand Up @@ -59,16 +59,15 @@ export class Container extends React.Component<Properties> {
groupManagement,
} = state;

const directMessage = denormalize(activeConversationId, state);
const channel = rawChannelSelector(activeConversationId)(state);
const directMessage = denormalizedChannelSelector(state, activeConversationId);

return {
activeConversationId,
directMessage,
isJoiningConversation,
isSecondarySidekickOpen: groupManagement.isSecondarySidekickOpen,
leaveGroupDialogStatus: groupManagement.leaveGroupDialogStatus,
otherMembersTypingInRoom: channel?.otherMembersTyping || [],
otherMembersTypingInRoom: directMessage?.otherMembersTyping || [],
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/components/messenger/conversation-actions/container.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { RootState } from '../../../store/reducer';
import { connectContainer } from '../../../store/redux-container';
import { Channel, DefaultRoomLabels, denormalize, onAddLabel, onRemoveLabel } from '../../../store/channels';
import { Channel, DefaultRoomLabels, onAddLabel, onRemoveLabel } from '../../../store/channels';
import { currentUserSelector } from '../../../store/authentication/selectors';
import {
startAddGroupMember,
Expand All @@ -13,6 +13,8 @@ import {
} from '../../../store/group-management';
import { ConversationActions } from '.';
import { openReportUserModal } from '../../../store/report-user';
import { denormalizedChannelSelector } from '../../../store/channels/selectors';

import './styles.scss';

export interface PublicProperties {
Expand Down Expand Up @@ -48,7 +50,7 @@ export class Container extends React.Component<Properties> {
groupManagement,
} = state;

const directMessage = denormalize(activeConversationId, state);
const directMessage = denormalizedChannelSelector(state, activeConversationId);
const currentUser = currentUserSelector(state);
const hasMultipleMembers = (directMessage?.otherMembers || []).length > 1;
const isSocialChannel = directMessage?.isSocialChannel;
Expand Down
5 changes: 3 additions & 2 deletions src/components/messenger/conversation-header/container.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import { RootState } from '../../../store/reducer';
import { connectContainer } from '../../../store/redux-container';
import { Channel, denormalize } from '../../../store/channels';
import { Channel } from '../../../store/channels';
import { toggleSecondarySidekick } from '../../../store/group-management';
import { ConversationHeader } from '.';
import { ConversationActionsContainer as ConversationActions } from '../conversation-actions/container';
import { PanelHeader } from '../../layout/panel';
import { denormalizedChannelSelector } from '../../../store/channels/selectors';

import { bemClassName } from '../../../lib/bem';
import './styles.scss';
Expand All @@ -32,7 +33,7 @@ export class Container extends React.Component<Properties> {
groupManagement,
} = state;

const directMessage = denormalize(activeConversationId, state);
const directMessage = denormalizedChannelSelector(state, activeConversationId);

return {
activeConversationId,
Expand Down
5 changes: 3 additions & 2 deletions src/components/messenger/feed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import React from 'react';
import { RootState } from '../../../store/reducer';
import { connectContainer } from '../../../store/redux-container';
import { PostPayload as PayloadPostMessage } from '../../../store/posts/saga';
import { Channel, denormalize } from '../../../store/channels';
import { Channel } from '../../../store/channels';
import { sendPost } from '../../../store/posts';
import { ConversationHeaderContainer as ConversationHeader } from '../conversation-header/container';
import { LeaveGroupDialogContainer } from '../../group-management/leave-group-dialog/container';
import { LeaveGroupDialogStatus, setLeaveGroupStatus } from '../../../store/group-management';
import { Switch, Route } from 'react-router-dom';
import { denormalizedChannelSelector } from '../../../store/channels/selectors';

import { bemClassName } from '../../../lib/bem';
import './styles.scss';
Expand Down Expand Up @@ -41,7 +42,7 @@ export class Container extends React.Component<Properties> {
posts,
} = state;

const currentChannel = denormalize(activeConversationId, state) || null;
const currentChannel = denormalizedChannelSelector(state, activeConversationId) || null;

return {
channel: currentChannel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import * as React from 'react';
import { RootState } from '../../../../store/reducer';
import { connectContainer } from '../../../../store/redux-container';
import { MemberManagementAction, openMemberManagement } from '../../../../store/group-management';
import { User, denormalize as denormalizeChannel } from '../../../../store/channels';
import { User } from '../../../../store/channels';

import { MemberManagementMenu } from '.';
import { isUserModerator } from '../../list/utils/utils';
import { currentUserSelector } from '../../../../store/authentication/selectors';
import { denormalizedChannelSelector } from '../../../../store/channels/selectors';

export interface PublicProperties {
user?: User;
Expand All @@ -30,7 +31,7 @@ export class Container extends React.Component<Properties> {
chat: { activeConversationId },
} = state;

const conversation = denormalizeChannel(activeConversationId, state);
const conversation = denormalizedChannelSelector(state, activeConversationId);
const conversationModeratorIds = conversation?.moderatorIds;

const currentUser = currentUserSelector(state);
Expand Down
4 changes: 2 additions & 2 deletions src/components/messenger/message-info/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react';
import { connectContainer } from '../../../store/redux-container';
import { RootState } from '../../../store/reducer';
import { closeMessageInfo } from '../../../store/message-info';
import { denormalize as denormalizeChannel } from '../../../store/channels';
import { User } from '../../../store/channels';
import { OverviewPanel } from './overview-panel';
import { denormalizedChannelSelector } from '../../../store/channels/selectors';

export interface PublicProperties {}

Expand All @@ -26,7 +26,7 @@ export class Container extends React.Component<Properties> {
messageInfo: { selectedMessageId },
} = state;

const channel = denormalizeChannel(activeConversationId, state) || {};
const channel = denormalizedChannelSelector(state, activeConversationId) || {};
const messages = channel.messages || [];
const selectedMessage = messages.find((msg) => msg.id === selectedMessageId) || {};
const sentBy = selectedMessage?.sender?.userId !== user.data?.id ? selectedMessage?.sender : null;
Expand Down
10 changes: 10 additions & 0 deletions src/store/channels/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { RootState } from '../reducer';
import getDeepProperty from 'lodash.get';
import { createSelector } from 'reselect';
import { denormalize as denormalizeChannel } from '../channels';

export const rawChannel = (state: RootState, channelId: string) => {
return getDeepProperty(state, `normalized.channels['${channelId}']`, null);
};

// Memoized selector for denormalized channel
export const denormalizedChannelSelector = createSelector(
[(state: RootState) => state, (_state: RootState, channelId: string) => channelId],
(state, channelId) => {
return denormalizeChannel(channelId, state);
}
);

0 comments on commit 5efeff3

Please sign in to comment.