refactor(ProcessPostUseCase, useCursorPagination, useDifferentialPosts): enhance post merging and loading states
- Introduced new methods for merging posts and comments efficiently, improving performance during data updates. - Updated loading state management in hooks to include initial loading and loading more states for better user feedback. - Refactored HomeScreen and PostDetailScreen to utilize the new loading states and merging logic, enhancing user experience during data fetching. - Improved pagination handling in useCursorPagination and useDifferentialPosts to ensure consistent data management across components.
This commit is contained in:
@@ -103,6 +103,7 @@ export function useMessages(conversationId: string | null): UseMessagesReturn {
|
||||
const [messages, setMessages] = useState<MessageResponse[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [hasMore, setHasMore] = useState(true);
|
||||
const loadMoreInFlightRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!conversationId) {
|
||||
@@ -156,23 +157,23 @@ export function useMessages(conversationId: string | null): UseMessagesReturn {
|
||||
}, [conversationId]);
|
||||
|
||||
const loadMore = useCallback(async () => {
|
||||
if (!conversationId || isLoading || !hasMore) return;
|
||||
if (!conversationId || !hasMore || loadMoreInFlightRef.current) return;
|
||||
|
||||
const currentMessages = messageManager.getMessages(conversationId);
|
||||
if (currentMessages.length === 0) return;
|
||||
|
||||
// 获取最早的消息seq
|
||||
const minSeq = Math.min(...currentMessages.map(m => m.seq));
|
||||
// 消息数组在 MessageManager 内保持 seq 升序,首项即最早消息
|
||||
const minSeq = currentMessages[0]?.seq ?? Math.min(...currentMessages.map(m => m.seq));
|
||||
|
||||
setIsLoading(true);
|
||||
loadMoreInFlightRef.current = true;
|
||||
const loadedMessages = await messageManager.loadMoreMessages(conversationId, minSeq, 20);
|
||||
setIsLoading(false);
|
||||
loadMoreInFlightRef.current = false;
|
||||
|
||||
// 如果没有加载到新消息,说明没有更多了
|
||||
if (loadedMessages.length === 0) {
|
||||
setHasMore(false);
|
||||
}
|
||||
}, [conversationId, isLoading, hasMore]);
|
||||
}, [conversationId, hasMore]);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
if (!conversationId) return;
|
||||
|
||||
Reference in New Issue
Block a user