@@ -80,7 +80,7 @@ const saveScrollPos = (itemId: React.Key) => () => {
80
80
}
81
81
82
82
const errors = {
83
- [ FeedV2Error . ConnectionError ] : < NotConnected /> ,
83
+ [ FeedV2Error . ConnectionError ] : < NotConnected /> ,
84
84
[ FeedV2Error . NoArticles ] : < NoArticles /> ,
85
85
[ FeedV2Error . NoFeeds ] : < NoFeeds />
86
86
}
@@ -138,14 +138,16 @@ export default function Component({ feed, onSessionStart }: Props) {
138
138
onSessionStart ( ) ;
139
139
}
140
140
lastViewedCardCount . current = largestCardCount ;
141
+ } , {
142
+ threshold : 1
141
143
} ) ) ;
142
144
143
- const registerViewDepthObservation = ( element : HTMLElement | null ) => {
145
+ const registerViewDepthObservation = React . useCallback ( ( element : HTMLElement | null ) => {
144
146
if ( ! element ) {
145
147
return ;
146
148
}
147
149
viewDepthIntersectionObserver . current . observe ( element ) ;
148
- } ;
150
+ } , [ ] ) ;
149
151
150
152
// Only observe the bottom card
151
153
const setLastCardRef = React . useCallback ( ( el : HTMLElement | null ) => {
@@ -157,6 +159,12 @@ export default function Component({ feed, onSessionStart }: Props) {
157
159
const cards = React . useMemo ( ( ) => {
158
160
const count = Math . min ( feed ?. items . length ?? 0 , cardCount )
159
161
let currentCardCount = 0 ;
162
+ const setRefAtIndex = ( index : number ) => ( element : HTMLElement | null ) => {
163
+ const isLast = index === count - 1
164
+ if ( isLast ) setLastCardRef ( element )
165
+ registerViewDepthObservation ( element )
166
+ }
167
+
160
168
return feed ?. items . slice ( 0 , count ) . map ( ( item , index ) => {
161
169
let el : React . ReactNode
162
170
@@ -185,14 +193,11 @@ export default function Component({ feed, onSessionStart }: Props) {
185
193
}
186
194
187
195
const key = getKey ( item , index )
188
- return < >
189
- < div className = { CARD_CLASS } onClickCapture = { saveScrollPos ( key ) } key = { key } data-id = { key } ref = { index === count - 1 ? setLastCardRef : undefined } >
190
- { el }
191
- </ div >
192
- < div key = { `${ key } -counter` } { ...{ [ CARD_COUNT_ATTRIBUTE ] : currentCardCount } } ref = { registerViewDepthObservation } />
193
- </ >
196
+ return < div className = { CARD_CLASS } onClickCapture = { saveScrollPos ( key ) } key = { key } data-id = { key } { ...{ [ CARD_COUNT_ATTRIBUTE ] : currentCardCount } } ref = { setRefAtIndex ( index ) } >
197
+ { el }
198
+ </ div >
194
199
} )
195
- } , [ cardCount , feed ?. items ] )
200
+ } , [ cardCount , feed ?. items , registerViewDepthObservation ] )
196
201
197
202
return < FeedContainer className = { NEWS_FEED_CLASS } >
198
203
{ feed
0 commit comments