All files / Rindu/components/CardContent CardContent.tsx

100% Statements 26/26
100% Branches 18/18
100% Functions 4/4
100% Lines 24/24

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 172 173 174 175 176 177 178 179 180 181 182 183 18419x   19x 19x   19x   19x   19x                                               28x                   30x 30x 30x 30x 30x   30x 11x 8x 1x 1x   7x     30x   3x 2x 2x 2x 2x                                                                                                                                                                                                                                              
import { ReactElement, useCallback, useId, useRef } from "react";
 
import { decode } from "html-entities";
import { useRouter } from "next/router";
 
import { useContextMenu } from "hooks";
import { Booleanish } from "types/customTypes";
import { chooseImage, handleAsyncError } from "utils";
 
export enum CardType {
  SIMPLE = "simple",
  PLAYLIST = "playlist",
  ALBUM = "album",
  ARTIST = "artist",
  USER = "user",
  SHOW = "show",
  GENRE = "genre",
  TRACK = "track",
  EPISODE = "episode",
  AD = "ad",
}
 
export interface ICardContent {
  id: string;
  images?: SpotifyApi.ImageObject[];
  title: string;
  subTitle: string | ReactElement;
  type: CardType;
  url?: string;
  tabIndex?: number;
  "aria-hidden"?: Booleanish;
}
 
export default function CardContent({
  id,
  type,
  images,
  title,
  subTitle,
  url,
  tabIndex,
  "aria-hidden": ariaHidden,
}: Readonly<ICardContent>): ReactElement {
  const router = useRouter();
  const handlerRef = useRef<HTMLButtonElement>(null);
  const { addContextMenu } = useContextMenu();
  const cardContentId = useId();
  const uri = `spotify:${type}:${id}`;
 
  const handleClick = useCallback(async () => {
    if (!type) return;
    if (type === CardType.SIMPLE && url) {
      await router.push(url);
      return;
    }
    await router.push(`/${type}/${encodeURIComponent(id)}`);
  }, [id, router, type, url]);
 
  const handleContextMenu = useCallback(
    (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
      if (type === CardType.SIMPLE && url) return;
      e.preventDefault();
      const x = e.pageX;
      const y = e.pageY;
      addContextMenu({
        type: type === CardType.TRACK ? "cardTrack" : "cardContent",
        data: {
          id,
          type,
          uri,
        },
        position: { x, y },
      });
    },
    [addContextMenu, id, type, uri, url]
  );
 
  return (
    <article>
      <button
        ref={handlerRef}
        data-testid="cardContent-button"
        className="handler"
        onClick={handleAsyncError(handleClick)}
        onContextMenu={handleContextMenu}
        aria-labelledby={cardContentId}
        tabIndex={tabIndex}
        aria-hidden={ariaHidden}
      ></button>
      {images && (
        // eslint-disable-next-line @next/next/no-img-element
        <img loading="lazy" src={chooseImage(images, 300).url} alt={title} />
      )}
      <div>
        <strong id={cardContentId}>{title}</strong>
        <p>
          {typeof subTitle === "string"
            ? decode(subTitle).slice(0, 200)
            : subTitle}
        </p>
      </div>
      <style jsx>{`
        div {
          min-height: 62px;
        }
        .handler {
          position: absolute;
          width: 100%;
          height: 100%;
          border-radius: 4px;
          z-index: 1;
          background-color: transparent;
          top: 0;
          left: 0;
          border: none;
          cursor: pointer;
        }
        strong {
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
          display: -webkit-box;
          overflow: hidden;
          text-align: left;
          text-overflow: ellipsis;
          white-space: unset;
          word-break: break-all;
          margin: 4px 0;
        }
        p {
          font-weight: 300;
          margin: 0em 0;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          display: -webkit-box;
          overflow: hidden;
          text-align: left;
          text-overflow: ellipsis;
          white-space: unset;
          word-break: break-all;
          font-size: 15px;
          color: #ffffffb3;
        }
        article {
          width: 100%;
          min-width: 190px;
          height: 100%;
          text-align: left;
          border-radius: 4px;
          flex: 1;
          isolation: isolate;
          padding: 16px;
          position: relative;
        }
        img {
          width: 100%;
          min-width: 160px;
          margin-bottom: 16px;
          --card-image-border-radius: clamp(
            4px,
            (var(--left-panel-width, 0px) - 32px) * 0.025,
            8px
          );
          border-radius: ${type === CardType.ARTIST || type === CardType.USER
            ? "50%"
            : "var(--card-image-border-radius)"};
          box-shadow: 0 8px 24px rgb(0 0 0 / 50%);
          object-fit: cover;
          object-position: center center;
          aspect-ratio: 1;
        }
        @media screen and (max-width: 768px) {
          img {
            min-width: 140px;
          }
          article {
            min-width: 160px;
            padding: 0 8px;
          }
        }
      `}</style>
    </article>
  );
}