All files ConversationShell.js

82.75% Statements 24/29
90% Branches 9/10
71.42% Functions 5/7
82.75% Lines 24/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                10x 10x 10x         10x             17x 17x 17x   17x           17x   17x 17x   17x 5x 5x     17x 5x         17x 5x 5x 5x       4x               5x             17x   17x                                                                                                                                                                                                          
import {
  ArrowLeftOutlined,
  MoreOutlined,
  SearchOutlined,
} from "@ant-design/icons";
import { useQuery, useSubscription } from "@apollo/client";
import { Spin, Tabs } from "antd";
import React, { useContext, useEffect } from "react";
import { useParams } from "react-router-dom";
import ChatContext from "../../context/chat/ChatContext";
import { LOAD_MESSAGES, LOAD_NEWMESSAGES } from "../../GraphQL/Queries";
import ConversationContainer from "../ConversationContainer/ConversationContainer";
import Participants from "../Participants/Participants";
import { ProfilePicPlaceholder } from "hc-inbox-profile";
import { getQueryMessages } from "../../api/api";
import styles from "./ConversationShell.module.scss";
const { TabPane } = Tabs;
const tabStyles = { paddingTop: "20px" };
export const profilePicPlaceholderStyle = {
  height: "40px",
  width: "40px",
  fontSize: "1em",
};
const ConversationShell = ({
  chatApiUrl,
  backendApiUrl,
  query,
  queryMessagesBody,
  handleSetIsUsername,
}) => {
  const chatContext = useContext(ChatContext);
  const { storeNewMessages } = chatContext;
  const { username } = useParams();
 
  const { data, refetch, loading } = useQuery(LOAD_MESSAGES, {
    variables: {
      leadId: username,
    },
  });
 
  const { data: newMessagesData } = useSubscription(LOAD_NEWMESSAGES);
 
  const [queryMessages, setQueryMessage] = React.useState([]);
  const [centralMessage, setCentralMessage] = React.useState(undefined);
 
  useEffect(() => {
    handleSetIsUsername(username);
    refetch();
  }, [username]);
 
  useEffect(() => {
    Iif (newMessagesData) {
      storeNewMessages(newMessagesData?.queryMessage);
    }
  }, [newMessagesData]);
 
  useEffect(() => {
    setQueryMessage([]);
    const fetchQueryMessages = async () => {
      if (
        (queryMessagesBody !== null) &
        (queryMessagesBody?.threadId !== null)
      ) {
        const { data } = await getQueryMessages(
          backendApiUrl,
          queryMessagesBody
        );
        setQueryMessage(data.messages);
        setCentralMessage(data.centralMessage);
      }
    };
    fetchQueryMessages();
  }, [queryMessagesBody]);
 
  function callback(key) {
    console.log(key);
  }
 
  const name = data?.getHC_Lead?.username || "";
 
  return (
    <div className={styles.ConversationShell}>
      {!username ? (
        <div className={styles.ConversationShell__bar}>
          <span
            data-testid="selectcontact"
            className={styles.ConversationShell__bar__selectcontact}
          >
            Select a contact
          </span>
        </div>
      ) : (
        <>
          <div className={styles.ConversationShell__bar}>
            <div className={styles.ConversationShell__bar__imgcontainer}>
              <ProfilePicPlaceholder
                name={name}
                id={username}
                height={profilePicPlaceholderStyle.height}
                width={profilePicPlaceholderStyle.width}
                fontSize={profilePicPlaceholderStyle.fontSize}
              />
              <div
                className={
                  styles.ConversationShell__bar__imgcontainer__status_circle
                }
              ></div>
            </div>
            <h6 className={styles.ConversationShell__bar__name}>
              {data?.getHC_Lead?.username}
            </h6>
            <div className={styles.ConversationShell__bar__panels}>
              <Tabs
                defaultActiveKey="1"
                onChange={callback}
                tabBarStyle={tabStyles}
              >
                <TabPane tab="Chat" key="1"></TabPane>
                <TabPane tab="Files" key="2"></TabPane>
                <TabPane tab="Starred Messages" key="3"></TabPane>
              </Tabs>
            </div>
            <div className={styles.ConversationShell__bar__participants}>
              <Participants
                username={username}
                loggedUser={"Sahil Thakur"}
                data={data?.getHC_Lead?.threads[0]?.participants}
              />
            </div>
          </div>
          <div className={styles.ConversationShell__mobilebar}>
            <ArrowLeftOutlined
              className={styles.ConversationShell__mobilebar__icon}
              onClick={() => window.history.back()}
            />
            <div className={styles.ConversationShell__mobilebar__imgcontainer}>
              <ProfilePicPlaceholder
                name={name}
                id={username}
                height={profilePicPlaceholderStyle.height}
                width={profilePicPlaceholderStyle.width}
                fontSize={profilePicPlaceholderStyle.fontSize}
              />
              <div
                className={
                  styles.ConversationShell__mobilebar__imgcontainer__status_circle
                }
              ></div>
            </div>
            <h6 className={styles.ConversationShell__mobilebar__name}>
              {data?.getHC_Lead?.username}
            </h6>
            <SearchOutlined
              className={styles.ConversationShell__mobilebar__icon}
            />
            <MoreOutlined
              className={styles.ConversationShell__mobilebar__icon}
            />
          </div>
        </>
      )}
      {loading ? (
        <Spin size="large" className={styles.ConversationShell__loading} />
      ) : (
        <ConversationContainer
          newMessagesData={newMessagesData}
          user={data}
          selectedId={username}
          refetch={refetch}
          chatApiUrl={chatApiUrl}
          centralMessage={centralMessage}
          query={query}
          queryMessages={queryMessages}
          queryMessagesBody={queryMessagesBody}
        />
      )}
    </div>
  );
};
 
export default ConversationShell;