feat(pagination): implement cursor-based pagination across the app

Add useCursorPagination hook and update multiple screens and services
to use cursor-based pagination for better performance and consistency.

- Add useCursorPagination hook with deduplication and caching support
- Add cursor pagination types to infrastructure layer
- Refactor HomeScreen, PostDetailScreen, SearchScreen for posts/comments
- Refactor GroupMembersScreen, JoinGroupScreen for groups/members
- Refactor MessageListScreen, NotificationsScreen for messages
- Update post, message, group, comment, notification services with cursor endpoints
- Add CursorPaginationRequest/Response DTOs
- Remove deprecated OPTIMIZATION_DESIGN.md documentation
This commit is contained in:
lafay
2026-03-20 23:00:27 +08:00
parent a005fb0a15
commit 8a0aea1c59
20 changed files with 2464 additions and 2166 deletions

View File

@@ -5,6 +5,7 @@
import { api, PaginatedData } from './api';
import { Post, CreatePostInput } from '../types';
import { CursorPaginationRequest, CursorPaginationResponse } from '../types/dto';
// 帖子列表响应
interface PostListResponse {
@@ -281,6 +282,92 @@ class PostService {
return false;
}
}
// ==================== 游标分页方法 ====================
/**
* 获取帖子列表(游标分页)
* GET /api/v1/posts/cursor
* @param params 游标分页请求参数(包含 post_type 可选recommend, follow, hot, latest
*/
async getPostsCursor(
params: CursorPaginationRequest = {}
): Promise<CursorPaginationResponse<Post>> {
try {
const response = await api.get<CursorPaginationResponse<Post>>('/posts/cursor', {
params: {
cursor: params.cursor,
page_size: params.page_size,
post_type: params.post_type,
},
});
return response.data;
} catch (error) {
console.error('获取帖子列表失败:', error);
return {
items: [],
next_cursor: null,
prev_cursor: null,
has_more: false,
};
}
}
/**
* 搜索帖子(游标分页)
* GET /api/v1/posts/search/cursor
* @param query 搜索关键词
* @param params 游标分页请求参数
*/
async searchPostsCursor(
query: string,
params: CursorPaginationRequest = {}
): Promise<CursorPaginationResponse<Post>> {
try {
const response = await api.get<CursorPaginationResponse<Post>>('/posts/search/cursor', {
params: {
...params,
keyword: query,
},
});
return response.data;
} catch (error) {
console.error('搜索帖子失败:', error);
return {
items: [],
next_cursor: null,
prev_cursor: null,
has_more: false,
};
}
}
/**
* 获取用户帖子列表(游标分页)
* GET /api/v1/users/:id/posts/cursor
* @param userId 用户ID
* @param params 游标分页请求参数
*/
async getUserPostsCursor(
userId: string,
params: CursorPaginationRequest = {}
): Promise<CursorPaginationResponse<Post>> {
try {
const response = await api.get<CursorPaginationResponse<Post>>(
`/users/${userId}/posts/cursor`,
{ params }
);
return response.data;
} catch (error) {
console.error('获取用户帖子列表失败:', error);
return {
items: [],
next_cursor: null,
prev_cursor: null,
has_more: false,
};
}
}
}
// 导出帖子服务实例