diff --git a/packages/react-chat/src/components/Channels/Channel.tsx b/packages/react-chat/src/components/Channels/Channel.tsx index 4193f180..ce20897d 100644 --- a/packages/react-chat/src/components/Channels/Channel.tsx +++ b/packages/react-chat/src/components/Channels/Channel.tsx @@ -20,7 +20,7 @@ function RenderChannelName({ case "group": return (
- + {` ${channel.name}`}
); diff --git a/packages/react-chat/src/components/Channels/Channels.tsx b/packages/react-chat/src/components/Channels/Channels.tsx index fc08ca31..e0526b4b 100644 --- a/packages/react-chat/src/components/Channels/Channels.tsx +++ b/packages/react-chat/src/components/Channels/Channels.tsx @@ -29,7 +29,7 @@ function GenerateChannels({ type, onCommunityClick }: GenerateChannelsProps) { 0} mention={mentions?.[channel.id]} onClick={() => { diff --git a/packages/react-chat/src/components/Chat/ChatBody.tsx b/packages/react-chat/src/components/Chat/ChatBody.tsx index 03b05d46..8ff134bb 100644 --- a/packages/react-chat/src/components/Chat/ChatBody.tsx +++ b/packages/react-chat/src/components/Chat/ChatBody.tsx @@ -1,10 +1,10 @@ import React, { useCallback, useEffect, useMemo, useState } from "react"; import styled from "styled-components"; -import { useIdentity } from "../../contexts/identityProvider"; import { useMessengerContext } from "../../contexts/messengerProvider"; import { useNarrow } from "../../contexts/narrowProvider"; import { Reply } from "../../hooks/useReply"; +import { ChannelData } from "../../models/ChannelData"; import { TokenRequirement } from "../Form/TokenRequirement"; import { MessagesList } from "../Messages/MessagesList"; import { NarrowChannels } from "../NarrowMode/NarrowChannels"; @@ -13,7 +13,7 @@ import { LoadingSkeleton } from "../Skeleton/LoadingSkeleton"; import { ChatCreation } from "./ChatCreation"; import { ChatInput } from "./ChatInput"; -import { ChatTopbar } from "./ChatTopbar"; +import { ChatTopbar, ChatTopbarLoading } from "./ChatTopbar"; export enum ChatBodyState { Chat, @@ -21,6 +21,55 @@ export enum ChatBodyState { Members, } +function ChatBodyLoading() { + const narrow = useNarrow(); + return ( + + + + + undefined} /> + + + ); +} + +type ChatBodyContentProps = { + showState: ChatBodyState; + switchShowState: (state: ChatBodyState) => void; + channel: ChannelData; +}; + +function ChatBodyContent({ + showState, + switchShowState, + channel, +}: ChatBodyContentProps) { + const [reply, setReply] = useState(undefined); + + switch (showState) { + case ChatBodyState.Chat: + return ( + <> + + + + ); + case ChatBodyState.Channels: + return ( + switchShowState(ChatBodyState.Channels)} + /> + ); + case ChatBodyState.Members: + return ( + switchShowState(ChatBodyState.Members)} + /> + ); + } +} + interface ChatBodyProps { onClick: () => void; showMembers: boolean; @@ -29,9 +78,8 @@ interface ChatBodyProps { export function ChatBody({ onClick, showMembers, permission }: ChatBodyProps) { const { messenger, activeChannel, communityData } = useMessengerContext(); - const narrow = useNarrow(); - const identity = useIdentity(); const [editGroup, setEditGroup] = useState(false); + const narrow = useNarrow(); const className = useMemo(() => (narrow ? "narrow" : ""), [narrow]); const [showState, setShowState] = useState(ChatBodyState.Chat); @@ -50,70 +98,40 @@ export function ChatBody({ onClick, showMembers, permission }: ChatBodyProps) { } }, [narrow]); - const [reply, setReply] = useState(undefined); - - return ( - - - {editGroup && communityData ? ( - - ) : ( - + + {editGroup && communityData ? ( + + ) : ( + + )} + + + {!permission && ( + + + )} - {messenger ? ( - <> - {showState === ChatBodyState.Chat && ( - <> - {messenger && communityData ? ( - - ) : ( - - )} - - - )} + + ); + } - {showState === ChatBodyState.Channels && ( - switchShowState(ChatBodyState.Channels)} - /> - )} - {showState === ChatBodyState.Members && ( - - switchShowState(ChatBodyState.Members) - } - /> - )} - - ) : ( - <> - - - - )} - - {!permission && ( - - - - )} - - ); + return ; } const Wrapper = styled.div` diff --git a/packages/react-chat/src/components/Chat/ChatInput.tsx b/packages/react-chat/src/components/Chat/ChatInput.tsx index 6db6ca09..b43cb849 100644 --- a/packages/react-chat/src/components/Chat/ChatInput.tsx +++ b/packages/react-chat/src/components/Chat/ChatInput.tsx @@ -7,6 +7,7 @@ import React, { } from "react"; import styled from "styled-components"; +import { useIdentity } from "../../contexts/identityProvider"; import { useMessengerContext } from "../../contexts/messengerProvider"; import { useModal } from "../../contexts/modalProvider"; import { Reply } from "../../hooks/useReply"; @@ -27,10 +28,11 @@ import { EmojiPicker } from "./EmojiPicker"; interface ChatInputProps { reply: Reply | undefined; setReply: (val: Reply | undefined) => void; - disabled: boolean; } -export function ChatInput({ reply, setReply, disabled }: ChatInputProps) { +export function ChatInput({ reply, setReply }: ChatInputProps) { + const identity = useIdentity(); + const disabled = useMemo(() => !identity, [identity]); const { sendMessage, contacts } = useMessengerContext(); const [content, setContent] = useState(""); const [clearComponent, setClearComponent] = useState(""); diff --git a/packages/react-chat/src/components/Chat/ChatTopbar.tsx b/packages/react-chat/src/components/Chat/ChatTopbar.tsx index 11cfee4d..15fbf1a6 100644 --- a/packages/react-chat/src/components/Chat/ChatTopbar.tsx +++ b/packages/react-chat/src/components/Chat/ChatTopbar.tsx @@ -16,9 +16,38 @@ import { Loading } from "../Skeleton/Loading"; import { ChatBodyState } from "./ChatBody"; +export function ChatTopbarLoading() { + const narrow = useNarrow(); + + return ( + + + + + + + + {!narrow && ( + + + + )} + + + + + + + + + + + + ); +} + type ChatTopbarProps = { showState: ChatBodyState; - className: string; onClick: () => void; switchShowState: (state: ChatBodyState) => void; showMembers: boolean; @@ -27,7 +56,6 @@ type ChatTopbarProps = { export function ChatTopbar({ showState, - className, onClick, switchShowState, showMembers, @@ -39,15 +67,19 @@ export function ChatTopbar({ const [showChannelMenu, setShowChannelMenu] = useState(false); const [showActivityCenter, setShowActivityCenter] = useState(false); + if (!activeChannel) { + return ; + } + return ( - + {messenger && communityData ? ( <> {narrow && ( - + )} diff --git a/packages/react-chat/src/components/Messages/MessagesList.tsx b/packages/react-chat/src/components/Messages/MessagesList.tsx index 8fce8819..de1ee393 100644 --- a/packages/react-chat/src/components/Messages/MessagesList.tsx +++ b/packages/react-chat/src/components/Messages/MessagesList.tsx @@ -5,6 +5,7 @@ import { useMessengerContext } from "../../contexts/messengerProvider"; import { useModal } from "../../contexts/modalProvider"; import { useChatScrollHandle } from "../../hooks/useChatScrollHandle"; import { Reply } from "../../hooks/useReply"; +import { ChannelData } from "../../models/ChannelData"; import { EmptyChannel } from "../Channels/EmptyChannel"; import { LoadingIcon } from "../Icons/LoadingIcon"; import { LinkModal, LinkModalName } from "../Modals/LinkModal"; @@ -14,12 +15,13 @@ import { UiMessage } from "./UiMessage"; interface MessagesListProps { setReply: (val: Reply | undefined) => void; + channel: ChannelData; } -export function MessagesList({ setReply }: MessagesListProps) { - const { messages, activeChannel, contacts } = useMessengerContext(); +export function MessagesList({ setReply, channel }: MessagesListProps) { + const { messages, contacts } = useMessengerContext(); const ref = useRef(null); - const loadingMessages = useChatScrollHandle(messages, ref, activeChannel); + const loadingMessages = useChatScrollHandle(messages, ref); const shownMessages = useMemo( () => @@ -50,7 +52,7 @@ export function MessagesList({ setReply }: MessagesListProps) { - + {loadingMessages && ( @@ -60,7 +62,7 @@ export function MessagesList({ setReply }: MessagesListProps) { { const { setModal } = useModal(EditModalName); const handleUpload = () => { - activeChannel.icon = image; - changeGroupChatName(groupName, activeChannel.id); - setModal(false); + if (activeChannel) { + activeChannel.icon = image; + changeGroupChatName(groupName, activeChannel.id); + setModal(false); + } }; return ( @@ -62,10 +64,10 @@ export const EditModal = () => { - - {!activeChannel.icon && + + {!activeChannel?.icon && !image && - activeChannel.name.slice(0, 1).toUpperCase()} + activeChannel?.name?.slice(0, 1)?.toUpperCase()} ({ communityData: undefined, contacts: {}, setContacts: () => undefined, - activeChannel: { - id: "", - name: "", - type: "channel", - }, + activeChannel: undefined, channels: {}, setChannel: () => undefined, removeChannel: () => undefined, diff --git a/packages/react-chat/src/hooks/messenger/useMessenger.ts b/packages/react-chat/src/hooks/messenger/useMessenger.ts index b980082d..6aefdbf6 100644 --- a/packages/react-chat/src/hooks/messenger/useMessenger.ts +++ b/packages/react-chat/src/hooks/messenger/useMessenger.ts @@ -42,7 +42,7 @@ export type MessengerType = { channels: ChannelsData; setChannel: (channel: ChannelData) => void; removeChannel: (channelId: string) => void; - activeChannel: ChannelData; + activeChannel: ChannelData | undefined; setActiveChannel: (channel: ChannelData) => void; createGroupChat: (members: string[]) => void; changeGroupChatName: (name: string, chatId: string) => void; diff --git a/packages/react-chat/src/hooks/useChatScrollHandle.ts b/packages/react-chat/src/hooks/useChatScrollHandle.ts index 6abafcee..71a7d8bd 100644 --- a/packages/react-chat/src/hooks/useChatScrollHandle.ts +++ b/packages/react-chat/src/hooks/useChatScrollHandle.ts @@ -1,15 +1,13 @@ import { useEffect, useState } from "react"; import { useMessengerContext } from "../contexts/messengerProvider"; -import { ChannelData } from "../models/ChannelData"; import { ChatMessage } from "../models/ChatMessage"; export function useChatScrollHandle( messages: ChatMessage[], - ref: React.RefObject, - activeChannel: ChannelData + ref: React.RefObject ) { - const { loadPrevDay, loadingMessages } = useMessengerContext(); + const { loadPrevDay, loadingMessages, activeChannel } = useMessengerContext(); const [scrollOnBot, setScrollOnBot] = useState(true); useEffect(() => { @@ -19,7 +17,7 @@ export function useChatScrollHandle( }, [messages.length, scrollOnBot]); useEffect(() => { - if (!loadingMessages) { + if (!loadingMessages && activeChannel) { if ( (ref?.current?.clientHeight ?? 0) >= (ref?.current?.scrollHeight ?? 0) ) { @@ -27,11 +25,11 @@ export function useChatScrollHandle( loadPrevDay(activeChannel.id, activeChannel.type === "group"); } } - }, [messages.length, loadingMessages]); + }, [messages.length, loadingMessages, activeChannel]); useEffect(() => { const setScroll = () => { - if (ref?.current) { + if (ref?.current && activeChannel) { if (ref.current.scrollTop <= 0) { loadPrevDay(activeChannel.id, activeChannel.type === "group"); } @@ -51,6 +49,6 @@ export function useChatScrollHandle( }; ref.current?.addEventListener("scroll", setScroll); return () => ref.current?.removeEventListener("scroll", setScroll); - }, [ref, scrollOnBot]); + }, [ref, scrollOnBot, activeChannel]); return loadingMessages; }