summaryrefslogtreecommitdiff
path: root/apps/web/src/hooks/use-infinite-scroll.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2025-08-15 02:12:16 +0200
committerMaze Winther <mazewinther@gmail.com>2025-08-15 02:12:16 +0200
commit7f91c9ca00cd51bea1cd1e97eeb3c58b7e7a1bae (patch)
tree25c6cddf67e3227d99c2c28f71496851962f1f45 /apps/web/src/hooks/use-infinite-scroll.ts
parent5e8a489a147c148b6495a6a25fb4dd9725e6fca6 (diff)
parentc3f3345d7b1ac820b411a5adbca3172378642953 (diff)
downloadopencut-7f91c9ca00cd51bea1cd1e97eeb3c58b7e7a1bae.tar.gz
Merge branch 'staging' of https://github.com/mazeincoding/AppCut into staging
Diffstat (limited to 'apps/web/src/hooks/use-infinite-scroll.ts')
-rw-r--r--apps/web/src/hooks/use-infinite-scroll.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/apps/web/src/hooks/use-infinite-scroll.ts b/apps/web/src/hooks/use-infinite-scroll.ts
new file mode 100644
index 00000000..0f8db198
--- /dev/null
+++ b/apps/web/src/hooks/use-infinite-scroll.ts
@@ -0,0 +1,35 @@
+import { useRef, useCallback } from "react";
+
+interface UseInfiniteScrollOptions {
+ onLoadMore: () => void;
+ hasMore: boolean;
+ isLoading: boolean;
+ threshold?: number;
+ enabled?: boolean;
+}
+
+export function useInfiniteScroll({
+ onLoadMore,
+ hasMore,
+ isLoading,
+ threshold = 200,
+ enabled = true,
+}: UseInfiniteScrollOptions) {
+ const scrollAreaRef = useRef<HTMLDivElement>(null);
+
+ const handleScroll = useCallback(
+ (event: React.UIEvent<HTMLDivElement>) => {
+ if (!enabled) return;
+
+ const { scrollTop, scrollHeight, clientHeight } = event.currentTarget;
+ const nearBottom = scrollTop + clientHeight >= scrollHeight - threshold;
+
+ if (nearBottom && hasMore && !isLoading) {
+ onLoadMore();
+ }
+ },
+ [onLoadMore, hasMore, isLoading, threshold, enabled]
+ );
+
+ return { scrollAreaRef, handleScroll };
+}