All files / Rindu/components/CardTrack TrackActions.tsx

36.66% Statements 11/30
36.11% Branches 13/36
25% Functions 1/4
36.66% Lines 11/30

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 20019x   19x 19x   19x           19x                   12x               12x 12x 12x 12x 12x                                                                                                                                                                                                                                                                                                                                            
import { Dispatch, ReactElement, SetStateAction, useRef } from "react";
 
import { Heart, ThreeDots } from "components/icons";
import { useContextMenu, useOnScreen, useToast, useTranslations } from "hooks";
import { ITrack } from "types/spotify";
import { formatTime, templateReplace } from "utils";
import {
  removeEpisodesFromLibrary,
  removeTracksFromLibrary,
  saveEpisodesToLibrary,
  saveTracksToLibrary,
} from "utils/spotifyCalls";
 
interface ITrackActions {
  mouseEnter: boolean;
  isFocusing: boolean;
  setIsLikedTrack: Dispatch<SetStateAction<boolean | undefined>>;
  isTrackInLibrary?: boolean;
  track: ITrack;
  onClickAdd?: () => void;
}
export function TrackActions({
  mouseEnter,
  isFocusing,
  setIsLikedTrack,
  isTrackInLibrary,
  track,
  onClickAdd,
}: Readonly<ITrackActions>): ReactElement {
  const trackRef = useRef<HTMLDivElement>(null);
  const { addContextMenu } = useContextMenu();
  const { addToast } = useToast();
  const { translations } = useTranslations();
  const isVisible = useOnScreen(trackRef);
 
  return (
    <section className="extras">
      <Heart
        className="trackHeart"
        active={!!isTrackInLibrary}
        tabIndex={isVisible ? 0 : -1}
        aria-hidden={isVisible ? "false" : "true"}
        handleLike={async () => {
          const saveToLibrary =
            track.type === "episode"
              ? saveEpisodesToLibrary
              : saveTracksToLibrary;
          const saveRes = await saveToLibrary([track.id ?? ""]);
          Iif (saveRes) {
            setIsLikedTrack(true);
            addToast({
              variant: "success",
              message: templateReplace(translations.toastMessages.typeAddedTo, [
                `${
                  track.type === "episode"
                    ? translations.contentType.episode
                    : translations.contentType.track
                }`,
                translations.contentType.library,
              ]),
            });
            return true;
          }
          return null;
        }}
        handleDislike={async () => {
          const removeFromLibrary =
            track.type === "episode"
              ? removeEpisodesFromLibrary
              : removeTracksFromLibrary;
          const removeRes = await removeFromLibrary([track.id ?? ""]);
          Iif (removeRes) {
            setIsLikedTrack(false);
            addToast({
              variant: "success",
              message: templateReplace(
                translations.toastMessages.typeRemovedFrom,
                [
                  `${
                    track.type === "episode"
                      ? translations.contentType.episode
                      : translations.contentType.track
                  }`,
                  translations.contentType.library,
                ]
              ),
            });
            return true;
          }
          return null;
        }}
      />
      <p className="trackArtists time">
        {track.duration_ms ? formatTime(track.duration_ms / 1000) : ""}
      </p>
      {onClickAdd && (
        <button
          type="button"
          className="add"
          onClick={onClickAdd}
          tabIndex={isVisible ? 0 : -1}
          aria-hidden={isVisible ? "false" : "true"}
        >
          Add
        </button>
      )}
      <button
        type="button"
        className="options"
        aria-label="Show more options"
        onClick={(e) => {
          e.stopPropagation();
          e.preventDefault();
          const x = e.pageX;
          const y = e.pageY;
          addContextMenu({
            type: "cardTrack",
            data: track,
            position: { x, y },
          });
        }}
        tabIndex={isVisible ? 0 : -1}
        aria-hidden={isVisible ? "false" : "true"}
      >
        {mouseEnter || isFocusing ? (
          <ThreeDots />
        ) : (
          <div className="threeDots"></div>
        )}
      </button>
      <style jsx>{`
        .options {
          width: ${onClickAdd ? "40px" : "32px"};
          height: ${onClickAdd ? "40px" : "32px"};
        }
      `}</style>
      <style jsx>{`
        :global(.trackItem) section.extras {
          justify-content: center;
        }
        .threeDots {
          width: 16px;
        }
        section.extras button.options {
          margin: 0;
          display: flex;
          align-items: center;
          background: transparent;
          border: none;
        }
        .options {
          color: #ffffffb3;
          justify-content: center;
        }
        .options:hover,
        .options:focus {
          color: #ffffff;
        }
        :global(.trackItem) .trackArtists.time {
          overflow: unset;
          min-width: 50px;
          text-align: center;
        }
        .add {
          align-items: center;
          background-color: transparent;
          border-radius: 500px;
          border: 1px solid #535353;
          color: #fff;
          cursor: auto;
          display: flex;
          font-size: 0.875rem;
          font-weight: 700;
          height: initial;
          justify-content: center;
          letter-spacing: 1.76px;
          line-height: 1rem;
          margin: 0;
          padding: 6px 28px;
          text-decoration: none;
          touch-action: manipulation;
          transition: all 33ms cubic-bezier(0.3, 0, 0, 1);
          user-select: none;
          white-space: nowrap;
          width: 40px;
          will-change: transform;
        }
        .add:hover,
        .add:focus {
          transform: scale(1.04);
          border-color: #fff;
        }
        .add:active {
          transform: scale(0.96);
        }
      `}</style>
    </section>
  );
}