mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-05-05 01:27:46 +07:00
refactored hooks directory
This commit is contained in:
parent
efa3ba902d
commit
f76a5a4798
106 changed files with 122 additions and 144 deletions
35
src/hooks/lab/useLab.ts
Normal file
35
src/hooks/lab/useLab.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { useShallowSelect } from '~/hooks/data/useShallowSelect';
|
||||
import {
|
||||
selectLabList,
|
||||
selectLabStatsHeroes,
|
||||
selectLabStatsLoading,
|
||||
selectLabStatsTags,
|
||||
selectLabUpdatesNodes,
|
||||
} from '~/redux/lab/selectors';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { labGetList, labGetMore, labGetStats } from '~/redux/lab/actions';
|
||||
|
||||
export const useLab = () => {
|
||||
const { is_loading: isLoading, nodes, count } = useShallowSelect(selectLabList);
|
||||
const dispatch = useDispatch();
|
||||
const tags = useShallowSelect(selectLabStatsTags);
|
||||
const heroes = useShallowSelect(selectLabStatsHeroes);
|
||||
const isLoadingStats = useShallowSelect(selectLabStatsLoading);
|
||||
const updates = useShallowSelect(selectLabUpdatesNodes);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(labGetList());
|
||||
dispatch(labGetStats());
|
||||
}, [dispatch]);
|
||||
|
||||
const onLoadMore = useCallback(() => {
|
||||
if (nodes.length >= count) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch(labGetMore());
|
||||
}, [nodes, count, dispatch]);
|
||||
|
||||
return { isLoading, nodes, count, onLoadMore, tags, heroes, isLoadingStats, updates };
|
||||
};
|
44
src/hooks/lab/useLabPagination.ts
Normal file
44
src/hooks/lab/useLabPagination.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { useCallback, useEffect, useMemo } from 'react';
|
||||
|
||||
export const useLabPagination = (
|
||||
isLoading: boolean,
|
||||
columns: Element[],
|
||||
onLoadMore: () => void
|
||||
) => {
|
||||
const loadOnIntersection = useCallback<IntersectionObserverCallback>(
|
||||
entries => {
|
||||
const isVisible = entries.some(entry => entry.intersectionRatio > 0);
|
||||
|
||||
if (!isVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
onLoadMore();
|
||||
},
|
||||
[onLoadMore]
|
||||
);
|
||||
|
||||
const observer = useMemo(
|
||||
() =>
|
||||
new IntersectionObserver(loadOnIntersection, {
|
||||
threshold: [0],
|
||||
}),
|
||||
[loadOnIntersection]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (isLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastItems = Array.from(columns)
|
||||
.map(col => col.children.item(col.childNodes.length - 1))
|
||||
.filter(el => el) as Element[];
|
||||
|
||||
lastItems.forEach(item => observer.observe(item));
|
||||
|
||||
return () => {
|
||||
lastItems.forEach(item => observer.unobserve(item));
|
||||
};
|
||||
}, [observer, columns, isLoading]);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue