Skip to content

Commit

Permalink
Add types to hooks (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
silversonicaxel authored Sep 15, 2024
1 parent 51681d4 commit 74188a8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/hooks/useLanguage.ts
Original file line number Diff line number Diff line change
@@ -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<SetStateAction<string>>;
};

export type UseLanguageHook = () => UseLanguageResult;

export const useLanguage = () => {
const { i18n } = useTranslation();
const [currentLanguage, setLanguage] = useState(i18n.language);
Expand All @@ -20,7 +28,6 @@ export const useLanguage = () => {
changeLanguage(currentLanguage);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentLanguage]);

return {
Expand Down
12 changes: 12 additions & 0 deletions src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { Dispatch, SetStateAction } from 'react';
import { useState, useEffect } from 'react';

export type UseLocalStorageParamKey = string;

export type UseLocalStorageParamValue<T> = T | undefined;

export type UseLocalStorageResult<T> = [value: T, setValue: Dispatch<SetStateAction<T>>, remove: () => void];

export type UseLocalStorageHook<T> = (
key: UseLocalStorageParamKey,
defaultValue?: UseLocalStorageParamValue<T>
) => UseLocalStorageResult<T>;

const useLocalStorage = <T>(key: string, defaultValue?: T) => {
const [value, setValue] = useState<T | undefined>(() => {
let storedValue;
Expand Down
18 changes: 15 additions & 3 deletions src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -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<Settings, Error, Partial<Settings>, 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();
Expand Down Expand Up @@ -38,9 +50,9 @@ export const useSettings = () => {
return {
settings: data,
updateSettings: saveSettings.mutate,
isLoading,
isError,
isFetching,
isLoading,
isReady: isFetched,
isSuccess,
};
Expand Down
14 changes: 13 additions & 1 deletion src/hooks/useUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 74188a8

Please sign in to comment.