Skip to content

Commit

Permalink
Uplift of #21953 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Feb 13, 2024
1 parent f076ad1 commit bb5e06c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions components/brave_news/browser/resources/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const saveScrollPos = (itemId: React.Key) => () => {
}

const errors = {
[FeedV2Error.ConnectionError]: <NotConnected/>,
[FeedV2Error.ConnectionError]: <NotConnected />,
[FeedV2Error.NoArticles]: <NoArticles />,
[FeedV2Error.NoFeeds]: <NoFeeds />
}
Expand Down Expand Up @@ -138,14 +138,16 @@ export default function Component({ feed, onSessionStart }: Props) {
onSessionStart();
}
lastViewedCardCount.current = largestCardCount;
}, {
threshold: 1
}));

const registerViewDepthObservation = (element: HTMLElement | null) => {
const registerViewDepthObservation = React.useCallback((element: HTMLElement | null) => {
if (!element) {
return;
}
viewDepthIntersectionObserver.current.observe(element);
};
}, []);

// Only observe the bottom card
const setLastCardRef = React.useCallback((el: HTMLElement | null) => {
Expand All @@ -157,6 +159,12 @@ export default function Component({ feed, onSessionStart }: Props) {
const cards = React.useMemo(() => {
const count = Math.min(feed?.items.length ?? 0, cardCount)
let currentCardCount = 0;
const setRefAtIndex = (index: number) => (element: HTMLElement | null) => {
const isLast = index === count - 1
if (isLast) setLastCardRef(element)
registerViewDepthObservation(element)
}

return feed?.items.slice(0, count).map((item, index) => {
let el: React.ReactNode

Expand Down Expand Up @@ -185,14 +193,11 @@ export default function Component({ feed, onSessionStart }: Props) {
}

const key = getKey(item, index)
return <>
<div className={CARD_CLASS} onClickCapture={saveScrollPos(key)} key={key} data-id={key} ref={index === count - 1 ? setLastCardRef : undefined}>
{el}
</div>
<div key={`${key}-counter`} {...{ [CARD_COUNT_ATTRIBUTE]: currentCardCount }} ref={registerViewDepthObservation} />
</>
return <div className={CARD_CLASS} onClickCapture={saveScrollPos(key)} key={key} data-id={key} {...{ [CARD_COUNT_ATTRIBUTE]: currentCardCount }} ref={setRefAtIndex(index)}>
{el}
</div>
})
}, [cardCount, feed?.items])
}, [cardCount, feed?.items, registerViewDepthObservation])

return <FeedContainer className={NEWS_FEED_CLASS}>
{feed
Expand Down

0 comments on commit bb5e06c

Please sign in to comment.