Team Chat Powered by PubNub

Team Chat Powered by PubNub

    ›Tutorial

    Quickstart

    • Introduction
    • Run Locally
    • Advanced Features

    Tutorial

    • Overview
    • Setup Theming
    • Add PubNub Keys
    • Populate Data
    • Login
    • Show User Details
    • Show Conversations
    • Send Messages
    • Join Conversation
    • Leave Conversation
    • Show Members
    • Send Typing Indicators
    • Profanity Filter

    Show My Conversations

    Below your user info, you’ll find your list of conversations. The Introductions conversation is automatically selected when the app launches.

    You can join other conversations with the + button, and leave existing conversations with the Exit button. These features are covered in the join conversation and leave conversation sections of the tutorial.

    conversations list

    The joinedConversations/MyConversations/MyConversations.tsx component gets the user’s list of conversations and allows the user to select a conversation. When the app loads, it selects the Introduction conversation by default.

    export interface ConversationFragment {
      id: string;
      name: string;
    }
    const MyConversations = () => {
      const currentUserId = useSelector(getLoggedInUserId);
      const conversationsById = useSelector(getConversationsById);
      const conversations: ConversationFragment[] = useSelector(
        getJoinedConversations
      );
      const currentConversationId: string = useSelector(getCurrentConversationId);
      if (conversationsById === undefined) {
        return <div>Loading...</div>;
      }
      return (
        <Wrapper>
          <Title>
            Conversations
          </Title>
          <ConversationList>
            {conversations.map(conversation => (
              <ConversationItem
                id={conversation.id}
                name={conversation.name}
                selected={conversation.id === currentConversationId}
                key={conversation.id}
                onClick={() => {
                  dispatch(focusOnConversation(conversation.id));
                  dispatch(setLayoutDefault());
                }}
              ></ConversationItem>
            ))}
          </ConversationList>
        </Wrapper>
      );
    };
    

    Get Conversations for User

    The MyConversations component calls the getConversationsByUserId() selector to get the user’s conversations from the local store. This selector returns the list of conversations, along with properties like id and name, to be displayed in the UI.

    export const getJoinedConversations = createSelector(
      [getConversationsById, getLoggedInUserId, getConversationsByUserId],
      (
        conversations: ConversationsIndexedById,
        userId: string,
        userConversations: MembershipHash
      ): ConversationFragment[] => {
        return userConversations[userId]
          ? userConversations[userId].map(conversation => {
              return {
                id: conversation.id,
                name: conversations[conversation.id].name
              };
            })
          : [];
      }
    );
    
    ← Show User DetailsSend Messages →
    • Get Conversations for User
    Docs
    QuickstartTutorialThemeingPubNub Chat
    Source
    GitHubStar
    PubNub
    Copyright © 2020 PubNub