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 | 4x 13x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 8x 12x 2x 2x 2x 12x 12x 1x 1x 1x 1x 12x 2x 1x 1x 1x 1x 12x 8x 8x 12x | import _ from "lodash"; import { useState, useCallback, useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; import { toast } from "react-toastify"; import apiManager from "@/apiManager"; import useUserStore from "@/store/useUserStore"; import useDeleteSessionMutation from "@/mutations/useDeleteSessionMutation"; import { IMovieList } from "@/interfaces"; const useNavbar = () => { const navigate = useNavigate(); const { t, i18n } = useTranslation(); const inputRef = useRef<HTMLInputElement | null>(null); const { isAuthenticated, sessionId, username, accentColor } = useUserStore(); const [suggestions, setSuggestions] = useState<IMovieList[]>([]); const [searchValue, setSearchValue] = useState<string>(""); const [isDrawerOpen, setIsDrawerOpen] = useState<boolean>(false); const [toggleSearchBar, setToggleSearchBar] = useState<boolean>(false); const [suggestionsLoading, setSuggestionsLoading] = useState<boolean>(false); const { deleteSessionMutation } = useDeleteSessionMutation(); const { data: trendingMovies } = useQuery({ queryKey: [apiManager.getTrendingMovies.key], queryFn: () => apiManager.getTrendingMovies.func(), }); const handleToggleSearchBar = () => { setToggleSearchBar(!toggleSearchBar); setTimeout(() => { Iif (inputRef.current) { inputRef.current.focus(); } }, 0); }; const fetchMovieSuggestions = useCallback( _.debounce(async (query) => { if (!query) { setSuggestions([]); return; } try { setSuggestionsLoading(true); const response = await apiManager.searchMovies.func(query, i18n.language); setSuggestions(response.results); } catch (err) { toast("There was an error"); } finally { setSuggestionsLoading(false); } }, 500), [] ); const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const newSearchValue = e.target.value; setSearchValue(newSearchValue); fetchMovieSuggestions.cancel(); fetchMovieSuggestions(newSearchValue); }; const handleEnterKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => { if (e.key === "Enter" && /[a-zA-Z0-9]/.test(searchValue)) { setSearchValue(""); setToggleSearchBar(!toggleSearchBar); navigate(`/search-results?query=${searchValue}`); I} else if (e.key === "Escape") { setSearchValue(""); setToggleSearchBar(!toggleSearchBar); } }; useEffect(() => { return () => { fetchMovieSuggestions.cancel(); }; }, [fetchMovieSuggestions]); return { ui: { accentColor, isDrawerOpen, setIsDrawerOpen, toggleSearchBar, setToggleSearchBar, handleToggleSearchBar, inputRef, }, auth: { deleteSessionMutation, }, user: { isAuthenticated, sessionId, username, }, movie: { suggestions, suggestionsLoading, trendingMovies, }, t, i18n, searchValue, handleInputChange, handleEnterKeyPress, }; }; export default useNavbar; |