mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
32 lines
969 B
TypeScript
32 lines
969 B
TypeScript
import { useEffect, useRef } from 'react';
|
|
|
|
interface UseSuggestionFeedLoaderOptions {
|
|
currentFeedLength: number;
|
|
feedLength: number;
|
|
hasMore: boolean;
|
|
loadMore: () => Promise<void>;
|
|
requestKey: string;
|
|
shouldLoad: boolean;
|
|
}
|
|
|
|
const EMPTY_REQUEST_KEY = '';
|
|
|
|
export const useSuggestionFeedLoader = ({ currentFeedLength, feedLength, hasMore, loadMore, requestKey, shouldLoad }: UseSuggestionFeedLoaderOptions) => {
|
|
const lastRequestKeyRef = useRef(EMPTY_REQUEST_KEY);
|
|
|
|
useEffect(() => {
|
|
if (!shouldLoad || !requestKey || !hasMore || feedLength > currentFeedLength) {
|
|
lastRequestKeyRef.current = EMPTY_REQUEST_KEY;
|
|
return;
|
|
}
|
|
|
|
const nextRequestKey = `${requestKey}:${currentFeedLength}:${feedLength}`;
|
|
if (lastRequestKeyRef.current === nextRequestKey) {
|
|
return;
|
|
}
|
|
|
|
lastRequestKeyRef.current = nextRequestKey;
|
|
void loadMore();
|
|
}, [currentFeedLength, feedLength, hasMore, loadMore, requestKey, shouldLoad]);
|
|
};
|