All files / Rindu/hooks useRouterEvents.ts

21.05% Statements 4/19
0% Branches 0/2
0% Functions 0/4
21.05% Lines 4/19

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 3719x   19x   19x 19x                                                              
import { MutableRefObject, useEffect } from "react";
 
import { useRouter } from "next/router";
 
import { useModal, useSpotify } from "hooks";
import { DisplayInFullScreen } from "types/spotify";
 
export function useRouterEvents(
  onAppScroll: () => void,
  appRef?: MutableRefObject<HTMLDivElement | undefined>
): void {
  const router = useRouter();
  const { setDisplayInFullScreen } = useSpotify();
  const { setModalData } = useModal();
 
  useEffect(() => {
    const app =
      appRef?.current ??
      document.querySelector("#right .simplebar-content-wrapper");
 
    const handleRouteChange = () => {
      app?.scrollTo(0, 0);
      setDisplayInFullScreen(DisplayInFullScreen.App);
      setModalData(null);
    };
 
    router.events.on("routeChangeComplete", handleRouteChange);
 
    app?.addEventListener("scroll", onAppScroll);
 
    return () => {
      router.events.off("routeChangeComplete", handleRouteChange);
      app?.removeEventListener("scroll", onAppScroll);
    };
  }, [router, appRef, onAppScroll, setDisplayInFullScreen, setModalData]);
}