feat(pagination): add updateItem for in-place list updates and optimize notifications
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:
@@ -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();
|
||||
|
||||
// 根据消息类型处理导航
|
||||
|
||||
@@ -47,6 +47,7 @@ export const UserProfileScreen: React.FC<UserProfileScreenProps> = ({ mode, user
|
||||
handleMessage,
|
||||
handleBlock,
|
||||
isBlocked,
|
||||
isBlockedProfile,
|
||||
handleSettings,
|
||||
handleEditProfile,
|
||||
isCurrentUser,
|
||||
@@ -54,7 +55,18 @@ export const UserProfileScreen: React.FC<UserProfileScreenProps> = ({ mode, user
|
||||
} = useUserProfile({ mode, userId, isDesktop, isTablet });
|
||||
|
||||
// 渲染帖子列表 - Twitter 风格无边框
|
||||
const renderPostList = useCallback((postList: Post[], emptyTitle: string, emptyDesc: string) => {
|
||||
const renderPostList = useCallback((postList: Post[], emptyTitle: string, emptyDesc: string, blockedTitle?: string, blockedDesc?: string) => {
|
||||
if (isBlockedProfile && blockedTitle) {
|
||||
return (
|
||||
<EmptyState
|
||||
title={blockedTitle}
|
||||
description={blockedDesc || ''}
|
||||
icon="account-off-outline"
|
||||
variant="modern"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (postList.length === 0) {
|
||||
return (
|
||||
<EmptyState
|
||||
@@ -85,7 +97,7 @@ export const UserProfileScreen: React.FC<UserProfileScreenProps> = ({ mode, user
|
||||
})}
|
||||
</View>
|
||||
);
|
||||
}, [currentUser?.id, handlePostAction, activeTab]);
|
||||
}, [currentUser?.id, handlePostAction, activeTab, isBlockedProfile]);
|
||||
|
||||
// 渲染内容
|
||||
const renderContent = useCallback(() => {
|
||||
@@ -95,7 +107,9 @@ export const UserProfileScreen: React.FC<UserProfileScreenProps> = ({ mode, user
|
||||
return renderPostList(
|
||||
posts,
|
||||
mode === 'self' ? '还没有帖子' : '这个用户还没有发布任何帖子',
|
||||
mode === 'self' ? '分享你的想法,发布第一条帖子吧' : ''
|
||||
mode === 'self' ? '分享你的想法,发布第一条帖子吧' : '',
|
||||
mode === 'other' ? '已将该用户拉黑' : undefined,
|
||||
mode === 'other' ? '你已将此用户拉黑,不再显示其帖子' : undefined
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ export interface UseUserProfileReturn {
|
||||
loading: boolean;
|
||||
refreshing: boolean;
|
||||
isBlocked: boolean;
|
||||
isBlockedProfile: boolean;
|
||||
|
||||
// Tab 相关
|
||||
activeTab: number;
|
||||
@@ -82,6 +83,7 @@ export const useUserProfile = (options: UseUserProfileOptions): UseUserProfileRe
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const [activeTab, setActiveTab] = useState(0);
|
||||
const [isBlocked, setIsBlocked] = useState(false);
|
||||
const [isBlockedProfile, setIsBlockedProfile] = useState(false);
|
||||
|
||||
// 获取其他用户数据(other 模式)
|
||||
const loadOtherUserData = useCallback(async (forceRefresh = false) => {
|
||||
@@ -98,6 +100,7 @@ export const useUserProfile = (options: UseUserProfileOptions): UseUserProfileRe
|
||||
|
||||
const response = await postService.getUserPosts(userId);
|
||||
setPosts(response.list);
|
||||
setIsBlockedProfile(response.is_blocked || false);
|
||||
|
||||
return userData;
|
||||
} catch (error) {
|
||||
@@ -407,6 +410,7 @@ case 'like':
|
||||
loading,
|
||||
refreshing,
|
||||
isBlocked,
|
||||
isBlockedProfile,
|
||||
activeTab,
|
||||
setActiveTab,
|
||||
scrollBottomInset,
|
||||
|
||||
Reference in New Issue
Block a user