All files / components/ChatList ChatList.js

47.05% Statements 8/17
25% Branches 1/4
28.57% Functions 2/7
50% Lines 8/16

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          3x 4x   4x 4x   4x 4x         4x           4x                                                      
import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import ChatItem from "../ChatItem/ChatItem";
import styles from "./ChatList.module.scss";
 
const ChatList = ({ data }) => {
  const navigate = useNavigate();
 
  const [users, setUsers] = useState([]);
  const [chosen, setChosen] = useState(null);
 
  useEffect(() => {
    Iif (data) {
      setUsers(data.queryHC_Lead);
    }
  }, [data]);
 
  const handleSetChosen = (value) => {
    if (value === chosen) return;
    setChosen(value);
    navigate(`/chat/${value}`);
  };
 
  return (
    <div className={styles.ChatList}>
      {users
        .filter((e) => e?.threads[0]?.messages?.length > 0)
        ?.sort(
          (a, b) =>
            b.threads[0]?.messages[0]?.timestamp -
            a.threads[0]?.messages[0]?.timestamp
        )
        ?.map((user) => {
          return (
            <div key={user.id}>
              <ChatItem
                data-testid="ChatItem"
                key={user.id}
                user={user}
                active={user.id === chosen}
                onClick={() => handleSetChosen(user.id)}
              />
            </div>
          );
        })}
    </div>
  );
};
 
export default ChatList;