All files / Rindu/components/FullScreenLyrics LyricLine.tsx

5.26% Statements 2/38
0% Branches 0/13
0% Functions 0/7
5.55% Lines 2/36

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 14419x   19x                                                                                                                                                                                                                                                                                          
import { ReactElement, useEffect, useRef, useState } from "react";
 
import { useAuth, useLyricsContext, useSpotify } from "hooks";
import { IFormatLyricsResponse, LineType } from "types/lyrics";
 
interface ILyricLineProps {
  line: IFormatLyricsResponse["lines"][0];
  type: LineType;
}
 
export function LyricLine({ line, type }: ILyricLineProps): ReactElement {
  const { player, updateLyricLine, setUpdateLyricLine } = useSpotify();
  const { isPremium } = useAuth();
  const lineRef = useRef<HTMLButtonElement>(null);
  const { lyricsProgressMs, lyricTextColor, lyricLineColor, lyrics } =
    useLyricsContext();
 
  const lineColors = {
    current: "#fff",
    previous: lyricLineColor + "80",
    next: lyricTextColor,
  };
 
  const [isManuallyScrolling, setIsManuallyScrolling] = useState(false);
 
  useEffect(() => {
    const line = lineRef.current;
    Iif (!line) return;
 
    const currentLine = line.classList.contains("current");
    Iif (!currentLine) return;
 
    const lineHeight = line.offsetHeight;
    let scrollTimeout: NodeJS.Timeout;
    const handleUserScroll = () => {
      setIsManuallyScrolling(true);
      clearTimeout(scrollTimeout);
 
      scrollTimeout = setTimeout(() => {
        setIsManuallyScrolling(false);
      }, 150);
    };
 
    window.addEventListener("wheel", handleUserScroll);
    window.addEventListener("touchmove", handleUserScroll);
 
    const observer = new IntersectionObserver(
      ([entry]) => {
        Iif ((entry.isIntersecting && !isManuallyScrolling) || updateLyricLine) {
          setUpdateLyricLine.off();
          line.scrollIntoView({
            behavior: "smooth",
            block: "center",
            inline: "nearest",
          });
        }
      },
      {
        root: null,
        rootMargin: `-${lineHeight}px 0px 0px 0px`,
        threshold: 1.0,
      }
    );
 
    observer.observe(line);
 
    return () => {
      observer.disconnect();
      setUpdateLyricLine.off();
      window.removeEventListener("wheel", handleUserScroll);
      window.removeEventListener("touchmove", handleUserScroll);
      clearTimeout(scrollTimeout);
    };
  }, [
    lyricsProgressMs,
    isManuallyScrolling,
    updateLyricLine,
    setUpdateLyricLine,
  ]);
 
  return (
    <button
      onClick={(e) => {
        e.stopPropagation();
        Iif (
          isPremium &&
          line?.startTimeMs &&
          player &&
          lyrics?.syncType === "LINE_SYNCED"
        ) {
          player.seek(Number(line.startTimeMs));
        }
      }}
      className={`line ${type}`}
      dir="auto"
      ref={lineRef}
    >
      {line.words}
      <style jsx>{`
        .line {
          display: block;
          color: ${lyricTextColor};
          background-color: transparent;
          border: none;
          width: 100%;
          text-align: left;
          padding-left: 144px;
          font-size: 32px;
          font-weight: 700;
          letter-spacing: -0.04em;
          line-height: 54px;
          cursor: pointer;
          transition: all 0.1s ease-out 0s;
        }
        .line.current {
          color: ${lineColors.current};
        }
        .line.previous {
          color: ${lineColors.previous};
        }
        .line.next {
          color: ${lineColors.next};
        }
        .line:hover {
          color: ${lyrics?.colors ? lyricLineColor : "#fff"};
          opacity: 1;
        }
        @media (max-width: 768px) {
          .line {
            padding-left: 0;
            font-size: 18px;
            line-height: 32px;
          }
        }
        @media (max-width: 658px) {
          .line {
            padding-left: 0;
          }
        }
      `}</style>
    </button>
  );
}