All files / src/features/Minibuilders/Conversations/Modal/Sections ConversationsGeneralSearchInput.tsx

87.5% Statements 14/16
66.66% Branches 2/3
83.33% Functions 5/6
87.5% Lines 14/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 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                    21x             3x 3x   3x   3x 1x     3x         3x   1x                   3x 1x 1x     3x             1x                                                                                    
import { Input } from "@common/Input";
import { Text } from "@common/Text";
import { Box } from "@mui/material";
import { useAppDispatch } from "@redux/hooks";
import { setConversationsGeneralSearchQueryString } from "@redux/slices/conversations";
import { debounce } from "lodash";
import { useCallback, useEffect, useRef, useState } from "react";
 
type TScreen = "Topics" | "Threads" | "Replies";
 
const ConversationsGeneralSearchInput = ({
  defaultValue = "",
  screen,
}: {
  defaultValue?: string;
  screen: TScreen;
}) => {
  const ref = useRef<HTMLInputElement>(null);
  const [value, setValue] = useState<string>(defaultValue);
 
  const dispatch = useAppDispatch();
 
  useEffect(() => {
    ref.current?.focus();
  }, []);
 
  const handleClick = () => {
    setValue("");
    debouncedSearch("");
  };
 
  const debouncedSearch = useCallback(
    debounce((text) => {
      dispatch(
        setConversationsGeneralSearchQueryString({
          queryKey: screen,
          queryString: text,
        }),
      );
    }, 300),
    [screen],
  );
 
  const handleSearch = (text: string) => {
    debouncedSearch(text);
    setValue(text);
  };
 
  return (
    <>
      <Box>
        <Input
          inputRef={ref}
          fullWidth
          value={value}
          onChange={(v) => handleSearch(v.target.value)}
          placeholder="Search"
          InputLabelProps={{
            style: {
              color: "#575353 !important",
            },
          }}
          endIcon={
            <Box
              onClick={handleClick}
              display={value.length > 0 ? "initial" : "none"}
              sx={{ cursor: "pointer" }}
            >
              <Text color="#6D9CF8">Clear</Text>
            </Box>
          }
          sx={{
            "& .MuiInputBase-root": {
              border: "none",
              borderTop: "1px solid #ECECE9",
              borderBottom: "1px solid #ECECE9",
              borderRadius: 0,
              backgroundColor: "transparent",
              ":hover": {
                borderTop: "1px solid #ECECE9",
                borderBottom: "1px solid #ECECE9",
              },
              padding: "14px 12px",
              "&.Mui-focused": {
                borderTop: "1px solid #ECECE9",
                borderBottom: "1px solid #ECECE9",
                backgroundColor: "transparent",
              },
            },
          }}
        />
      </Box>
    </>
  );
};
 
export default ConversationsGeneralSearchInput;