diff --git a/src/hooks/useLanguage.ts b/src/hooks/useLanguage.ts index d6f01bd..f1cc5f7 100644 --- a/src/hooks/useLanguage.ts +++ b/src/hooks/useLanguage.ts @@ -1,9 +1,17 @@ +import type { Dispatch, SetStateAction } from 'react'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { changeLanguage } from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; import { fallbackLng } from 'utils/i18n'; +export type UseLanguageResult = { + currentLanguage: string; + setLanguage: Dispatch>; +}; + +export type UseLanguageHook = () => UseLanguageResult; + export const useLanguage = () => { const { i18n } = useTranslation(); const [currentLanguage, setLanguage] = useState(i18n.language); @@ -20,7 +28,6 @@ export const useLanguage = () => { changeLanguage(currentLanguage); } } - // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentLanguage]); return { diff --git a/src/hooks/useLocalStorage.ts b/src/hooks/useLocalStorage.ts index 0ee3b8f..a1b0a00 100644 --- a/src/hooks/useLocalStorage.ts +++ b/src/hooks/useLocalStorage.ts @@ -1,5 +1,17 @@ +import type { Dispatch, SetStateAction } from 'react'; import { useState, useEffect } from 'react'; +export type UseLocalStorageParamKey = string; + +export type UseLocalStorageParamValue = T | undefined; + +export type UseLocalStorageResult = [value: T, setValue: Dispatch>, remove: () => void]; + +export type UseLocalStorageHook = ( + key: UseLocalStorageParamKey, + defaultValue?: UseLocalStorageParamValue +) => UseLocalStorageResult; + const useLocalStorage = (key: string, defaultValue?: T) => { const [value, setValue] = useState(() => { let storedValue; diff --git a/src/hooks/useSettings.ts b/src/hooks/useSettings.ts index c02486f..2273945 100644 --- a/src/hooks/useSettings.ts +++ b/src/hooks/useSettings.ts @@ -1,12 +1,24 @@ import { useEffect } from 'react'; -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { UseMutateFunction, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { userGetSettings } from 'utils/clients/api/methods/userGetSettings'; import { settingsUpdate } from 'utils/clients/api/methods/settingsUpdate'; import type { Settings } from 'utils/types/settings'; import { useLanguage } from './useLanguage'; import { useUserData } from './useUserData'; -export const useSettings = () => { +export type UseSettingsResult = { + settings: Settings; + updateSettings: UseMutateFunction, unknown>; + isLoading: boolean; + isError: boolean; + isFetching: boolean; + isReady: boolean; + isSuccess: boolean; +}; + +export type UseSettingsHook = () => UseSettingsResult; + +export const useSettings: UseSettingsHook = () => { const queryClient = useQueryClient(); const { currentLanguage, setLanguage } = useLanguage(); const { isReady: userIsReady, isLoggedIn } = useUserData(); @@ -38,9 +50,9 @@ export const useSettings = () => { return { settings: data, updateSettings: saveSettings.mutate, + isLoading, isError, isFetching, - isLoading, isReady: isFetched, isSuccess, }; diff --git a/src/hooks/useUserData.ts b/src/hooks/useUserData.ts index 8b54b38..f29d484 100644 --- a/src/hooks/useUserData.ts +++ b/src/hooks/useUserData.ts @@ -6,12 +6,24 @@ import ReactGA from 'react-ga-neo'; import useLocalStorage from './useLocalStorage'; import { userGetProfile } from 'utils/clients/api/methods/userGetProfile'; import { userTransformer } from 'utils/clients/api/transformers/user.transformer'; +import type { User } from 'utils/types/user'; import { sha256 } from 'utils/common'; import { saveToLocalStorage } from 'localstorage'; const emptyUser = userTransformer(null); -export const useUserData = () => { +export type UseUserDataResult = { + user?: User; + isLoggedIn: boolean; + isLoading: boolean; + isError: boolean; + isFetching: boolean; + isReady: boolean; +}; + +export type UseUserDataHook = () => UseUserDataResult; + +export const useUserData: UseUserDataHook = () => { const [storedUserData, updateStoredUserData] = useLocalStorage('user', emptyUser); const growthbook = useGrowthBook(); const { data, isLoading, isFetching, isSuccess, isStale, isError } = useQuery({