feat(pagination): add updateItem for in-place list updates and optimize notifications
Some checks failed
Frontend CI / build-and-push-web (push) Failing after 2m48s
Frontend CI / ota-android (push) Successful in 10m30s
Frontend CI / build-android-apk (push) Successful in 39m3s

Add `updateItem` function to cursor pagination hook for updating single items
without full list refresh. Use this in NotificationsScreen to mark messages
as read locally instead of refetching. Also add `is_blocked` detection in
postService and userProfile to show blocked state in profile screen.
This commit is contained in:
lafay
2026-04-25 11:26:13 +08:00
parent ad19bc2af7
commit f16f001f6c
6 changed files with 53 additions and 17 deletions

View File

@@ -110,6 +110,7 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
hasMore,
loadMore,
refresh,
updateItem,
error: paginationError,
} = useCursorPagination(fetchSystemMessages, { pageSize: 20 });
@@ -182,17 +183,16 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
}
}, [refresh, setSystemUnreadCount]);
// 页面加载和获得焦点时刷新,并自动标记所有消息为已读
// 使用 ref 防止重复执行,避免无限循环
const hasInitializedRef = useRef(false);
// 页面获得焦点时刷新未读数并标记全部已读
const markAllReadAndRefreshRef = useRef(handleMarkAllRead);
markAllReadAndRefreshRef.current = handleMarkAllRead;
useEffect(() => {
if (isFocused && !hasInitializedRef.current) {
hasInitializedRef.current = true;
if (isFocused) {
fetchUnreadCount();
// 进入界面自动标记所有消息为已读
handleMarkAllRead();
markAllReadAndRefreshRef.current();
}
}, [isFocused]); // 只依赖 isFocused函数使用 ref 稳定引用
}, [isFocused]);
// 启动入场动画
useEffect(() => {
@@ -308,13 +308,11 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
const messageId = String(message.id);
const wasUnread = message.is_read !== true;
await messageService.markSystemMessageRead(messageId);
// 【游标分页】不再直接修改 messages 状态,而是通过刷新获取最新数据
if (wasUnread) {
updateItem(messageId, { is_read: true });
setUnreadCount(prev => Math.max(0, prev - 1));
decrementSystemUnreadCount(1);
}
// 更新本地未读数以及全局 TabBar 红点
fetchUnreadCount();
messageManager.fetchUnreadCount();
// 根据消息类型处理导航