All files / src/features/Minibuilders/Conversations/hooks useConversationsHeader.tsx

93.93% Statements 31/33
61.11% Branches 11/18
100% Functions 7/7
93.93% Lines 31/33

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                        21x 1x             21x       1x 1x               21x             34x 34x   34x 1x   34x 1x     1x 1x 1x   1x 1x 1x         34x 2x               34x 1x 1x     1x 1x 1x 1x 1x                             1x 1x     34x    
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  resetConversationTopic,
  resetThreads,
  selectReplies,
  setConversationTopic,
  setConversationsGeneralSearchQueryString,
  setReplies,
} from "@redux/slices/conversations";
import { Dispatch, SetStateAction } from "react";
import { TQueryObject } from "../types";
 
const setConversationTopicAction = (id?: number, name = "", paths: [] = []) => {
  return setConversationTopic({
    isOpen: false,
    queryObject: { id, name, paths },
    isOpenedFromSidePanel: true,
  });
};
 
const resetState = (
  dispatch: Dispatch<any>,
  setShowSearchInput: Dispatch<SetStateAction<boolean>>,
) => {
  setShowSearchInput(false);
  dispatch(
    setConversationsGeneralSearchQueryString({
      queryKey: "",
      queryString: "",
    }),
  );
};
 
export const useConversationsHeader = ({
  queryObject,
  setShowSearchInput,
}: {
  queryObject?: TQueryObject;
  setShowSearchInput: Dispatch<SetStateAction<boolean>>;
}) => {
  const { isRepliesOpen } = useAppSelector(selectReplies);
  const dispatch = useAppDispatch();
 
  const toggleReplies = (isOpen: boolean) =>
    dispatch(setReplies({ isRepliesOpen: isOpen }));
 
  const handleClick = () => {
    Iif (isRepliesOpen) {
      toggleReplies(false);
    } else {
      dispatch(setConversationTopicAction());
      dispatch(resetThreads());
      dispatch(resetConversationTopic());
    }
    resetState(dispatch, setShowSearchInput);
    const activeBar = document.querySelector(".conversationsBarActive");
    Iif (activeBar) {
      activeBar.classList.remove("conversationsBarActive");
    }
  };
 
  const clearQuery = () => {
    dispatch(
      setConversationsGeneralSearchQueryString({
        queryKey: "",
        queryString: "",
      }),
    );
  };
 
  const handleBreadCrumbClick = () => {
    Eif (isRepliesOpen) {
      toggleReplies(false);
    }
 
    const pathsArray = queryObject?.paths || [];
    const pathsCopy = [...pathsArray];
    const lastPath = pathsCopy.pop();
    const isActivityTab = queryObject?.tabs === "Activity";
    dispatch(
      setConversationTopic({
        queryObject: {
          id: queryObject?.id,
          name: queryObject?.name || "",
          paths: lastPath && isActivityTab ? pathsCopy : [],
          defaultMessage: undefined,
          challengeId: undefined,
          challengeTypeName: undefined,
        },
        ...(isActivityTab && {
          isOpen: pathsCopy?.length > 0,
        }),
      }),
    );
    setShowSearchInput(false);
    clearQuery();
  };
 
  return { handleBreadCrumbClick, handleClick, isRepliesOpen, clearQuery };
};