Add communityData to useMessenger (#118)

This commit is contained in:
Szymon Szlachtowicz 2021-11-05 19:42:43 +01:00 committed by GitHub
parent 52e457991b
commit ba9770999d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 87 additions and 101 deletions

View File

@ -1,9 +1,7 @@
import React, { useMemo, useState } from "react"; import React, { useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useMessengerContext } from "../contexts/messengerProvider";
import { useNarrow } from "../contexts/narrowProvider"; import { useNarrow } from "../contexts/narrowProvider";
import { uintToImgUrl } from "../utils/uintToImgUrl";
import { Channels } from "./Channels/Channels"; import { Channels } from "./Channels/Channels";
import { ChatBody } from "./Chat/ChatBody"; import { ChatBody } from "./Chat/ChatBody";
@ -12,7 +10,6 @@ import { Community } from "./Community";
import { Members } from "./Members/Members"; import { Members } from "./Members/Members";
import { CommunityModal } from "./Modals/CommunityModal"; import { CommunityModal } from "./Modals/CommunityModal";
import { EditModal } from "./Modals/EditModal"; import { EditModal } from "./Modals/EditModal";
import { CommunitySkeleton } from "./Skeleton/CommunitySkeleton";
export function Chat() { export function Chat() {
const [showMembers, setShowMembers] = useState(true); const [showMembers, setShowMembers] = useState(true);
@ -29,36 +26,11 @@ export function Chat() {
const [isEditVisible, setIsEditVisible] = useState(false); const [isEditVisible, setIsEditVisible] = useState(false);
const showEditModal = () => setIsEditVisible(true); const showEditModal = () => setIsEditVisible(true);
const { community } = useMessengerContext();
const communityData = useMemo(() => {
if (community?.description) {
return {
id: community.publicKeyStr,
name: community.description.identity?.displayName ?? "",
icon: uintToImgUrl(
community.description?.identity?.images?.thumbnail.payload ??
new Uint8Array()
),
members: 0,
membersList: Object.keys(community.description.proto.members),
description: community.description.identity?.description ?? "",
};
} else {
return undefined;
}
}, [community]);
return ( return (
<ChatWrapper> <ChatWrapper>
{showChannels && !narrow && ( {showChannels && !narrow && (
<ChannelsWrapper> <ChannelsWrapper>
{communityData ? ( <StyledCommunity onClick={showModal} />
<StyledCommunity onClick={showModal} community={communityData} />
) : (
<SkeletonWrapper>
<CommunitySkeleton />
</SkeletonWrapper>
)}
<Channels <Channels
membersList={membersList} membersList={membersList}
groupList={groupList} groupList={groupList}
@ -71,7 +43,6 @@ export function Chat() {
<ChatBody <ChatBody
onClick={() => setShowMembers(!showMembers)} onClick={() => setShowMembers(!showMembers)}
showMembers={showMembers} showMembers={showMembers}
community={communityData}
showCommunity={!showChannels} showCommunity={!showChannels}
onCommunityClick={showModal} onCommunityClick={showModal}
onEditClick={showEditModal} onEditClick={showEditModal}
@ -88,22 +59,18 @@ export function Chat() {
setMembersList={setMembersList} setMembersList={setMembersList}
/> />
)} )}
{createChat && communityData && ( {createChat && (
<ChatCreation <ChatCreation
community={communityData}
setMembersList={setMembersList} setMembersList={setMembersList}
setGroupList={setGroupList} setGroupList={setGroupList}
setCreateChat={setCreateChat} setCreateChat={setCreateChat}
/> />
)} )}
{communityData && ( <CommunityModal
<CommunityModal isVisible={isModalVisible}
isVisible={isModalVisible} onClose={() => setIsModalVisible(false)}
onClose={() => setIsModalVisible(false)} subtitle="Public Community"
community={communityData} />
subtitle="Public Community"
/>
)}
<EditModal <EditModal
isVisible={isEditVisible} isVisible={isEditVisible}
onClose={() => setIsEditVisible(false)} onClose={() => setIsEditVisible(false)}
@ -128,10 +95,6 @@ const ChannelsWrapper = styled.div`
flex-direction: column; flex-direction: column;
`; `;
const SkeletonWrapper = styled.div`
margin-bottom: 16px;
`;
const StyledCommunity = styled(Community)` const StyledCommunity = styled(Community)`
padding: 0 0 0 8px; padding: 0 0 0 8px;
margin: 0 0 16px; margin: 0 0 16px;

View File

@ -3,7 +3,6 @@ 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 { CommunityData } from "../../models/CommunityData";
import { Channel } from "../Channels/Channel"; import { Channel } from "../Channels/Channel";
import { Community } from "../Community"; import { Community } from "../Community";
import { ChannelMenu } from "../Form/ChannelMenu"; import { ChannelMenu } from "../Form/ChannelMenu";
@ -20,7 +19,6 @@ import { ChatInput } from "./ChatInput";
import { ChatMessages } from "./ChatMessages"; import { ChatMessages } from "./ChatMessages";
interface ChatBodyProps { interface ChatBodyProps {
community: CommunityData | undefined;
onClick: () => void; onClick: () => void;
showMembers: boolean; showMembers: boolean;
showCommunity: boolean; showCommunity: boolean;
@ -34,7 +32,6 @@ interface ChatBodyProps {
} }
export function ChatBody({ export function ChatBody({
community,
onClick, onClick,
showMembers, showMembers,
showCommunity, showCommunity,
@ -46,7 +43,8 @@ export function ChatBody({
setGroupList, setGroupList,
setCreateChat, setCreateChat,
}: ChatBodyProps) { }: ChatBodyProps) {
const { messenger, messages, activeChannel } = useMessengerContext(); const { messenger, messages, activeChannel, communityData } =
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);
@ -73,9 +71,8 @@ export function ChatBody({
return ( return (
<ChatBodyWrapper className={className}> <ChatBodyWrapper className={className}>
{editGroup && community ? ( {editGroup && communityData ? (
<ChatCreation <ChatCreation
community={community}
setMembersList={setMembersList} setMembersList={setMembersList}
setGroupList={setGroupList} setGroupList={setGroupList}
setCreateChat={setCreateChat} setCreateChat={setCreateChat}
@ -88,14 +85,11 @@ export function ChatBody({
} }
> >
<ChannelWrapper className={className}> <ChannelWrapper className={className}>
{messenger && community ? ( {messenger && communityData ? (
<> <>
{(showCommunity || narrow) && ( {(showCommunity || narrow) && (
<CommunityWrap className={className}> <CommunityWrap className={className}>
<Community <Community onClick={onCommunityClick} />
onClick={onCommunityClick}
community={community}
/>
</CommunityWrap> </CommunityWrap>
)} )}
@ -127,7 +121,7 @@ export function ChatBody({
<MoreIcon /> <MoreIcon />
</MoreBtn> </MoreBtn>
</MenuWrapper> </MenuWrapper>
{!community && <Loading />} {!messenger && !communityData && <Loading />}
{showChannelMenu && ( {showChannelMenu && (
<ChannelMenu <ChannelMenu
channel={activeChannel} channel={activeChannel}
@ -140,11 +134,11 @@ export function ChatBody({
)} )}
</ChatTopbar> </ChatTopbar>
)} )}
{messenger && community ? ( {messenger ? (
<> <>
{!showChannelsList && !showMembersList && ( {!showChannelsList && !showMembersList && (
<> <>
{messenger && community ? ( {messenger && communityData ? (
<ChatMessages <ChatMessages
messages={messages} messages={messages}
activeChannelId={activeChannel.id} activeChannelId={activeChannel.id}
@ -159,7 +153,6 @@ export function ChatBody({
{showChannelsList && narrow && ( {showChannelsList && narrow && (
<NarrowChannels <NarrowChannels
community={community.name}
setShowChannels={setShowChannelsList} setShowChannels={setShowChannelsList}
membersList={membersList} membersList={membersList}
groupList={groupList} groupList={groupList}
@ -168,7 +161,6 @@ export function ChatBody({
)} )}
{showMembersList && narrow && ( {showMembersList && narrow && (
<NarrowMembers <NarrowMembers
community={community}
setShowChannels={setShowChannelsList} setShowChannels={setShowChannelsList}
setShowMembersList={setShowMembersList} setShowMembersList={setShowMembersList}
setMembersList={setMembersList} setMembersList={setMembersList}

View File

@ -4,14 +4,12 @@ import styled from "styled-components";
import { useIdentity } from "../../contexts/identityProvider"; import { useIdentity } from "../../contexts/identityProvider";
import { useMessengerContext } from "../../contexts/messengerProvider"; import { useMessengerContext } from "../../contexts/messengerProvider";
import { CommunityData } from "../../models/CommunityData";
import { buttonStyles } from "../Buttons/buttonStyle"; import { buttonStyles } from "../Buttons/buttonStyle";
import { CrossIcon } from "../Icons/CrossIcon"; import { CrossIcon } from "../Icons/CrossIcon";
import { Member } from "../Members/Member"; import { Member } from "../Members/Member";
import { SearchBlock } from "../SearchBlock"; import { SearchBlock } from "../SearchBlock";
import { textMediumStyles } from "../Text"; import { textMediumStyles } from "../Text";
interface ChatCreationProps { interface ChatCreationProps {
community: CommunityData;
setMembersList: any; setMembersList: any;
setGroupList: any; setGroupList: any;
setCreateChat: (val: boolean) => void; setCreateChat: (val: boolean) => void;
@ -19,7 +17,6 @@ interface ChatCreationProps {
} }
export function ChatCreation({ export function ChatCreation({
community,
setMembersList, setMembersList,
setGroupList, setGroupList,
setCreateChat, setCreateChat,
@ -99,7 +96,6 @@ export function ChatCreation({
/> />
{query && ( {query && (
<SearchBlock <SearchBlock
community={community}
query={query} query={query}
styledGroup={styledGroup} styledGroup={styledGroup}
setStyledGroup={setStyledGroup} setStyledGroup={setStyledGroup}

View File

@ -1,24 +1,36 @@
import React from "react"; import React from "react";
import styled from "styled-components";
import { CommunityData } from "../models/CommunityData"; import { useMessengerContext } from "../contexts/messengerProvider";
import { CommunityIdentity } from "./CommunityIdentity"; import { CommunityIdentity } from "./CommunityIdentity";
import { CommunitySkeleton } from "./Skeleton/CommunitySkeleton";
interface CommunityProps { interface CommunityProps {
community: CommunityData;
onClick: () => void; onClick: () => void;
className?: string; className?: string;
} }
export function Community({ community, onClick, className }: CommunityProps) { export function Community({ onClick, className }: CommunityProps) {
const { communityData } = useMessengerContext();
if (!communityData) {
return (
<SkeletonWrapper>
<CommunitySkeleton />
</SkeletonWrapper>
);
}
return ( return (
<> <>
<button className={className} onClick={onClick}> <button className={className} onClick={onClick}>
<CommunityIdentity <CommunityIdentity subtitle={`${communityData.members} members`} />
community={community}
subtitle={`${community.members} members`}
/>
</button> </button>
</> </>
); );
} }
const SkeletonWrapper = styled.div`
margin-bottom: 16px;
`;

View File

@ -1,26 +1,25 @@
import React from "react"; import React from "react";
import styled from "styled-components"; import styled from "styled-components";
import { CommunityData } from "../models/CommunityData"; import { useMessengerContext } from "../contexts/messengerProvider";
import { textMediumStyles } from "./Text"; import { textMediumStyles } from "./Text";
export interface CommunityIdentityProps { export interface CommunityIdentityProps {
community: CommunityData;
subtitle: string; subtitle: string;
className?: string; className?: string;
} }
export const CommunityIdentity = ({ export const CommunityIdentity = ({
community,
subtitle, subtitle,
className, className,
}: CommunityIdentityProps) => { }: CommunityIdentityProps) => {
const { communityData } = useMessengerContext();
return ( return (
<Row className={className}> <Row className={className}>
<Logo src={community.icon} alt={`${name} logo`} /> <Logo src={communityData?.icon} alt={`${communityData?.name} logo`} />
<Column> <Column>
<Name>{community.name}</Name> <Name>{communityData?.name}</Name>
<Subtitle>{subtitle}</Subtitle> <Subtitle>{subtitle}</Subtitle>
</Column> </Column>
</Row> </Row>

View File

@ -1,6 +1,7 @@
import React from "react"; import React from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider";
import { useNarrow } from "../../contexts/narrowProvider"; import { useNarrow } from "../../contexts/narrowProvider";
import { DownloadButton } from "../Buttons/DownloadButton"; import { DownloadButton } from "../Buttons/DownloadButton";
import { import {
@ -19,21 +20,23 @@ interface CommunityModalProps extends BasicModalProps, CommunityIdentityProps {}
export const CommunityModal = ({ export const CommunityModal = ({
isVisible, isVisible,
onClose, onClose,
community,
subtitle, subtitle,
}: CommunityModalProps) => { }: CommunityModalProps) => {
const narrow = useNarrow(); const narrow = useNarrow();
const { communityData } = useMessengerContext();
return ( return (
<Modal isVisible={isVisible} onClose={onClose}> <Modal isVisible={isVisible} onClose={onClose}>
<Section> <Section>
<CommunityIdentity community={community} subtitle={subtitle} /> <CommunityIdentity subtitle={subtitle} />
</Section> </Section>
<Section> <Section>
<Text>{community.description}</Text> <Text>{communityData?.description}</Text>
</Section> </Section>
<Section> <Section>
<CopyInput value={community.id} label="Community public key" /> <CopyInput
value={communityData?.id ?? ""}
label="Community public key"
/>
<Hint> <Hint>
To access this community, paste community public key in Status desktop To access this community, paste community public key in Status desktop
or mobile app. or mobile app.

View File

@ -6,7 +6,6 @@ import { Channels } from "../Channels/Channels";
import { NarrowTopbar } from "./NarrowTopbar"; import { NarrowTopbar } from "./NarrowTopbar";
interface NarrowChannelsProps { interface NarrowChannelsProps {
community: string;
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
membersList: string[]; membersList: string[];
groupList: string[][]; groupList: string[][];
@ -14,7 +13,6 @@ interface NarrowChannelsProps {
} }
export function NarrowChannels({ export function NarrowChannels({
community,
setShowChannels, setShowChannels,
membersList, membersList,
groupList, groupList,
@ -22,7 +20,7 @@ export function NarrowChannels({
}: NarrowChannelsProps) { }: NarrowChannelsProps) {
return ( return (
<ListWrapper> <ListWrapper>
<NarrowTopbar list="Channels" community={community} /> <NarrowTopbar list="Channels" />
<Channels <Channels
onCommunityClick={() => setShowChannels(false)} onCommunityClick={() => setShowChannels(false)}
membersList={membersList} membersList={membersList}

View File

@ -1,27 +1,24 @@
import React from "react"; import React from "react";
import styled from "styled-components"; import styled from "styled-components";
import { CommunityData } from "../../models/CommunityData";
import { MembersList } from "../Members/MembersList"; import { MembersList } from "../Members/MembersList";
import { NarrowTopbar } from "./NarrowTopbar"; import { NarrowTopbar } from "./NarrowTopbar";
interface NarrowMembersProps { interface NarrowMembersProps {
community: CommunityData;
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
setShowMembersList: (val: boolean) => void; setShowMembersList: (val: boolean) => void;
setMembersList: any; setMembersList: any;
} }
export function NarrowMembers({ export function NarrowMembers({
community,
setShowChannels, setShowChannels,
setShowMembersList, setShowMembersList,
setMembersList, setMembersList,
}: NarrowMembersProps) { }: NarrowMembersProps) {
return ( return (
<ListWrapper> <ListWrapper>
<NarrowTopbar list="Community members" community={community.name} /> <NarrowTopbar list="Community members" />
<MembersList <MembersList
setShowChannels={setShowChannels} setShowChannels={setShowChannels}
setShowMembers={setShowMembersList} setShowMembers={setShowMembersList}

View File

@ -1,16 +1,18 @@
import React from "react"; import React from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider";
interface NarrowTopbarProps { interface NarrowTopbarProps {
list: string; list: string;
community: string;
} }
export function NarrowTopbar({ list, community }: NarrowTopbarProps) { export function NarrowTopbar({ list }: NarrowTopbarProps) {
const { communityData } = useMessengerContext();
return ( return (
<TopbarWrapper> <TopbarWrapper>
<Heading>{list}</Heading> <Heading>{list}</Heading>
<SubHeading>{community}</SubHeading> <SubHeading>{communityData?.name}</SubHeading>
</TopbarWrapper> </TopbarWrapper>
); );
} }

View File

@ -1,29 +1,33 @@
import React, { useMemo } from "react"; import React, { useMemo } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { CommunityData } from "../models/CommunityData"; import { useMessengerContext } from "../contexts/messengerProvider";
import { Channel } from "./Channels/Channel"; import { Channel } from "./Channels/Channel";
import { ContactsList } from "./Chat/ChatCreation"; import { ContactsList } from "./Chat/ChatCreation";
interface SearchBlockProps { interface SearchBlockProps {
community: CommunityData;
query: string; query: string;
styledGroup: string[]; styledGroup: string[];
setStyledGroup: React.Dispatch<React.SetStateAction<string[]>>; setStyledGroup: React.Dispatch<React.SetStateAction<string[]>>;
} }
export const SearchBlock = ({ export const SearchBlock = ({
community,
query, query,
styledGroup, styledGroup,
setStyledGroup, setStyledGroup,
}: SearchBlockProps) => { }: SearchBlockProps) => {
const { communityData } = useMessengerContext();
if (!communityData) {
return null;
}
const searchList = useMemo(() => { const searchList = useMemo(() => {
return community.membersList return communityData.membersList
.filter((member) => member.includes(query)) .filter((member) => member.includes(query))
.filter((member) => !styledGroup.includes(member)); .filter((member) => !styledGroup.includes(member));
}, [query, styledGroup, community.membersList]); }, [query, styledGroup, communityData.membersList]);
const addMember = (member: string) => { const addMember = (member: string) => {
setStyledGroup((prevMembers: string[]) => { setStyledGroup((prevMembers: string[]) => {
@ -40,7 +44,7 @@ export const SearchBlock = ({
return ( return (
<SearchContacts> <SearchContacts>
<ContactsList> <ContactsList>
{community.membersList {communityData.membersList
.filter((member) => member.includes(query)) .filter((member) => member.includes(query))
.filter((member) => !styledGroup.includes(member)) .filter((member) => !styledGroup.includes(member))
.map((member) => ( .map((member) => (

View File

@ -11,7 +11,7 @@ const MessengerContext = createContext<MessengerType>({
clearNotifications: () => undefined, clearNotifications: () => undefined,
loadPrevDay: async () => undefined, loadPrevDay: async () => undefined,
loadingMessages: false, loadingMessages: false,
community: undefined, communityData: undefined,
contacts: [], contacts: [],
activeChannel: { activeChannel: {
id: "", id: "",

View File

@ -9,9 +9,11 @@ import {
import { ChannelData } from "../../models/ChannelData"; import { ChannelData } from "../../models/ChannelData";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { CommunityData } from "../../models/CommunityData";
import { Contact } from "../../models/Contact"; import { Contact } from "../../models/Contact";
import { createCommunity } from "../../utils/createCommunity"; import { createCommunity } from "../../utils/createCommunity";
import { createMessenger } from "../../utils/createMessenger"; import { createMessenger } from "../../utils/createMessenger";
import { uintToImgUrl } from "../../utils/uintToImgUrl";
import { useLoadPrevDay } from "./useLoadPrevDay"; import { useLoadPrevDay } from "./useLoadPrevDay";
import { useMessages } from "./useMessages"; import { useMessages } from "./useMessages";
@ -27,7 +29,7 @@ export type MessengerType = {
clearNotifications: (id: string) => void; clearNotifications: (id: string) => void;
loadPrevDay: (id: string) => Promise<void>; loadPrevDay: (id: string) => Promise<void>;
loadingMessages: boolean; loadingMessages: boolean;
community: Community | undefined; communityData: CommunityData | undefined;
contacts: Contact[]; contacts: Contact[];
channels: ChannelData[]; channels: ChannelData[];
activeChannel: ChannelData; activeChannel: ChannelData;
@ -144,6 +146,24 @@ export function useMessenger(
if (channels.length > 0) setActiveChannel(channels[0]); if (channels.length > 0) setActiveChannel(channels[0]);
}, [channels]); }, [channels]);
const communityData = useMemo(() => {
if (community?.description) {
return {
id: community.publicKeyStr,
name: community.description.identity?.displayName ?? "",
icon: uintToImgUrl(
community.description?.identity?.images?.thumbnail.payload ??
new Uint8Array()
),
members: 0,
membersList: Object.keys(community.description.proto.members),
description: community.description.identity?.description ?? "",
};
} else {
return undefined;
}
}, [community]);
return { return {
messenger, messenger,
messages, messages,
@ -152,7 +172,7 @@ export function useMessenger(
clearNotifications, clearNotifications,
loadPrevDay, loadPrevDay,
loadingMessages, loadingMessages,
community, communityData,
contacts, contacts,
channels, channels,
activeChannel, activeChannel,