From fc2d65f2026c0755a2acf7d53d2875b049cd8af1 Mon Sep 17 00:00:00 2001
From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com>
Date: Fri, 28 Jan 2022 14:01:53 +0100
Subject: [PATCH] Refactor channels to use reducer (#207)
---
.../ActivityCenter/ActivityMessage.tsx | 5 +-
.../src/components/Channels/Channel.tsx | 9 +-
.../src/components/Channels/Channels.tsx | 11 +-
.../src/components/Chat/ChatCreation.tsx | 5 +-
.../src/components/Form/ChannelMenu.tsx | 33 ++--
.../src/contexts/messengerProvider.tsx | 3 +-
.../src/hooks/messenger/useGroupChats.ts | 26 +--
.../src/hooks/messenger/useMessenger.ts | 167 ++++++++++++------
packages/status-communities/src/groupChats.ts | 9 +-
9 files changed, 161 insertions(+), 107 deletions(-)
diff --git a/packages/react-chat/src/components/ActivityCenter/ActivityMessage.tsx b/packages/react-chat/src/components/ActivityCenter/ActivityMessage.tsx
index 368cc26d..666cc64e 100644
--- a/packages/react-chat/src/components/ActivityCenter/ActivityMessage.tsx
+++ b/packages/react-chat/src/components/ActivityCenter/ActivityMessage.tsx
@@ -47,10 +47,11 @@ export function ActivityMessage({
activity,
setShowActivityCenter,
}: ActivityMessageProps) {
- const { contacts, setActiveChannel } = useMessengerContext();
+ const { contacts, channelsDispatch } = useMessengerContext();
const { setModal } = useModal(ProfileModalName);
const showChannel = () => {
- activity.channel && setActiveChannel(activity.channel),
+ activity.channel &&
+ channelsDispatch({ type: "ChangeActive", payload: activity.channel.id }),
setShowActivityCenter(false);
};
diff --git a/packages/react-chat/src/components/Channels/Channel.tsx b/packages/react-chat/src/components/Channels/Channel.tsx
index ebe1a611..6aa60d91 100644
--- a/packages/react-chat/src/components/Channels/Channel.tsx
+++ b/packages/react-chat/src/components/Channels/Channel.tsx
@@ -59,6 +59,7 @@ export function Channel({
setEditGroup,
}: ChannelProps) {
const narrow = useNarrow();
+ const { channelsDispatch } = useMessengerContext();
return (
{channel?.isMuted && activeView && !narrow && (
- (channel.isMuted = !channel.isMuted)}>
+
+ channelsDispatch({ type: "ToggleMuted", payload: channel.id })
+ }
+ >
@@ -90,7 +95,7 @@ export function Channel({
)}
- {!activeView && !!mention && mention > 0 && !channel?.isMuted && (
+ {!activeView && !!mention && !channel?.isMuted && (
{mention}
)}
{channel?.isMuted && !activeView && }
diff --git a/packages/react-chat/src/components/Channels/Channels.tsx b/packages/react-chat/src/components/Channels/Channels.tsx
index d05d6039..e0e5428d 100644
--- a/packages/react-chat/src/components/Channels/Channels.tsx
+++ b/packages/react-chat/src/components/Channels/Channels.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { useMemo } from "react";
import styled from "styled-components";
import { ChatState, useChatState } from "../../contexts/chatStateProvider";
@@ -23,12 +23,15 @@ function GenerateChannels({
onCommunityClick,
setEditGroup,
}: GenerateChannelsProps) {
- const { mentions, notifications, activeChannel, setActiveChannel, channels } =
+ const { mentions, notifications, activeChannel, channelsDispatch, channels } =
useMessengerContext();
+
+ const channelList = useMemo(() => Object.values(channels), [channels]);
+
const setChatState = useChatState()[1];
return (
<>
- {Object.values(channels)
+ {channelList
.filter((channel) => channel.type === type)
.map((channel) => (
0}
mention={mentions?.[channel.id]}
onClick={() => {
- setActiveChannel(channel);
+ channelsDispatch({ type: "ChangeActive", payload: channel.id });
if (onCommunityClick) {
onCommunityClick();
}
diff --git a/packages/react-chat/src/components/Chat/ChatCreation.tsx b/packages/react-chat/src/components/Chat/ChatCreation.tsx
index 85843326..e80e16d1 100644
--- a/packages/react-chat/src/components/Chat/ChatCreation.tsx
+++ b/packages/react-chat/src/components/Chat/ChatCreation.tsx
@@ -34,8 +34,7 @@ export function ChatCreation({
(member) => member?.customName ?? member.trueName
) ?? []
);
- const { contacts, createGroupChat, addMembers, setChannel } =
- useMessengerContext();
+ const { contacts, createGroupChat, addMembers } = useMessengerContext();
const setChatState = useChatState()[1];
const addMember = useCallback(
@@ -71,7 +70,7 @@ export function ChatCreation({
setChatState(ChatState.ChatBody);
}
},
- [identity, createGroupChat, setChannel]
+ [identity, createGroupChat]
);
return (
diff --git a/packages/react-chat/src/components/Form/ChannelMenu.tsx b/packages/react-chat/src/components/Form/ChannelMenu.tsx
index 12c76573..023feadb 100644
--- a/packages/react-chat/src/components/Form/ChannelMenu.tsx
+++ b/packages/react-chat/src/components/Form/ChannelMenu.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useMemo, useState } from "react";
+import React, { useMemo, useState } from "react";
import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider";
@@ -41,20 +41,12 @@ export const ChannelMenu = ({
className,
}: ChannelMenuProps) => {
const narrow = useNarrow();
- const { clearNotifications } = useMessengerContext();
+ const { clearNotifications, channelsDispatch } = useMessengerContext();
const { setModal } = useModal(EditModalName);
const { setModal: setLeavingModal } = useModal(LeavingModalName);
const { setModal: setProfileModal } = useModal(ProfileModalName);
const [showSubmenu, setShowSubmenu] = useState(false);
- const muting = channel.isMuted;
- const [isMuted, setIsMuted] = useState(muting);
-
- useEffect(() => {
- if (isMuted) channel.isMuted = true;
- if (!isMuted) channel.isMuted = false;
- }, [isMuted]);
-
const { showMenu, setShowMenu: setShowSideMenu } = useContextMenu(
channel.id + "contextMenu"
);
@@ -113,26 +105,31 @@ export const ChannelMenu = ({