Fix groupChat creation, add bold channel on new message (#123)

Signed-off-by: Szymon Szlachtowicz <szymon@TPL014-SS-MBP17.local>

Co-authored-by: Szymon Szlachtowicz <szymon@TPL014-SS-MBP17.local>
This commit is contained in:
Szymon Szlachtowicz 2021-11-10 11:42:26 +01:00 committed by GitHub
parent 70e4f34cfe
commit a03a102cd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 67 deletions

View File

@ -29,7 +29,7 @@ export function Channel({
() => (narrow && !activeView ? "narrow" : activeView ? "active" : ""), () => (narrow && !activeView ? "narrow" : activeView ? "active" : ""),
[narrow] [narrow]
); );
const mention = false;
return ( return (
<ChannelWrapper <ChannelWrapper
className={ className={
@ -73,7 +73,7 @@ export function Channel({
)} )}
</ChannelTextInfo> </ChannelTextInfo>
</ChannelInfo> </ChannelInfo>
{notification && notification > 0 && !activeView && ( {notification && notification > 0 && !activeView && mention && (
<NotificationBagde>{notification}</NotificationBagde> <NotificationBagde>{notification}</NotificationBagde>
)} )}
{isMuted && !notification && <MutedIcon />} {isMuted && !notification && <MutedIcon />}

View File

@ -43,8 +43,7 @@ export function ChatBody({
setGroupList, setGroupList,
setCreateChat, setCreateChat,
}: ChatBodyProps) { }: ChatBodyProps) {
const { messenger, messages, activeChannel, communityData } = const { messenger, activeChannel, communityData } = useMessengerContext();
useMessengerContext();
const narrow = useNarrow(); const narrow = useNarrow();
const [showChannelsList, setShowChannelsList] = useState(false); const [showChannelsList, setShowChannelsList] = useState(false);
const [showMembersList, setShowMembersList] = useState(false); const [showMembersList, setShowMembersList] = useState(false);
@ -125,7 +124,6 @@ export function ChatBody({
{showChannelMenu && ( {showChannelMenu && (
<ChannelMenu <ChannelMenu
channel={activeChannel} channel={activeChannel}
messages={messages}
switchMemberList={switchMemberList} switchMemberList={switchMemberList}
setShowChannelMenu={setShowChannelMenu} setShowChannelMenu={setShowChannelMenu}
setEditGroup={setEditGroup} setEditGroup={setEditGroup}
@ -140,11 +138,7 @@ export function ChatBody({
{!showChannelsList && !showMembersList && ( {!showChannelsList && !showMembersList && (
<> <>
{messenger && communityData ? ( {messenger && communityData ? (
<ChatMessages <ChatMessages />
messages={messages}
activeChannelId={activeChannel.id}
channel={activeChannel}
/>
) : ( ) : (
<LoadingSkeleton /> <LoadingSkeleton />
)} )}

View File

@ -39,8 +39,7 @@ export function ChatCreation({
}; };
const removeMember = (member: string) => { const removeMember = (member: string) => {
const idx = styledGroup.indexOf(member); setStyledGroup((prev) => prev.filter((e) => e != member));
setStyledGroup(styledGroup.splice(idx, 1));
}; };
const createChat = (group: string[]) => { const createChat = (group: string[]) => {
@ -117,12 +116,16 @@ export function ChatCreation({
Confirm Confirm
</CreationBtn> </CreationBtn>
</CreationBar> </CreationBar>
{!editGroup && !query && styledGroup.length === 0 && ( {!editGroup && !query && (
<Contacts> <Contacts>
<ContactsHeading>Contacts</ContactsHeading> <ContactsHeading>Contacts</ContactsHeading>
<ContactsList> <ContactsList>
{contacts {contacts
.filter((e) => e.id != bufToHex(identity.publicKey)) .filter(
(e) =>
e.id != bufToHex(identity.publicKey) &&
!styledGroup.includes(e.id)
)
.map((contact) => ( .map((contact) => (
<Member <Member
key={contact.id} key={contact.id}

View File

@ -2,8 +2,8 @@ import React, { useMemo, useRef, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useBlockedUsers } from "../../contexts/blockedUsersProvider"; import { useBlockedUsers } from "../../contexts/blockedUsersProvider";
import { useMessengerContext } from "../../contexts/messengerProvider";
import { useChatScrollHandle } from "../../hooks/useChatScrollHandle"; import { useChatScrollHandle } from "../../hooks/useChatScrollHandle";
import { ChannelData } from "../../models/ChannelData";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { equalDate } from "../../utils"; import { equalDate } from "../../utils";
import { EmptyChannel } from "../Channels/EmptyChannel"; import { EmptyChannel } from "../Channels/EmptyChannel";
@ -70,25 +70,17 @@ function ChatUiMessage({
); );
} }
type ChatMessagesProps = { export function ChatMessages() {
messages: ChatMessage[]; const { messages, activeChannel } = useMessengerContext();
activeChannelId: string;
channel: ChannelData;
};
export function ChatMessages({
messages,
activeChannelId,
channel,
}: ChatMessagesProps) {
const ref = useRef<HTMLHeadingElement>(null); const ref = useRef<HTMLHeadingElement>(null);
const loadingMessages = useChatScrollHandle(messages, ref, activeChannelId); const loadingMessages = useChatScrollHandle(messages, ref, activeChannel.id);
const { blockedUsers } = useBlockedUsers(); const { blockedUsers } = useBlockedUsers();
const shownMessages = useMemo(() => { const shownMessages = useMemo(
return messages.filter((message) => !blockedUsers.includes(message.sender)); () => messages.filter((message) => !blockedUsers.includes(message.sender)),
}, [blockedUsers, messages.length]); [blockedUsers, messages]
);
const [image, setImage] = useState(""); const [image, setImage] = useState("");
const [link, setLink] = useState(""); const [link, setLink] = useState("");
@ -100,24 +92,22 @@ export function ChatMessages({
image={image} image={image}
/> />
<LinkModal isVisible={!!link} onClose={() => setLink("")} link={link} /> <LinkModal isVisible={!!link} onClose={() => setLink("")} link={link} />
<EmptyChannel channel={channel} /> <EmptyChannel channel={activeChannel} />
{loadingMessages && ( {loadingMessages && (
<LoadingWrapper> <LoadingWrapper>
<LoadingIcon className="message" /> <LoadingIcon className="message" />
</LoadingWrapper> </LoadingWrapper>
)} )}
{shownMessages.map((message, idx) => { {shownMessages.map((message, idx) => (
return (
<ChatUiMessage <ChatUiMessage
key={message.date.getTime()} key={message.date.getTime().toString() + message.content}
message={message} message={message}
idx={idx} idx={idx}
prevMessage={shownMessages[idx - 1]} prevMessage={shownMessages[idx - 1]}
setLink={setLink} setLink={setLink}
setImage={setImage} setImage={setImage}
/> />
); ))}
})}
</MessagesWrapper> </MessagesWrapper>
); );
} }

View File

@ -4,10 +4,8 @@ import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider"; import { useMessengerContext } from "../../contexts/messengerProvider";
import { useNarrow } from "../../contexts/narrowProvider"; import { useNarrow } from "../../contexts/narrowProvider";
import { ChannelData } from "../../models/ChannelData"; import { ChannelData } from "../../models/ChannelData";
import { ChatMessage } from "../../models/ChatMessage";
import { AddMemberIconSvg } from "../Icons/AddMemberIcon"; import { AddMemberIconSvg } from "../Icons/AddMemberIcon";
import { CheckSvg } from "../Icons/CheckIcon"; import { CheckSvg } from "../Icons/CheckIcon";
import { ClearSvg } from "../Icons/ClearIcon";
import { EgitGroupSvg } from "../Icons/EditGroupIcon"; import { EgitGroupSvg } from "../Icons/EditGroupIcon";
import { LeftIconSvg } from "../Icons/LeftIcon"; import { LeftIconSvg } from "../Icons/LeftIcon";
import { MembersSmallSvg } from "../Icons/MembersSmallIcon"; import { MembersSmallSvg } from "../Icons/MembersSmallIcon";
@ -17,7 +15,6 @@ import { DropdownMenu, MenuItem, MenuText } from "./DropdownMenu";
interface ChannelMenuProps { interface ChannelMenuProps {
channel: ChannelData; channel: ChannelData;
messages: ChatMessage[];
switchMemberList: () => void; switchMemberList: () => void;
setShowChannelMenu: (val: boolean) => void; setShowChannelMenu: (val: boolean) => void;
setEditGroup: (val: boolean) => void; setEditGroup: (val: boolean) => void;
@ -27,7 +24,6 @@ interface ChannelMenuProps {
export const ChannelMenu = ({ export const ChannelMenu = ({
channel, channel,
messages,
switchMemberList, switchMemberList,
setShowChannelMenu, setShowChannelMenu,
setEditGroup, setEditGroup,
@ -81,10 +77,6 @@ export const ChannelMenu = ({
<CheckSvg width={16} height={16} /> <CheckSvg width={16} height={16} />
<MenuText>Mark as Read</MenuText> <MenuText>Mark as Read</MenuText>
</MenuItem> </MenuItem>
<MenuItem onClick={() => messages.length === 0}>
<ClearSvg width={16} height={16} />
<MenuText>Clear History</MenuText>
</MenuItem>
{channel.type === "group" && ( {channel.type === "group" && (
<MenuSection> <MenuSection>
{" "} {" "}

View File

@ -17,17 +17,13 @@ export const SearchBlock = ({
styledGroup, styledGroup,
setStyledGroup, setStyledGroup,
}: SearchBlockProps) => { }: SearchBlockProps) => {
const { communityData } = useMessengerContext(); const { contacts } = useMessengerContext();
if (!communityData) {
return null;
}
const searchList = useMemo(() => { const searchList = useMemo(() => {
return communityData.membersList return contacts
.filter((member) => member.includes(query)) .filter((member) => member.id.includes(query))
.filter((member) => !styledGroup.includes(member)); .filter((member) => !styledGroup.includes(member.id));
}, [query, styledGroup, communityData.membersList]); }, [query, styledGroup, contacts]);
const addMember = (member: string) => { const addMember = (member: string) => {
setStyledGroup((prevMembers: string[]) => { setStyledGroup((prevMembers: string[]) => {
@ -44,20 +40,20 @@ export const SearchBlock = ({
return ( return (
<SearchContacts> <SearchContacts>
<ContactsList> <ContactsList>
{communityData.membersList {contacts
.filter((member) => member.includes(query)) .filter((member) => member.id.includes(query))
.filter((member) => !styledGroup.includes(member)) .filter((member) => !styledGroup.includes(member.id))
.map((member) => ( .map((member) => (
<Channel <Channel
key={member} key={member.id}
channel={{ channel={{
id: member, id: member.id,
name: member.slice(0, 10), name: member.id.slice(0, 10),
type: "dm", type: "dm",
}} }}
isActive={false} isActive={false}
isMuted={false} isMuted={false}
onClick={() => addMember(member)} onClick={() => addMember(member.id)}
/> />
))} ))}
</ContactsList> </ContactsList>

View File

@ -148,6 +148,9 @@ export function useMessenger(
const communityData = useMemo(() => { const communityData = useMemo(() => {
if (community?.description) { if (community?.description) {
Object.keys(community.description.proto.members).forEach((contact) =>
contactsClass?.addContact(contact)
);
return { return {
id: community.publicKeyStr, id: community.publicKeyStr,
name: community.description.identity?.displayName ?? "", name: community.description.identity?.displayName ?? "",