@@ -5,6 +5,7 @@ import React, { useContext, useEffect, useRef, useState } from 'react';
5
5
import NotificationCard from '@/components/shared/Notification/NotificationCard' ;
6
6
import Skeleton from '@/components/skeleton/skeleton' ;
7
7
import { NotificationType } from '@/constants/types' ;
8
+ import { useScrollablePagination } from '@/hooks/useScrollablePagination' ;
8
9
import { getNotifications , updateNotification } from '@/services/notificationApi' ;
9
10
import { AppContext } from '../../../../context/AppContextProvider' ;
10
11
import logger from '../../../../logger.config.mjs' ;
@@ -21,10 +22,8 @@ export default function NotificationPage() {
21
22
const [ hasFetched , setHasFetched ] = useState ( false ) ;
22
23
const [ hasMore , setHasMore ] = useState ( true ) ;
23
24
24
- const debounceTimer = useRef < NodeJS . Timeout | null > ( null ) ;
25
25
const scrollContainerRef = useRef < HTMLDivElement | null > ( null ) ;
26
26
const observer = useRef < IntersectionObserver | null > ( null ) ;
27
- const loadMoreObserver = useRef < IntersectionObserver | null > ( null ) ;
28
27
29
28
const handleShopItemRef = ( node : HTMLElement | null ) => {
30
29
if ( node && observer . current ) {
@@ -102,44 +101,17 @@ export default function NotificationPage() {
102
101
fetchNotifications ( ) ;
103
102
} , [ currentUser ?. pi_uid ] ) ;
104
103
105
- useEffect ( ( ) => {
106
- if ( ! currentUser ?. pi_uid || ! hasMore ) return ;
107
-
108
- if ( loadMoreObserver . current ) {
109
- loadMoreObserver . current . disconnect ( ) ;
110
- }
111
-
112
- loadMoreObserver . current = new IntersectionObserver (
113
- ( entries ) => {
114
- const entry = entries [ 0 ] ;
115
- if ( entry . isIntersecting && ! isLoading && hasMore ) {
116
- setLoading ( true ) ;
117
- if ( debounceTimer . current ) clearTimeout ( debounceTimer . current ) ;
118
-
119
- debounceTimer . current = setTimeout ( ( ) => {
120
- fetchNotifications ( ) ;
121
- } , 1000 ) ; // ⏱️ 1s delay before triggering fetch
122
- }
123
- } ,
124
- {
125
- root : scrollContainerRef . current , // ✅ use actual DOM ref
126
- rootMargin : '0px' ,
127
- threshold : 1.0 ,
128
- }
129
- ) ;
130
-
131
- const currentRef = loadMoreRef . current ;
132
- if ( currentRef ) {
133
- loadMoreObserver . current . observe ( currentRef ) ;
134
- }
135
-
136
- return ( ) => {
137
- if ( loadMoreObserver . current && currentRef ) {
138
- loadMoreObserver . current . unobserve ( currentRef ) ;
139
- }
140
- } ;
141
- } , [ currentUser ?. pi_uid , hasMore , notifications ] ) ;
142
-
104
+ useScrollablePagination ( {
105
+ containerRef : scrollContainerRef ,
106
+ loadMoreRef,
107
+ fetchNextPage : async ( ) => {
108
+ setLoading ( true ) ;
109
+ await fetchNotifications ( ) ;
110
+ } ,
111
+ hasMore,
112
+ isLoading,
113
+ } ) ;
114
+
143
115
return (
144
116
< div className = "w-full md:w-[500px] md:mx-auto p-4" >
145
117
< div className = "text-center mb-7" >
0 commit comments