|
1 | 1 | import { useOnScreen } from '@mezon/core'; |
2 | 2 | import { ThreadsEntity } from '@mezon/store'; |
3 | | -import { MutableRefObject, useEffect } from 'react'; |
| 3 | +import { useEffect, useRef } from 'react'; |
4 | 4 | import GroupThreads from './GroupThreads'; |
5 | 5 | import { getActiveThreads, getJoinedThreadsWithinLast30Days, getThreadsOlderThan30Days } from './hepler'; |
6 | 6 |
|
7 | 7 | type ThreadListProps = { |
8 | | - hasMore: boolean; |
9 | 8 | isLoading: boolean; |
10 | 9 | loadMore: () => void | undefined | null; |
11 | 10 | threads: ThreadsEntity[]; |
12 | | - preventClosePannel: MutableRefObject<boolean>; |
| 11 | + preventClosePannel: React.MutableRefObject<boolean>; |
13 | 12 | }; |
14 | 13 |
|
15 | | -export default function ThreadList({ hasMore, isLoading, loadMore, threads, preventClosePannel }: ThreadListProps) { |
| 14 | +export default function ThreadList({ isLoading, threads, loadMore, preventClosePannel }: ThreadListProps) { |
| 15 | + const ulRef = useRef<HTMLUListElement | null>(null); |
16 | 16 | const activeThreads = getActiveThreads(threads); |
17 | 17 | const joinedThreads = getJoinedThreadsWithinLast30Days(threads); |
18 | 18 | const oldThreads = getThreadsOlderThan30Days(threads); |
19 | 19 |
|
20 | | - const { measureRef, isIntersecting, observer } = useOnScreen(); |
| 20 | + const { measureRef, isIntersecting, observer } = useOnScreen({ root: ulRef.current }); |
| 21 | + |
| 22 | + const lastThread = threads[threads.length - 1]; |
| 23 | + |
| 24 | + const isLastInActive = activeThreads.includes(lastThread); |
| 25 | + const isLastInJoined = joinedThreads.includes(lastThread); |
| 26 | + const isLastInOld = oldThreads.includes(lastThread); |
21 | 27 |
|
22 | 28 | useEffect(() => { |
23 | | - if (isIntersecting && hasMore) { |
| 29 | + if (isIntersecting) { |
24 | 30 | loadMore(); |
25 | 31 | observer?.disconnect(); |
26 | 32 | } |
27 | | - }, [isIntersecting, hasMore, loadMore]); |
| 33 | + }, [isIntersecting, loadMore]); |
28 | 34 |
|
29 | 35 | return ( |
30 | | - <ul className="pb-4 pr-4 pl-4"> |
31 | | - <GroupThreads preventClosePannel={preventClosePannel} title="Active Threads" threads={activeThreads} /> |
32 | | - <GroupThreads preventClosePannel={preventClosePannel} title="Joined Threads (Last 30 Days)" threads={joinedThreads} /> |
33 | | - <GroupThreads preventClosePannel={preventClosePannel} title="Older Threads" threads={oldThreads} measureRef={measureRef} /> |
| 36 | + <ul ref={ulRef} className="pb-4 pr-4 pl-4 overflow-y-auto h-[500px]"> |
| 37 | + <GroupThreads |
| 38 | + preventClosePannel={preventClosePannel} |
| 39 | + title="Active Threads" |
| 40 | + threads={activeThreads} |
| 41 | + measureRef={isLastInActive ? measureRef : undefined} |
| 42 | + /> |
| 43 | + <GroupThreads |
| 44 | + preventClosePannel={preventClosePannel} |
| 45 | + title="Joined Threads (Last 30 Days)" |
| 46 | + threads={joinedThreads} |
| 47 | + measureRef={isLastInJoined ? measureRef : undefined} |
| 48 | + /> |
| 49 | + <GroupThreads |
| 50 | + preventClosePannel={preventClosePannel} |
| 51 | + title="Older Threads" |
| 52 | + threads={oldThreads} |
| 53 | + measureRef={isLastInOld ? measureRef : undefined} |
| 54 | + /> |
34 | 55 | {isLoading && <li>Loading...</li>} |
35 | 56 | </ul> |
36 | 57 | ); |
|
0 commit comments