Add loading state and rename chat component (#205)

This commit is contained in:
Szymon Szlachtowicz 2022-01-27 12:13:12 +01:00 committed by GitHub
parent 2519181953
commit bcb41c6110
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 21 deletions

View File

@ -1,4 +1,8 @@
import { darkTheme, lightTheme, ReactChat } from "@waku/react-chat-sdk"; import {
DappConnectCommunityChat,
darkTheme,
lightTheme,
} from "@waku/react-chat-sdk";
import React, { useRef, useState } from "react"; import React, { useRef, useState } from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import styled from "styled-components"; import styled from "styled-components";
@ -73,10 +77,10 @@ function DragDiv() {
}} }}
/> />
<FloatingDiv className={showChat ? "" : "hide"}> <FloatingDiv className={showChat ? "" : "hide"}>
<ReactChat <DappConnectCommunityChat
theme={theme ? lightTheme : darkTheme} theme={theme ? lightTheme : darkTheme}
communityKey={process.env.COMMUNITY_KEY ?? ""}
config={{ config={{
communityKey: process.env.COMMUNITY_KEY ?? "",
environment: process.env.ENV ?? "", environment: process.env.ENV ?? "",
dappUrl: "https://0.0.0.0:8080", dappUrl: "https://0.0.0.0:8080",
}} }}

View File

@ -85,7 +85,7 @@ export function ChatBody({
editGroup, editGroup,
setEditGroup, setEditGroup,
}: ChatBodyProps) { }: ChatBodyProps) {
const { messenger, activeChannel, communityData } = useMessengerContext(); const { activeChannel, loadingMessenger } = useMessengerContext();
const narrow = useNarrow(); const narrow = useNarrow();
const className = useMemo(() => (narrow ? "narrow" : ""), [narrow]); const className = useMemo(() => (narrow ? "narrow" : ""), [narrow]);
@ -106,11 +106,11 @@ export function ChatBody({
} }
}, [narrow]); }, [narrow]);
if (messenger && communityData && activeChannel) { if (!loadingMessenger && activeChannel) {
return ( return (
<Wrapper> <Wrapper>
<ChatBodyWrapper className={className}> <ChatBodyWrapper className={className}>
{editGroup && communityData ? ( {editGroup ? (
<ChatCreation <ChatCreation
setEditGroup={setEditGroup} setEditGroup={setEditGroup}
activeChannel={activeChannel} activeChannel={activeChannel}

View File

@ -63,7 +63,7 @@ export function ChatTopbar({
showMembers, showMembers,
setEditGroup, setEditGroup,
}: ChatTopbarProps) { }: ChatTopbarProps) {
const { messenger, activeChannel, communityData } = useMessengerContext(); const { activeChannel, loadingMessenger } = useMessengerContext();
const narrow = useNarrow(); const narrow = useNarrow();
const [showChannelMenu, setShowChannelMenu] = useState(false); const [showChannelMenu, setShowChannelMenu] = useState(false);
@ -77,7 +77,7 @@ export function ChatTopbar({
className={narrow && showState !== ChatBodyState.Chat ? "narrow" : ""} className={narrow && showState !== ChatBodyState.Chat ? "narrow" : ""}
> >
<ChannelWrapper className={narrow ? "narrow" : ""}> <ChannelWrapper className={narrow ? "narrow" : ""}>
{messenger && communityData ? ( {!loadingMessenger ? (
<> <>
{narrow && ( {narrow && (
<CommunityWrap className={narrow ? "narrow" : ""}> <CommunityWrap className={narrow ? "narrow" : ""}>
@ -110,7 +110,7 @@ export function ChatTopbar({
</TopBtn> </TopBtn>
{!narrow && <ActivityButton />} {!narrow && <ActivityButton />}
</MenuWrapper> </MenuWrapper>
{!messenger && !communityData && <Loading />} {loadingMessenger && <Loading />}
{showChannelMenu && ( {showChannelMenu && (
<ChannelMenu <ChannelMenu
channel={activeChannel} channel={activeChannel}

View File

@ -11,13 +11,17 @@ import {
import { Chat } from "./Chat"; import { Chat } from "./Chat";
import { IdentityLoader } from "./Form/IdentityLoader"; import { IdentityLoader } from "./Form/IdentityLoader";
export function ChatLoader() { type ChatLoaderProps = {
communityKey: string;
};
export function ChatLoader({ communityKey }: ChatLoaderProps) {
const [userCreationState] = useUserCreationState(); const [userCreationState] = useUserCreationState();
if (userCreationState === UserCreationState.NotCreating) if (userCreationState === UserCreationState.NotCreating)
return ( return (
<IdentityProvider> <IdentityProvider>
<MessengerProvider> <MessengerProvider communityKey={communityKey}>
<ChatStateProvider> <ChatStateProvider>
<Chat /> <Chat />
</ChatStateProvider> </ChatStateProvider>

View File

@ -16,13 +16,19 @@ import { Theme } from "../styles/themes";
import { ChatLoader } from "./ChatLoader"; import { ChatLoader } from "./ChatLoader";
interface ReactChatProps { interface DappConnectCommunityChatProps {
theme: Theme; theme: Theme;
communityKey: string;
config: ConfigType; config: ConfigType;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
} }
export function ReactChat({ theme, config, fetchMetadata }: ReactChatProps) { export function DappConnectCommunityChat({
theme,
config,
fetchMetadata,
communityKey,
}: DappConnectCommunityChatProps) {
const ref = useRef<HTMLHeadingElement>(null); const ref = useRef<HTMLHeadingElement>(null);
return ( return (
<ConfigProvider config={config}> <ConfigProvider config={config}>
@ -35,7 +41,7 @@ export function ReactChat({ theme, config, fetchMetadata }: ReactChatProps) {
<ToastProvider> <ToastProvider>
<Wrapper ref={ref}> <Wrapper ref={ref}>
<GlobalStyle /> <GlobalStyle />
<ChatLoader /> <ChatLoader communityKey={communityKey} />
<div id="modal-root" /> <div id="modal-root" />
</Wrapper> </Wrapper>
</ToastProvider> </ToastProvider>

View File

@ -1,13 +1,11 @@
import React, { createContext, useContext } from "react"; import React, { createContext, useContext } from "react";
export type ConfigType = { export type ConfigType = {
communityKey: string;
environment: string; environment: string;
dappUrl: string; dappUrl: string;
}; };
const ConfigContext = createContext<ConfigType>({ const ConfigContext = createContext<ConfigType>({
communityKey: "",
environment: "production", environment: "production",
dappUrl: "", dappUrl: "",
}); });

View File

@ -14,6 +14,7 @@ const MessengerContext = createContext<MessengerType>({
clearMentions: () => undefined, clearMentions: () => undefined,
loadPrevDay: async () => undefined, loadPrevDay: async () => undefined,
loadingMessages: false, loadingMessages: false,
loadingMessenger: true,
communityData: undefined, communityData: undefined,
contacts: {}, contacts: {},
setContacts: () => undefined, setContacts: () => undefined,
@ -33,12 +34,16 @@ export function useMessengerContext() {
} }
interface MessengerProviderProps { interface MessengerProviderProps {
communityKey: string;
children: React.ReactNode; children: React.ReactNode;
} }
export function MessengerProvider({ children }: MessengerProviderProps) { export function MessengerProvider({
communityKey,
children,
}: MessengerProviderProps) {
const identity = useIdentity(); const identity = useIdentity();
const nickname = useNickname(); const nickname = useNickname();
const messenger = useMessenger(identity, nickname); const messenger = useMessenger(communityKey, identity, nickname);
return <MessengerContext.Provider value={messenger} children={children} />; return <MessengerContext.Provider value={messenger} children={children} />;
} }

View File

@ -36,6 +36,7 @@ export type MessengerType = {
clearMentions: (id: string) => void; clearMentions: (id: string) => void;
loadPrevDay: (id: string, groupChat?: boolean) => Promise<void>; loadPrevDay: (id: string, groupChat?: boolean) => Promise<void>;
loadingMessages: boolean; loadingMessages: boolean;
loadingMessenger: boolean;
communityData: CommunityData | undefined; communityData: CommunityData | undefined;
contacts: Contacts; contacts: Contacts;
setContacts: React.Dispatch<React.SetStateAction<Contacts>>; setContacts: React.Dispatch<React.SetStateAction<Contacts>>;
@ -101,10 +102,10 @@ function useCreateCommunity(
} }
export function useMessenger( export function useMessenger(
communityKey: string,
identity: Identity | undefined, identity: Identity | undefined,
newNickname: string | undefined newNickname: string | undefined
) { ) {
const { communityKey } = useConfig();
const [activeChannel, setActiveChannel] = useState<ChannelData>({ const [activeChannel, setActiveChannel] = useState<ChannelData>({
id: "", id: "",
name: "", name: "",
@ -238,6 +239,10 @@ export function useMessenger(
} }
}, [notifications, activeChannel]); }, [notifications, activeChannel]);
const loadingMessenger = useMemo(() => {
return !communityData || !messenger || !activeChannel;
}, [communityData, messenger, activeChannel]);
return { return {
messenger, messenger,
messages, messages,
@ -246,6 +251,7 @@ export function useMessenger(
clearNotifications, clearNotifications,
loadPrevDay, loadPrevDay,
loadingMessages, loadingMessages,
loadingMessenger,
communityData, communityData,
contacts, contacts,
setContacts, setContacts,

View File

@ -1,4 +1,4 @@
import { ReactChat } from "./components/ReactChat"; import { DappConnectCommunityChat } from "./components/DappConnectCommunityChat";
import { ConfigType } from "./contexts/configProvider"; import { ConfigType } from "./contexts/configProvider";
import { darkTheme, lightTheme } from "./styles/themes"; import { darkTheme, lightTheme } from "./styles/themes";
export { ReactChat, lightTheme, darkTheme, ConfigType }; export { DappConnectCommunityChat, lightTheme, darkTheme, ConfigType };