All files / Rindu/components/SearchInputElement SearchInputElement.tsx

11.62% Statements 5/43
0% Branches 0/29
0% Functions 0/14
11.62% Lines 5/43

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                19x   19x 19x 19x 19x                                                                                                                                                                                                                                                                                                                          
import {
  Dispatch,
  MutableRefObject,
  ReactElement,
  SetStateAction,
  useEffect,
  useRef,
  useState,
} from "react";
 
import { Search } from "components/icons";
import { useSpotify, useTranslations } from "hooks";
import { isCorruptedTrack } from "utils";
import { search } from "utils/spotifyCalls";
 
interface InputElementProps {
  setData: Dispatch<SetStateAction<SpotifyApi.SearchResponse | null>>;
  source: "search" | "playlist";
}
 
export default function SearchInputElement({
  setData,
  source,
}: Readonly<InputElementProps>): ReactElement {
  const inputRef = useRef<HTMLInputElement>();
  const [isTyping, setIsTyping] = useState(false);
  const [query, setQuery] = useState("");
  const [shouldSearch, setShouldSearch] = useState(false);
  const { setAllTracks, setIgnoreShortcuts } = useSpotify();
  const isFromSearch = source === "search";
  const { translations } = useTranslations();
 
  useEffect(() => {
    inputRef.current?.focus();
  }, []);
 
  useEffect(() => {
    let timer: NodeJS.Timeout;
    Iif (!isTyping && query) {
      timer = setTimeout(() => {
        Iif (!isTyping && query) {
          setShouldSearch(true);
        }
      }, 1500);
    }
 
    Iif (!query) {
      setData(null);
    }
 
    return () => {
      Iif (timer) {
        clearTimeout(timer);
      }
    };
  }, [query, isTyping, setData]);
 
  useEffect(() => {
    async function searchQuery() {
      const searchData = await search(query);
      setData(searchData);
      Iif (searchData?.tracks?.items.length && source === "search") {
        const tracks = searchData.tracks?.items?.map((track) => {
          return {
            ...track,
            audio: track.preview_url,
            corruptedTrack: isCorruptedTrack(track),
          };
        });
        setAllTracks(tracks);
      }
      setShouldSearch(false);
    }
    Iif (shouldSearch && query) {
      searchQuery();
    }
  }, [query, shouldSearch, setData, setAllTracks, source]);
 
  return (
    <div>
      <form role="search" onSubmit={(e) => e.preventDefault()}>
        <input
          ref={inputRef as MutableRefObject<HTMLInputElement>}
          maxLength={80}
          autoCorrect="off"
          autoCapitalize="off"
          spellCheck="false"
          placeholder={translations.pages.playlist.searchPlaceholder}
          defaultValue=""
          onChange={(e) => {
            setQuery(e.target.value);
            setIsTyping(true);
          }}
          onFocus={() => {
            setIgnoreShortcuts.on();
          }}
          onBlur={() => {
            setIgnoreShortcuts.off();
          }}
          onKeyDown={(e) => {
            Iif (e.key === " ") {
              e.stopPropagation();
            }
          }}
          onKeyUp={() => {
            setIsTyping(false);
          }}
        />
      </form>
      <Search
        width={isFromSearch ? "24px" : "16px"}
        height={isFromSearch ? "24px" : "16px"}
        fill={isFromSearch ? "#121212" : "#ffffffb3"}
      />
      <style jsx>{`
        div {
          max-width: 364px;
          position: relative;
        }
        div :global(svg) {
          position: absolute;
          display: flex;
          align-items: center;
          left: 12px;
          pointer-events: none;
          height: 100%;
          right: 12px;
          top: 0;
          bottom: 0;
          border: 0;
          margin: 0;
          padding: 0;
          vertical-align: baseline;
        }
        form {
          border: 0;
          margin: 0;
          padding: 0;
          vertical-align: baseline;
        }
        input {
          border: 0;
          border-radius: ${isFromSearch ? "500px" : "4px"};
          background-color: ${isFromSearch ? "#fff" : "#ffffff1a"};
          color: ${isFromSearch ? "#000" : "#ffffffb3"};
          height: 40px;
          padding: ${isFromSearch ? "6px 48px" : "4px 36px"};
          text-overflow: ellipsis;
          width: 100%;
          font-size: 0.875rem;
          font-weight: 400;
          letter-spacing: normal;
          line-height: 16px;
          text-transform: none;
        }
        input:focus {
          outline: none;
        }
 
        @media (max-width: 768px) {
          div {
            max-width: 430px;
            width: 100%;
            margin: 0 8px;
          }
        }
      `}</style>
    </div>
  );
}