All files / Rindu/hooks useShortCuts.ts

53.57% Statements 15/28
27.27% Branches 6/22
66.66% Functions 4/6
53.84% Lines 14/26

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 6119x     19x   41x                     41x   4x 4x     4x             4x     4x               4x     4x                       41x 41x 41x      
import { Dispatch, SetStateAction, useCallback, useEffect } from "react";
 
import { AudioPlayer } from "hooks/useSpotifyPlayer";
import { DisplayInFullScreen, ITrack } from "types/spotify";
 
export function useShortCuts({
  ignoreShortcuts,
  setDisplayInFullScreen,
  player,
  currentlyPlaying,
}: {
  ignoreShortcuts: boolean;
  setDisplayInFullScreen: Dispatch<SetStateAction<DisplayInFullScreen>>;
  player: Spotify.Player | AudioPlayer | undefined;
  currentlyPlaying: ITrack | undefined;
}): void {
  const handleKeyUp = useCallback(
    (e: KeyboardEvent) => {
      Iif (ignoreShortcuts) return;
      Iif (e.key === "n" && player && typeof player.nextTrack === "function") {
        player.nextTrack();
      }
      Iif (
        e.key === "b" &&
        player &&
        typeof player.previousTrack === "function"
      ) {
        player.previousTrack();
      }
      Iif (e.key === "m" && player && typeof player.togglePlay === "function") {
        player.togglePlay();
      }
      Iif (e.key === "f" || e.key === "p") {
        setDisplayInFullScreen((displayInFullScreen) => {
          Iif (displayInFullScreen !== DisplayInFullScreen.Player) {
            return DisplayInFullScreen.Player;
          }
          return DisplayInFullScreen.App;
        });
      }
      Iif (e.key === "Escape") {
        setDisplayInFullScreen(DisplayInFullScreen.App);
      }
      Iif (e.key === "l" && currentlyPlaying?.type === "track") {
        setDisplayInFullScreen((displayInFullScreen) => {
          Iif (displayInFullScreen !== DisplayInFullScreen.Lyrics) {
            return DisplayInFullScreen.Lyrics;
          }
          return DisplayInFullScreen.App;
        });
      }
    },
    [currentlyPlaying?.type, ignoreShortcuts, player, setDisplayInFullScreen]
  );
 
  useEffect(() => {
    document.addEventListener("keyup", handleKeyUp);
    return () => document.removeEventListener("keyup", handleKeyUp);
  }, [handleKeyUp]);
}