Add messenger context (#101)

This commit is contained in:
Szymon Szlachtowicz 2021-10-29 10:48:52 +02:00 committed by GitHub
parent 892e06fdb1
commit 08e7ea2ab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 114 additions and 119 deletions

View File

@ -1,14 +1,13 @@
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider";
import { ChannelData } from "../../models/ChannelData"; import { ChannelData } from "../../models/ChannelData";
import { EditIcon } from "../Icons/EditIcon"; import { EditIcon } from "../Icons/EditIcon";
import { Channel } from "./Channel"; import { Channel } from "./Channel";
interface ChannelsProps { interface ChannelsProps {
notifications: { [id: string]: number };
clearNotifications: (id: string) => void;
onCommunityClick: (val: ChannelData) => void; onCommunityClick: (val: ChannelData) => void;
activeChannelId: string; activeChannelId: string;
channels: ChannelData[]; channels: ChannelData[];
@ -17,14 +16,14 @@ interface ChannelsProps {
} }
export function Channels({ export function Channels({
notifications,
onCommunityClick, onCommunityClick,
clearNotifications,
activeChannelId, activeChannelId,
channels, channels,
membersList, membersList,
setCreateChat, setCreateChat,
}: ChannelsProps) { }: ChannelsProps) {
const { clearNotifications, notifications } = useMessengerContext();
useEffect(() => { useEffect(() => {
const channel = channels.find((channel) => channel.id === activeChannelId); const channel = channels.find((channel) => channel.id === activeChannelId);
if (channel) { if (channel) {

View File

@ -2,11 +2,10 @@ import React, { useEffect, useMemo, useState } from "react";
import { Identity } from "status-communities/dist/cjs"; import { Identity } from "status-communities/dist/cjs";
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 { useMessenger } from "../hooks/messenger/useMessenger";
import { ChannelData } from "../models/ChannelData"; import { ChannelData } from "../models/ChannelData";
import { Metadata } from "../models/Metadata"; import { Metadata } from "../models/Metadata";
import { Theme } from "../styles/themes";
import { uintToImgUrl } from "../utils/uintToImgUrl"; import { uintToImgUrl } from "../utils/uintToImgUrl";
import { Channels } from "./Channels/Channels"; import { Channels } from "./Channels/Channels";
@ -18,23 +17,20 @@ import { CommunityModal } from "./Modals/CommunityModal";
import { CommunitySkeleton } from "./Skeleton/CommunitySkeleton"; import { CommunitySkeleton } from "./Skeleton/CommunitySkeleton";
interface ChatProps { interface ChatProps {
theme: Theme;
communityKey: string; communityKey: string;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
identity: Identity; identity: Identity;
setActiveChannel: (channel: ChannelData) => void;
activeChannel: ChannelData;
} }
export function Chat({ export function Chat({
theme,
communityKey, communityKey,
fetchMetadata, fetchMetadata,
identity, identity,
setActiveChannel,
activeChannel,
}: ChatProps) { }: ChatProps) {
const [activeChannel, setActiveChannel] = useState<ChannelData>({
id: "",
name: "",
description: "",
});
const [showMembers, setShowMembers] = useState(true); const [showMembers, setShowMembers] = useState(true);
const [showChannels, setShowChannels] = useState(true); const [showChannels, setShowChannels] = useState(true);
const [membersList, setMembersList] = useState([]); const [membersList, setMembersList] = useState([]);
@ -42,21 +38,11 @@ export function Chat({
const narrow = useNarrow(); const narrow = useNarrow();
const {
messenger,
messages,
sendMessage,
notifications,
clearNotifications,
loadPrevDay,
loadingMessages,
community,
contacts,
} = useMessenger(activeChannel?.id ?? "", communityKey, identity);
const [isModalVisible, setIsModalVisible] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false);
const showModal = () => setIsModalVisible(true); const showModal = () => setIsModalVisible(true);
const { community } = useMessengerContext();
const communityData = useMemo(() => { const communityData = useMemo(() => {
if (community?.description) { if (community?.description) {
return { return {
@ -103,8 +89,6 @@ export function Chat({
<CommunitySkeleton /> <CommunitySkeleton />
)} )}
<Channels <Channels
notifications={notifications}
clearNotifications={clearNotifications}
onCommunityClick={(e) => setActiveChannel(e)} onCommunityClick={(e) => setActiveChannel(e)}
activeChannelId={activeChannel?.id ?? ""} activeChannelId={activeChannel?.id ?? ""}
channels={channels} channels={channels}
@ -117,24 +101,15 @@ export function Chat({
{!createChat && ( {!createChat && (
<ChatBody <ChatBody
identity={identity} identity={identity}
contacts={contacts}
theme={theme}
channel={activeChannel} channel={activeChannel}
messenger={messenger}
messages={messages}
sendMessage={sendMessage}
notifications={notifications}
setActiveChannel={setActiveChannel} setActiveChannel={setActiveChannel}
activeChannelId={activeChannel.id} activeChannelId={activeChannel.id}
onClick={() => setShowMembers(!showMembers)} onClick={() => setShowMembers(!showMembers)}
showMembers={showMembers} showMembers={showMembers}
community={communityData} community={communityData}
showCommunity={!showChannels} showCommunity={!showChannels}
loadPrevDay={() => loadPrevDay(activeChannel.id)}
onCommunityClick={showModal} onCommunityClick={showModal}
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
loadingMessages={loadingMessages}
clearNotifications={clearNotifications}
channels={channels} channels={channels}
membersList={membersList} membersList={membersList}
setMembersList={setMembersList} setMembersList={setMembersList}
@ -144,7 +119,6 @@ export function Chat({
{showMembers && !narrow && !createChat && ( {showMembers && !narrow && !createChat && (
<Members <Members
identity={identity} identity={identity}
contacts={contacts}
setShowChannels={setShowChannels} setShowChannels={setShowChannels}
setMembersList={setMembersList} setMembersList={setMembersList}
/> />

View File

@ -2,13 +2,11 @@ import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Identity } from "status-communities/dist/cjs"; import { Identity } from "status-communities/dist/cjs";
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 { ChannelData } from "../../models/ChannelData"; import { ChannelData } from "../../models/ChannelData";
import { ChatMessage } from "../../models/ChatMessage";
import { CommunityData } from "../../models/CommunityData"; import { CommunityData } from "../../models/CommunityData";
import { Contact } from "../../models/Contact";
import { Metadata } from "../../models/Metadata"; import { Metadata } from "../../models/Metadata";
import { Theme } from "../../styles/themes";
import { Channel } from "../Channels/Channel"; import { Channel } from "../Channels/Channel";
import { EmptyChannel } from "../Channels/EmptyChannel"; import { EmptyChannel } from "../Channels/EmptyChannel";
import { Community } from "../Community"; import { Community } from "../Community";
@ -24,24 +22,15 @@ import { ChatMessages } from "./ChatMessages";
interface ChatBodyProps { interface ChatBodyProps {
identity: Identity; identity: Identity;
contacts: Contact[];
theme: Theme;
channel: ChannelData; channel: ChannelData;
community: CommunityData | undefined; community: CommunityData | undefined;
messenger: any;
messages: ChatMessage[];
sendMessage: (text: string, image?: Uint8Array) => void;
onClick: () => void; onClick: () => void;
showMembers: boolean; showMembers: boolean;
showCommunity: boolean; showCommunity: boolean;
notifications: { [id: string]: number };
setActiveChannel: (val: ChannelData) => void; setActiveChannel: (val: ChannelData) => void;
activeChannelId: string; activeChannelId: string;
loadPrevDay: () => void;
onCommunityClick: () => void; onCommunityClick: () => void;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
loadingMessages: boolean;
clearNotifications: (id: string) => void;
channels: ChannelData[]; channels: ChannelData[];
membersList: string[]; membersList: string[];
setMembersList: any; setMembersList: any;
@ -50,29 +39,21 @@ interface ChatBodyProps {
export function ChatBody({ export function ChatBody({
identity, identity,
contacts,
theme,
channel, channel,
community, community,
messenger,
messages,
sendMessage,
onClick, onClick,
showMembers, showMembers,
showCommunity, showCommunity,
notifications,
setActiveChannel, setActiveChannel,
activeChannelId, activeChannelId,
loadPrevDay,
onCommunityClick, onCommunityClick,
fetchMetadata, fetchMetadata,
loadingMessages,
clearNotifications,
channels, channels,
membersList, membersList,
setMembersList, setMembersList,
setCreateChat, setCreateChat,
}: ChatBodyProps) { }: ChatBodyProps) {
const { messenger, messages } = 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);
@ -143,9 +124,8 @@ export function ChatBody({
messenger && community ? ( messenger && community ? (
<ChatMessages <ChatMessages
messages={messages} messages={messages}
loadPrevDay={loadPrevDay} activeChannelId={activeChannelId}
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
loadingMessages={loadingMessages}
/> />
) : ( ) : (
<LoadingSkeleton /> <LoadingSkeleton />
@ -153,7 +133,7 @@ export function ChatBody({
) : ( ) : (
<EmptyChannel channel={channel} /> <EmptyChannel channel={channel} />
)} )}
<ChatInput addMessage={sendMessage} theme={theme} /> <ChatInput />
</> </>
)} )}
@ -161,11 +141,9 @@ export function ChatBody({
<NarrowChannels <NarrowChannels
channels={channels} channels={channels}
community={community.name} community={community.name}
notifications={notifications}
setActiveChannel={setActiveChannel} setActiveChannel={setActiveChannel}
setShowChannels={setShowChannelsList} setShowChannels={setShowChannelsList}
activeChannelId={activeChannelId} activeChannelId={activeChannelId}
clearNotifications={clearNotifications}
membersList={membersList} membersList={membersList}
setCreateChat={setCreateChat} setCreateChat={setCreateChat}
/> />
@ -173,7 +151,6 @@ export function ChatBody({
{showMembersList && narrow && ( {showMembersList && narrow && (
<NarrowMembers <NarrowMembers
identity={identity} identity={identity}
contacts={contacts}
community={community} community={community}
setShowChannels={setShowChannelsList} setShowChannels={setShowChannelsList}
setShowMembersList={setShowMembersList} setShowMembersList={setShowMembersList}
@ -184,7 +161,7 @@ export function ChatBody({
) : ( ) : (
<> <>
<LoadingSkeleton /> <LoadingSkeleton />
<ChatInput addMessage={sendMessage} theme={theme} /> <ChatInput />
</> </>
)} )}
</ChatBodyWrapper> </ChatBodyWrapper>

View File

@ -1,7 +1,8 @@
import { Picker } from "emoji-mart"; import { Picker } from "emoji-mart";
import React, { useCallback, useEffect, useMemo, useState } from "react"; import React, { useCallback, useEffect, useMemo, useState } from "react";
import styled from "styled-components"; import styled, { useTheme } from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider";
import { useLow } from "../../contexts/narrowProvider"; import { useLow } from "../../contexts/narrowProvider";
import { lightTheme, Theme } from "../../styles/themes"; import { lightTheme, Theme } from "../../styles/themes";
import { uintToImgUrl } from "../../utils/uintToImgUrl"; import { uintToImgUrl } from "../../utils/uintToImgUrl";
@ -12,12 +13,9 @@ import { StickerIcon } from "../Icons/StickerIcon";
import "emoji-mart/css/emoji-mart.css"; import "emoji-mart/css/emoji-mart.css";
import { Modal } from "../Modals/Modal"; import { Modal } from "../Modals/Modal";
type ChatInputProps = { export function ChatInput() {
theme: Theme; const { sendMessage } = useMessengerContext();
addMessage: (message: string, image?: Uint8Array) => void; const theme = useTheme() as Theme;
};
export function ChatInput({ theme, addMessage }: ChatInputProps) {
const [content, setContent] = useState(""); const [content, setContent] = useState("");
const [showEmoji, setShowEmoji] = useState(false); const [showEmoji, setShowEmoji] = useState(false);
const [inputHeight, setInputHeight] = useState(40); const [inputHeight, setInputHeight] = useState(40);
@ -61,7 +59,7 @@ export function ChatInput({ theme, addMessage }: ChatInputProps) {
e.preventDefault(); e.preventDefault();
(e.target as HTMLTextAreaElement).style.height = "40px"; (e.target as HTMLTextAreaElement).style.height = "40px";
setInputHeight(40); setInputHeight(40);
addMessage(content, imageUint); sendMessage(content, imageUint);
setImageUint(undefined); setImageUint(undefined);
setContent(""); setContent("");
} }

View File

@ -1,6 +1,7 @@
import React, { useEffect, useMemo, useRef, useState } from "react"; import React, { useEffect, useMemo, useRef, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { Metadata } from "../../models/Metadata"; import { Metadata } from "../../models/Metadata";
import { equalDate } from "../../utils"; import { equalDate } from "../../utils";
@ -14,17 +15,16 @@ import { ChatMessageContent } from "./ChatMessageContent";
type ChatMessagesProps = { type ChatMessagesProps = {
messages: ChatMessage[]; messages: ChatMessage[];
loadPrevDay: () => void; activeChannelId: string;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
loadingMessages: boolean;
}; };
export function ChatMessages({ export function ChatMessages({
messages, messages,
loadPrevDay, activeChannelId,
fetchMetadata, fetchMetadata,
loadingMessages,
}: ChatMessagesProps) { }: ChatMessagesProps) {
const { loadPrevDay, loadingMessages } = useMessengerContext();
const [scrollOnBot, setScrollOnBot] = useState(true); const [scrollOnBot, setScrollOnBot] = useState(true);
const ref = useRef<HTMLHeadingElement>(null); const ref = useRef<HTMLHeadingElement>(null);
const today = useMemo(() => new Date(), []); const today = useMemo(() => new Date(), []);
@ -40,7 +40,7 @@ export function ChatMessages({
(ref?.current?.clientHeight ?? 0) >= (ref?.current?.scrollHeight ?? 0) (ref?.current?.clientHeight ?? 0) >= (ref?.current?.scrollHeight ?? 0)
) { ) {
setScrollOnBot(true); setScrollOnBot(true);
loadPrevDay(); loadPrevDay(activeChannelId);
} }
} }
}, [messages, messages.length, loadingMessages]); }, [messages, messages.length, loadingMessages]);
@ -49,7 +49,7 @@ export function ChatMessages({
const setScroll = () => { const setScroll = () => {
if (ref && ref.current) { if (ref && ref.current) {
if (ref.current.scrollTop <= 0) { if (ref.current.scrollTop <= 0) {
loadPrevDay(); loadPrevDay(activeChannelId);
} }
if ( if (
ref.current.scrollTop + ref.current.clientHeight == ref.current.scrollTop + ref.current.clientHeight ==

View File

@ -1,33 +1,41 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { Identity } from "status-communities/dist/cjs"; import { Identity } from "status-communities/dist/cjs";
import { MessengerProvider } from "../contexts/messengerProvider";
import { ChannelData } from "../models/ChannelData";
import { Metadata } from "../models/Metadata"; import { Metadata } from "../models/Metadata";
import { Theme } from "../styles/themes";
import { Chat } from "./Chat"; import { Chat } from "./Chat";
import { IdentityLoader } from "./Form/IdentityLoader"; import { IdentityLoader } from "./Form/IdentityLoader";
interface ChatLoaderProps { interface ChatLoaderProps {
theme: Theme;
communityKey: string; communityKey: string;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
} }
export function ChatLoader({ export function ChatLoader({ communityKey, fetchMetadata }: ChatLoaderProps) {
theme,
communityKey,
fetchMetadata,
}: ChatLoaderProps) {
const [identity, setIdentity] = useState<Identity | undefined>(undefined); const [identity, setIdentity] = useState<Identity | undefined>(undefined);
const [activeChannel, setActiveChannel] = useState<ChannelData>({
id: "",
name: "",
description: "",
});
if (identity) { if (identity) {
return ( return (
<MessengerProvider
identity={identity}
communityKey={communityKey}
activeChannel={activeChannel.id}
>
<Chat <Chat
communityKey={communityKey} communityKey={communityKey}
activeChannel={activeChannel}
setActiveChannel={setActiveChannel}
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
theme={theme}
identity={identity} identity={identity}
/> />
</MessengerProvider>
); );
} else { } else {
return <IdentityLoader setIdentity={setIdentity} />; return <IdentityLoader setIdentity={setIdentity} />;

View File

@ -2,20 +2,16 @@ import React from "react";
import { Identity } from "status-communities/dist/cjs"; import { Identity } from "status-communities/dist/cjs";
import styled from "styled-components"; import styled from "styled-components";
import { Contact } from "../../models/Contact";
import { MembersList } from "./MembersList"; import { MembersList } from "./MembersList";
interface MembersProps { interface MembersProps {
identity: Identity; identity: Identity;
contacts: Contact[];
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
setMembersList: any; setMembersList: any;
} }
export function Members({ export function Members({
identity, identity,
contacts,
setShowChannels, setShowChannels,
setMembersList, setMembersList,
}: MembersProps) { }: MembersProps) {
@ -24,7 +20,6 @@ export function Members({
<MemberHeading>Members</MemberHeading> <MemberHeading>Members</MemberHeading>
<MembersList <MembersList
identity={identity} identity={identity}
contacts={contacts}
setShowChannels={setShowChannels} setShowChannels={setShowChannels}
setMembersList={setMembersList} setMembersList={setMembersList}
/> />

View File

@ -3,14 +3,13 @@ import { Identity, utils } from "status-communities/dist/cjs";
import { bufToHex } from "status-communities/dist/cjs/utils"; import { bufToHex } from "status-communities/dist/cjs/utils";
import styled from "styled-components"; import styled from "styled-components";
import { Contact } from "../../models/Contact"; import { useMessengerContext } from "../../contexts/messengerProvider";
import { UserIcon } from "../Icons/UserIcon"; import { UserIcon } from "../Icons/UserIcon";
import { Member, MemberData, MemberIcon } from "./Member"; import { Member, MemberData, MemberIcon } from "./Member";
interface MembersListProps { interface MembersListProps {
identity: Identity; identity: Identity;
contacts: Contact[];
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
setShowMembers?: (val: boolean) => void; setShowMembers?: (val: boolean) => void;
setMembersList: any; setMembersList: any;
@ -18,11 +17,12 @@ interface MembersListProps {
export function MembersList({ export function MembersList({
identity, identity,
contacts,
setShowChannels, setShowChannels,
setShowMembers, setShowMembers,
setMembersList, setMembersList,
}: MembersListProps) { }: MembersListProps) {
const { contacts } = useMessengerContext();
return ( return (
<MembersListWrap> <MembersListWrap>
<MemberCategory> <MemberCategory>

View File

@ -8,11 +8,9 @@ import { NarrowTopbar } from "./NarrowTopbar";
interface NarrowChannelsProps { interface NarrowChannelsProps {
community: string; community: string;
notifications: { [id: string]: number };
setActiveChannel: (val: ChannelData) => void; setActiveChannel: (val: ChannelData) => void;
activeChannelId: string; activeChannelId: string;
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
clearNotifications: (id: string) => void;
channels: ChannelData[]; channels: ChannelData[];
membersList: string[]; membersList: string[];
setCreateChat: (val: boolean) => void; setCreateChat: (val: boolean) => void;
@ -20,11 +18,9 @@ interface NarrowChannelsProps {
export function NarrowChannels({ export function NarrowChannels({
community, community,
notifications,
setActiveChannel, setActiveChannel,
activeChannelId, activeChannelId,
setShowChannels, setShowChannels,
clearNotifications,
channels, channels,
membersList, membersList,
setCreateChat, setCreateChat,
@ -33,8 +29,6 @@ export function NarrowChannels({
<ListWrapper> <ListWrapper>
<NarrowTopbar list="Channels" community={community} /> <NarrowTopbar list="Channels" community={community} />
<Channels <Channels
notifications={notifications}
clearNotifications={clearNotifications}
onCommunityClick={(e) => { onCommunityClick={(e) => {
setActiveChannel(e); setActiveChannel(e);
setShowChannels(false); setShowChannels(false);

View File

@ -3,7 +3,6 @@ import { Identity } from "status-communities/dist/cjs";
import styled from "styled-components"; import styled from "styled-components";
import { CommunityData } from "../../models/CommunityData"; import { CommunityData } from "../../models/CommunityData";
import { Contact } from "../../models/Contact";
import { MembersList } from "../Members/MembersList"; import { MembersList } from "../Members/MembersList";
import { NarrowTopbar } from "./NarrowTopbar"; import { NarrowTopbar } from "./NarrowTopbar";
@ -11,7 +10,6 @@ import { NarrowTopbar } from "./NarrowTopbar";
interface NarrowMembersProps { interface NarrowMembersProps {
identity: Identity; identity: Identity;
community: CommunityData; community: CommunityData;
contacts: Contact[];
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
setShowMembersList: (val: boolean) => void; setShowMembersList: (val: boolean) => void;
setMembersList: any; setMembersList: any;
@ -20,7 +18,6 @@ interface NarrowMembersProps {
export function NarrowMembers({ export function NarrowMembers({
identity, identity,
community, community,
contacts,
setShowChannels, setShowChannels,
setShowMembersList, setShowMembersList,
setMembersList, setMembersList,
@ -30,7 +27,6 @@ export function NarrowMembers({
<NarrowTopbar list="Community members" community={community.name} /> <NarrowTopbar list="Community members" community={community.name} />
<MembersList <MembersList
identity={identity} identity={identity}
contacts={contacts}
setShowChannels={setShowChannels} setShowChannels={setShowChannels}
setShowMembers={setShowMembersList} setShowMembers={setShowMembersList}
setMembersList={setMembersList} setMembersList={setMembersList}

View File

@ -29,7 +29,6 @@ export function ReactChat({
<ChatLoader <ChatLoader
communityKey={communityKey} communityKey={communityKey}
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
theme={theme}
/> />
</Wrapper> </Wrapper>
</NarrowProvider> </NarrowProvider>

View File

@ -0,0 +1,37 @@
import React, { createContext, useContext } from "react";
import { Identity } from "status-communities/dist/cjs";
import { MessengerType, useMessenger } from "../hooks/messenger/useMessenger";
const MessengerContext = createContext<MessengerType>({
messenger: undefined,
messages: [],
sendMessage: async () => undefined,
notifications: {},
clearNotifications: () => undefined,
loadPrevDay: async () => undefined,
loadingMessages: false,
community: undefined,
contacts: [],
});
export function useMessengerContext() {
return useContext(MessengerContext);
}
interface NarrowProviderProps {
children: React.ReactNode;
activeChannel: string;
communityKey?: string;
identity?: Identity;
}
export function MessengerProvider({
children,
activeChannel,
communityKey,
identity,
}: NarrowProviderProps) {
const messenger = useMessenger(activeChannel, communityKey, identity);
return <MessengerContext.Provider value={messenger} children={children} />;
}

View File

@ -7,6 +7,7 @@ import {
Messenger, Messenger,
} from "status-communities/dist/cjs"; } from "status-communities/dist/cjs";
import { ChatMessage } from "../../models/ChatMessage";
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";
@ -14,10 +15,25 @@ import { createMessenger } from "../../utils/createMessenger";
import { useLoadPrevDay } from "./useLoadPrevDay"; import { useLoadPrevDay } from "./useLoadPrevDay";
import { useMessages } from "./useMessages"; import { useMessages } from "./useMessages";
export type MessengerType = {
messenger: Messenger | undefined;
messages: ChatMessage[];
sendMessage: (
messageText?: string | undefined,
image?: Uint8Array | undefined
) => Promise<void>;
notifications: { [chatId: string]: number };
clearNotifications: (id: string) => void;
loadPrevDay: (id: string) => Promise<void>;
loadingMessages: boolean;
community: Community | undefined;
contacts: Contact[];
};
export function useMessenger( export function useMessenger(
chatId: string, chatId: string,
communityKey: string, communityKey: string | undefined,
identity: Identity identity: Identity | undefined
) { ) {
const [messenger, setMessenger] = useState<Messenger | undefined>(undefined); const [messenger, setMessenger] = useState<Messenger | undefined>(undefined);
@ -26,7 +42,7 @@ export function useMessenger(
}>({}); }>({});
const contactsClass = useMemo(() => { const contactsClass = useMemo(() => {
if (messenger) { if (messenger && identity) {
const newContacts = new Contacts( const newContacts = new Contacts(
identity, identity,
messenger.waku, messenger.waku,
@ -38,7 +54,7 @@ export function useMessenger(
); );
return newContacts; return newContacts;
} }
}, [messenger]); }, [messenger, identity]);
const contacts = useMemo<Contact[]>(() => { const contacts = useMemo<Contact[]>(() => {
const now = Date.now(); const now = Date.now();
@ -58,13 +74,15 @@ export function useMessenger(
const { loadPrevDay, loadingMessages } = useLoadPrevDay(chatId, messenger); const { loadPrevDay, loadingMessages } = useLoadPrevDay(chatId, messenger);
useEffect(() => { useEffect(() => {
if (identity) {
createMessenger(identity).then((e) => { createMessenger(identity).then((e) => {
setMessenger(e); setMessenger(e);
}); });
}, []); }
}, [identity]);
useEffect(() => { useEffect(() => {
if (messenger && contactsClass) { if (messenger && contactsClass && communityKey) {
createCommunity(communityKey, addMessage, messenger).then((e) => { createCommunity(communityKey, addMessage, messenger).then((e) => {
setCommunity(e); setCommunity(e);
}); });