refactoring: improvements for lighthouse

This commit is contained in:
ner committed 2024-10-21 15:35:08 +03:00
1 parent f2f242a684
commit 38aebcbe28
27 files changed
+132 -93

No files matched your search

+6 -3
View File
@@ -1,14 +1,17 @@
import { useMemo } from 'react';
import { QueryKey, useInfiniteQuery } from '@tanstack/react-query';
import { QueryKey, useInfiniteQuery, useSuspenseInfiniteQuery } from '@tanstack/react-query';
interface Props {
suspense?: boolean;
queryKey: QueryKey;
enabled?: boolean;
queryFn: ({ page }: { page: string }) => Promise<Pagination<Show>>;
}
const useInfiniteList = ({ queryKey, queryFn, enabled }: Props) => {
const { data, hasNextPage, fetchNextPage, isFetched, isLoading } = useInfiniteQuery({
const useInfiniteList = ({ queryKey, queryFn, enabled, suspense }: Props) => {
const { data, hasNextPage, fetchNextPage, isFetched, isLoading } = (
suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
)({
enabled,
queryKey,
queryFn: ({ pageParam }) => queryFn({ page: pageParam }),
-33
View File
@@ -1,33 +0,0 @@
import { MutableRefObject, useCallback, useEffect, useRef, useState } from 'react';
const useSessionRef = <T>(key: string, initialValue: T): [MutableRefObject<T>, (value: T) => void] => {
const [state] = useState(() => {
if (typeof window === 'undefined') return initialValue;
const storedValue = sessionStorage.getItem(key);
return storedValue ? JSON.parse(storedValue) : initialValue;
});
const ref = useRef(state);
useEffect(() => {
const listener = () => sessionStorage.removeItem(key);
window.addEventListener('beforeunload', listener);
return () => window.removeEventListener('beforeunload', listener);
}, [key]);
const setter = useCallback(
(value: T) => {
sessionStorage.setItem(key, JSON.stringify(value));
ref.current = value;
},
[key]
);
return [ref, setter];
};
export default useSessionRef;
+4 -3
View File
@@ -1,8 +1,9 @@
import { accountStates, updateAccountStates } from '@/api';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { accountStates, updateAccountStates } from '@/api';
import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists';
import { useSession } from './useSession';
import { KEY as USERS_SHOWS_KEY } from './useUsersShows';
const KEY = ['show-state'];
@@ -49,7 +50,7 @@ export const useMutateShowState = (show: Show) => {
queryClient.setQueryData<ShowState>(key, snapshot);
},
onSuccess(_, { list, showType }) {
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType }) });
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType }), refetchType: 'all' });
}
});