All files / Rindu/components/PlayButton PlayButton.tsx

52.17% Statements 12/23
27.77% Branches 5/18
50% Functions 1/2
52.17% Lines 12/23

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 14719x   19x             19x     19x                       18x                                         18x 18x 18x 18x 18x 18x   18x                                                                                                                                                                                            
import { HTMLAttributes, ReactElement, useRef } from "react";
 
import { Pause, Play } from "components/icons";
import {
  useAuth,
  useIsThisPlaybackPlaying,
  useOnScreen,
  useSpotify,
  useToast,
} from "hooks";
import type { AudioPlayer } from "hooks/useSpotifyPlayer";
import { ITrack } from "types/spotify";
import { handleNonPremiumPlay, handlePremiumPlay } from "utils";
 
interface PlayButtonProps {
  size: number;
  centerSize: number;
  track?: ITrack;
  isSingle?: boolean;
  uri?: string;
  position?: number;
  allTracks: ITrack[];
}
 
export default function PlayButton({
  size,
  centerSize,
  track,
  isSingle,
  uri,
  position,
  allTracks,
  ...props
}: PlayButtonProps & HTMLAttributes<HTMLButtonElement>): ReactElement | null {
  const {
    isPlaying,
    player,
    deviceId,
    pageDetails,
    playlistPlayingId,
    setPlaylistPlayingId,
    setCurrentlyPlaying,
    setIsPlaying,
    setPlayedSource,
    setReconnectionError,
  } = useSpotify();
  const { user, isPremium } = useAuth();
  const { addToast } = useToast();
  const uriId = uri?.split(":")?.[2];
  const buttonRef = useRef<HTMLButtonElement>(null);
  const isVisible = useOnScreen(buttonRef);
 
  const isThisPlaying = useIsThisPlaybackPlaying({
    track,
    trackId: track?.id,
    uri,
    isSingle,
    uriId,
    allTracksToPlay: allTracks,
  });
 
  return (
    <>
      <button
        type="button"
        aria-label="Play/Pause"
        className="play-Button"
        onClick={(e) => {
          e.preventDefault();
          e.stopPropagation();
          Iif ((!pageDetails && !track && !uri) || !user) {
            return;
          }
          Iif (isPremium && deviceId) {
            Iif (isThisPlaying) {
              player?.pause();
              return;
            }
            handlePremiumPlay(
              player as Spotify.Player,
              deviceId,
              addToast,
              setReconnectionError,
              setPlaylistPlayingId,
              setPlayedSource,
              track,
              isSingle,
              playlistPlayingId,
              uriId,
              pageDetails,
              uri,
              isPlaying,
              position,
              allTracks
            );
            return;
          }
          handleNonPremiumPlay(
            player as AudioPlayer,
            isThisPlaying,
            setIsPlaying,
            setCurrentlyPlaying,
            setPlaylistPlayingId,
            allTracks,
            pageDetails,
            track
          );
        }}
        ref={buttonRef}
        aria-hidden={isVisible ? "false" : "true"}
        tabIndex={isVisible ? 0 : -1}
        {...props}
      >
        {isThisPlaying ? (
          <Pause fill="#000" width={centerSize} height={centerSize} />
        ) : (
          <Play fill="#000" width={centerSize} height={centerSize} />
        )}
      </button>
      <style jsx>{`
        .play-Button {
          background-color: #1ed760;
          display: flex;
          justify-content: center;
          align-items: center;
          width: ${size}px;
          height: ${size}px;
          border: none;
          border-radius: 50%;
          min-width: ${size}px;
          z-index: 1;
          box-shadow: 0 8px 8px rgb(0 0 0 / 30%);
        }
        .play-Button:focus,
        .play-Button:hover {
          transform: scale(1.06);
          background-color: #1fdf64;
        }
        .play-Button:active {
          transform: scale(1);
          background-color: #169c46;
        }
      `}</style>
    </>
  );
}