All files / src/hooks useMovieDetails.ts

0% Statements 0/31
0% Branches 0/8
0% Functions 0/6
0% Lines 0/31

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                                                                                                                                                                               
import React from "react";
import { useTranslation } from "react-i18next";
import { extractColors } from "extract-colors";
import { useParams } from "react-router-dom";
 
import useMovieDetailsQuery from "@/queries/useMovieDetailsQuery";
import { IApiFunction } from "@/interfaces";
import useMovieStore from "@/store/useMovieStore";
import useUserStore from "@/store/useUserStore";
 
const useMovieDetails = (apiFunctions: IApiFunction) => {
  const { i18n, t } = useTranslation();
  const { movieId } = useParams();
  const { setMovieStatus } = useMovieStore();
  const { sessionId, isAuthenticated } = useUserStore();
 
  const [posterBackDropColors, setPosterBackDropColors] = React.useState<any>([]);
 
  const {
    data: movieDetails,
    isLoading,
    error,
  } = useMovieDetailsQuery(apiFunctions, movieId, i18n, sessionId);
 
  React.useEffect(() => {
    const movieStatus = movieDetails?.account_states;
 
    if (isAuthenticated && movieStatus) {
      setMovieStatus(movieStatus);
    }
  }, [isAuthenticated, movieDetails]);
 
  React.useEffect(() => {
    const fetchColor = async () => {
      try {
        if (movieDetails) {
          const posterPath = `https://image.tmdb.org/t/p/w1920_and_h800_multi_faces/${movieDetails.backdrop_path}`;
          const colors = await extractColors(posterPath, options);
          setPosterBackDropColors(colors);
        }
      } catch (error) {
        console.error(error);
      }
    };
 
    fetchColor();
  }, [movieDetails]);
 
  const options = {
    pixels: 64000,
    distance: 0.22,
    crossOrigin: "Anonymous",
    saturationDistance: 0.2,
    lightnessDistance: 0.2,
    hueDistance: 0.083333333,
  } as {};
 
  React.useEffect(() => {
    const fetchColor = async () => {
      try {
        if (movieDetails) {
          const posterPath = `https://image.tmdb.org/t/p/w1920_and_h800_multi_faces/${movieDetails.backdrop_path}`;
          const colors = await extractColors(posterPath, options);
          setPosterBackDropColors(colors);
        }
      } catch (error) {
        console.error(error);
      }
    };
 
    fetchColor();
  }, [movieDetails]);
 
  return {
    i18n,
    t,
    movieId,
    posterBackDropColors,
    setPosterBackDropColors,
    movieDetails,
    isLoading,
    error,
    options,
  };
};
 
export default useMovieDetails;