All files / Rindu/components/CardTrack TrackDetails.tsx

100% Statements 9/9
42.3% Branches 11/26
100% Functions 1/1
100% Lines 9/9

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 16619x   19x     19x 19x   19x             12x         12x 12x 12x                                                                                                                                                                                                                                                                                              
import { ReactElement, useRef } from "react";
 
import Link from "next/link";
 
import { CardType } from "./CardTrack";
import { ArtistList, ExplicitSign } from "components";
import { useOnScreen, useTranslations } from "hooks";
import { ITrack } from "types/spotify";
import { chooseImage, getTrackAddedDate } from "utils";
 
interface ITrackDetails {
  track: ITrack;
  type: CardType;
  isSmallScreen: boolean;
}
export function TrackDetails({
  track,
  type,
  isSmallScreen,
}: Readonly<ITrackDetails>): ReactElement {
  const { locale } = useTranslations();
  const trackRef = useRef<HTMLDivElement>(null);
  const isVisible = useOnScreen(trackRef);
 
  return (
    <>
      <section>
        {type !== "presentation" &&
          (track.album?.images?.length ? (
            //  eslint-disable-next-line @next/next/no-img-element
            <img
              loading="lazy"
              src={chooseImage(track.album?.images, 48).url}
              alt=""
              className="img"
              width="48"
              height="48"
            />
          ) : (
            <div className="img"></div>
          ))}
        <div className="trackArtistsContainer">
          {track.id && track.name ? (
            <Link
              href={`/${track.type ?? "track"}/${track.id}`}
              className="trackName"
              tabIndex={isVisible ? 0 : -1}
            >
              {track.name}
            </Link>
          ) : null}
          <span className="trackArtists">
            {track.explicit && <ExplicitSign />}
            <ArtistList artists={track.artists} />
          </span>
        </div>
      </section>
      {type === "playlist" && track.album?.id ? (
        <>
          <section>
            <p className="trackArtists">
              <Link
                href={`/${track.album.type ?? "album"}/${track.album.id}`} // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
                tabIndex={isVisible ? 0 : -1}
                aria-hidden={!isVisible}
              >
                {track.album.name}
              </Link>
            </p>
          </section>
          <section>
            <p className="trackArtists">
              {getTrackAddedDate(track.added_at, locale)}
            </p>
          </section>
        </>
      ) : null}
      <section>
        {track.popularity && !isSmallScreen ? (
          <div className="pop-meter">
            <div className="pop-meter-bar"></div>
            <div
              className="pop-meter-overlay"
              style={{
                width: `${track.popularity}%`,
              }}
            ></div>
          </div>
        ) : null}
      </section>
      <style jsx>{`
        .img {
          margin: 0;
          padding: 0;
          margin-right: 23px;
          width: 48px;
          height: 48px;
        }
        :global(.trackName),
        .trackArtists :global(a) {
          text-decoration: none;
        }
        :global(.trackName) {
          margin: 0;
          padding: 0;
        }
        .trackArtistsContainer {
          display: block;
          align-items: center;
        }
        :global(.trackName:hover),
        .trackArtists :global(a:hover) {
          text-decoration: underline;
        }
        .pop-meter {
          width: 30px;
          height: 9px;
          position: relative;
          margin: 0 auto;
        }
        .pop-meter-bar,
        .pop-meter-overlay {
          width: 100%;
          position: absolute;
          top: -1px;
          height: 9px;
          overflow-x: hidden;
        }
        .pop-meter-bar:after,
        .pop-meter-overlay:after {
          content: " ";
          display: block;
          transform: translate(0, 0.5px);
          position: absolute;
          left: -4px;
          width: 2px;
          height: 8px;
          top: 0;
        }
        .pop-meter-bar:after {
          box-shadow:
            4px 0 0 0 #3e3e40,
            8px 0 0 0 #3e3e40,
            12px 0 0 0 #3e3e40,
            16px 0 0 0 #3e3e40,
            20px 0 0 0 #3e3e40,
            24px 0 0 0 #3e3e40,
            28px 0 0 0 #3e3e40,
            32px 0 0 0 #3e3e40;
        }
        .pop-meter-overlay:after {
          box-shadow:
            4px 0 0 0 #88898c,
            8px 0 0 0 #88898c,
            12px 0 0 0 #88898c,
            16px 0 0 0 #88898c,
            20px 0 0 0 #88898c,
            24px 0 0 0 #88898c,
            28px 0 0 0 #88898c,
            32px 0 0 0 #88898c;
        }
      `}</style>
    </>
  );
}