All files / src/hooks useNavbar.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 2/2
100% Lines 14/14

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              5x 13x 12x 12x   12x 12x 12x   12x   12x 2x 1x 1x 1x       12x                                      
import React from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
 
import useUserStore from "@/store/useUserStore";
import useDeleteSessionMutation from "@/mutations/useDeleteSessionMutation";
 
const useNavbar = () => {
  const navigate = useNavigate();
  const { t, i18n } = useTranslation();
  const { isAuthenticated, sessionId, username, accentColor } = useUserStore();
 
  const [searchValue, setSearchValue] = React.useState<string>("");
  const [isDrawerOpen, setIsDrawerOpen] = React.useState<boolean>(false);
  const [toggleSearchBar, setToggleSearchBar] = React.useState<boolean>(false);
 
  const { deleteSessionMutation } = useDeleteSessionMutation();
 
  const handleEnterKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
    if (e.key === "Enter" && /[a-zA-Z0-9]/.test(searchValue)) {
      setSearchValue("");
      setToggleSearchBar(!toggleSearchBar);
      navigate(`/search-results?query=${searchValue}`);
    }
  };
 
  return {
    t,
    i18n,
    deleteSessionMutation,
    isAuthenticated,
    sessionId,
    username,
    accentColor,
    searchValue,
    setSearchValue,
    isDrawerOpen,
    setIsDrawerOpen,
    toggleSearchBar,
    setToggleSearchBar,
    handleEnterKeyPress,
  };
};
 
export default useNavbar;