Skip to content

Commit 903510f

Browse files
[Brave News]: Fix feed layout (#21953)
1 parent 3209f9c commit 903510f

File tree

1 file changed

+15
-10
lines changed
  • components/brave_news/browser/resources

1 file changed

+15
-10
lines changed

components/brave_news/browser/resources/Feed.tsx

+15-10
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const saveScrollPos = (itemId: React.Key) => () => {
8080
}
8181

8282
const errors = {
83-
[FeedV2Error.ConnectionError]: <NotConnected/>,
83+
[FeedV2Error.ConnectionError]: <NotConnected />,
8484
[FeedV2Error.NoArticles]: <NoArticles />,
8585
[FeedV2Error.NoFeeds]: <NoFeeds />
8686
}
@@ -138,14 +138,16 @@ export default function Component({ feed, onSessionStart }: Props) {
138138
onSessionStart();
139139
}
140140
lastViewedCardCount.current = largestCardCount;
141+
}, {
142+
threshold: 1
141143
}));
142144

143-
const registerViewDepthObservation = (element: HTMLElement | null) => {
145+
const registerViewDepthObservation = React.useCallback((element: HTMLElement | null) => {
144146
if (!element) {
145147
return;
146148
}
147149
viewDepthIntersectionObserver.current.observe(element);
148-
};
150+
}, []);
149151

150152
// Only observe the bottom card
151153
const setLastCardRef = React.useCallback((el: HTMLElement | null) => {
@@ -157,6 +159,12 @@ export default function Component({ feed, onSessionStart }: Props) {
157159
const cards = React.useMemo(() => {
158160
const count = Math.min(feed?.items.length ?? 0, cardCount)
159161
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+
160168
return feed?.items.slice(0, count).map((item, index) => {
161169
let el: React.ReactNode
162170

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

187195
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>
194199
})
195-
}, [cardCount, feed?.items])
200+
}, [cardCount, feed?.items, registerViewDepthObservation])
196201

197202
return <FeedContainer className={NEWS_FEED_CLASS}>
198203
{feed

0 commit comments

Comments
 (0)