All files / Rindu/hooks useIsThisPlaybackPlaying.ts

69.23% Statements 54/78
32.14% Branches 18/56
64.28% Functions 9/14
69.23% Lines 54/78

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 18219x   19x   19x                       18x               18x 18x 18x 18x 18x 18x 18x   18x 18x 18x 18x 18x 18x     18x   18x 10x 10x 10x 10x           10x 10x         10x 10x     10x 10x       10x         10x     10x 10x 10x 10x                                                   10x 10x 10x 10x                     10x   10x 10x     10x 10x 10x 10x                               10x 10x 10x 10x 10x 10x 10x                                     18x                          
import { useEffect, useState } from "react";
 
import { useRouter } from "next/router";
 
import { useSpotify } from "hooks";
import { ITrack } from "types/spotify";
 
interface IUseIsThisPlaybackPlaying {
  track?: ITrack;
  trackId?: string | null;
  uri?: string;
  isSingle?: boolean;
  uriId?: string;
  allTracksToPlay?: ITrack[];
}
 
export function useIsThisPlaybackPlaying({
  trackId,
  uri,
  isSingle,
  uriId,
  allTracksToPlay,
  track,
}: IUseIsThisPlaybackPlaying): boolean {
  const [isThisTrackPlaying, setIsThisTrackPlaying] = useState(false);
  const [isThisPlaylistPlaying, setIsThisPlaylistPlaying] = useState(false);
  const [isThisArtistPlaying, setIsThisArtistPlaying] = useState(false);
  const [isThisStationPlaying, setIsThisStationPlaying] = useState(false);
  const [isThisConcertPlaying, setIsThisConcertPlaying] = useState(false);
  const [isLikedTracksPlaying, setIsLikedTracksPlaying] = useState(false);
  const [isSameTracksPlaying, setIsSameTracksPlaying] = useState(false);
  const { isPlaying, currentlyPlaying, playlistPlayingId, allTracks } =
    useSpotify();
  const router = useRouter();
  const isStationTrack = router.pathname.includes("/station/track/");
  const isConcert = router.pathname.includes("/concert/");
  const isLikedTracks = router.pathname.includes("/collection/tracks");
  const stationTrackId = isStationTrack
    ? (router.query.trackId as string)
    : null;
  const concertId = isConcert ? (router.query.id as string) : null;
 
  useEffect(() => {
    const updateIsThisTrackPlaying = () => {
      if (!trackId) {
        setIsThisTrackPlaying(false);
        return;
      }
      const isTheSameTrackAsPlaying = trackId === currentlyPlaying?.id;
      setIsThisTrackPlaying(isTheSameTrackAsPlaying && isPlaying);
    };
 
    const updateIsThisArtistPlaying = () => {
      Iif (!uri) {
        setIsThisArtistPlaying(false);
        return;
      }
      const isTheSameArtistPlaying =
        uri === currentlyPlaying?.artists?.[0]?.uri;
      setIsThisArtistPlaying(isTheSameArtistPlaying && isPlaying);
    };
 
    const updateIsThisPlaylistPlaying = () => {
      Iif (!uriId) {
        setIsThisPlaylistPlaying(false);
        return;
      }
      const isTheSamePlaylistPlaying = !!(
        playlistPlayingId &&
        playlistPlayingId === uriId &&
        !isSingle
      );
      setIsThisPlaylistPlaying(isTheSamePlaylistPlaying && isPlaying);
    };
 
    const updateIsThisStationPlaying = () => {
      if (!stationTrackId) {
        setIsThisStationPlaying(false);
        return;
      }
      const stationTrackIdIsInAllTracks = allTracksToPlay
        ?.map((track) => track.id)
        ?.includes(stationTrackId);
      const stationTrackIdIsInAllTracksFromSpotify = allTracks
        ?.map((track) => track.id)
        ?.includes(stationTrackId);
      const isTheSameTracksPlaying =
        allTracksToPlay?.join("") === allTracks?.join("");
      const nowPlayingIsStationTrack = !!allTracks
        ?.map((track) => track.id)
        ?.includes(currentlyPlaying?.id);
      const isTheSameStationTracks = !!(
        stationTrackIdIsInAllTracks &&
        stationTrackIdIsInAllTracksFromSpotify &&
        isTheSameTracksPlaying &&
        nowPlayingIsStationTrack
      );
      const isTheSameStationPlaying =
        !playlistPlayingId || playlistPlayingId === stationTrackId;
      setIsThisStationPlaying(
        isTheSameStationPlaying && isTheSameStationTracks && isPlaying
      );
    };
 
    const updateIsThisConcertPlaying = () => {
      if (!concertId) {
        setIsThisConcertPlaying(false);
        return;
      }
      const isTheSameConcertTracks = !!allTracksToPlay
        ?.map((track) => track.id)
        .includes(currentlyPlaying?.id);
      const isTheSameConcertPlaying =
        !playlistPlayingId ||
        (playlistPlayingId === concertId && isTheSameConcertTracks);
      setIsThisConcertPlaying(isTheSameConcertPlaying && isPlaying);
    };
 
    const updateIsLikedTracksPlaying = () => {
      const isLikedTracksPlaying =
        playlistPlayingId === "tracks" && isPlaying && isLikedTracks;
      setIsLikedTracksPlaying(isLikedTracksPlaying);
    };
 
    const updateIsSameTracksPlaying = () => {
      if (!allTracksToPlay?.length || !allTracks?.length || uri || uriId) {
        setIsSameTracksPlaying(false);
        return;
      }
      const isTheSameTracksPlaying =
        allTracksToPlay?.join("") === allTracks?.join("");
      const isTrackPlayingInTracks = !!allTracksToPlay
        ?.map((track) => track.id)
        .includes(currentlyPlaying?.id);
      setIsSameTracksPlaying(
        isTheSameTracksPlaying &&
          isTrackPlayingInTracks &&
          !playlistPlayingId &&
          !isSingle &&
          isPlaying
      );
    };
 
    updateIsSameTracksPlaying();
    updateIsLikedTracksPlaying();
    updateIsThisTrackPlaying();
    updateIsThisArtistPlaying();
    updateIsThisPlaylistPlaying();
    updateIsThisStationPlaying();
    updateIsThisConcertPlaying();
  }, [
    allTracks,
    allTracksToPlay,
    concertId,
    currentlyPlaying?.artists,
    currentlyPlaying?.id,
    isPlaying,
    isSingle,
    isLikedTracks,
    playlistPlayingId,
    stationTrackId,
    trackId,
    uri,
    uriId,
    router.asPath,
    currentlyPlaying?.uri,
  ]);
 
  return (
    (isThisTrackPlaying && !isThisPlaylistPlaying) ||
    isThisArtistPlaying ||
    (isThisPlaylistPlaying &&
      !isThisArtistPlaying &&
      !isThisTrackPlaying &&
      !track) ||
    isThisStationPlaying ||
    isThisConcertPlaying ||
    isLikedTracksPlaying ||
    isSameTracksPlaying
  );
}