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

@@ -19,6 +19,16 @@ export type {
FetchPageFunction,
} from './types';
// 导出游标分页相关类型
export type {
CursorDirection,
CursorPaginationConfig,
CursorPaginationState,
CursorPaginationActions,
UseCursorPaginationReturn,
CursorFetchFunction,
} from './types';
// 导出工具函数和常量
export {
DEFAULT_PAGINATION_CONFIG,

View File

@@ -180,3 +180,79 @@ export function createPageCache<T>(
export function isCacheExpired<T>(cache: PageCache<T>, ttl: number): boolean {
return Date.now() - cache.cachedAt > ttl;
}
// ==================== 游标分页相关类型 ====================
/**
* 游标分页方向
*/
export type CursorDirection = 'forward' | 'backward';
/**
* 游标分页配置
*/
export interface CursorPaginationConfig {
/** 每页数量 */
pageSize: number;
/** 是否启用双向分页 */
bidirectional?: boolean;
}
/**
* 游标分页状态
*/
export interface CursorPaginationState<T> {
/** 数据项列表 */
items: T[];
/** 下一页游标 */
nextCursor: string | null;
/** 上一页游标 */
prevCursor: string | null;
/** 是否有更多数据 */
hasMore: boolean;
/** 是否正在加载 */
isLoading: boolean;
/** 是否正在刷新 */
isRefreshing: boolean;
/** 错误信息 */
error: string | null;
/** 是否为首次加载 */
isFirstLoad: boolean;
}
/**
* 游标分页操作
*/
export interface CursorPaginationActions<T> {
/** 加载更多(下一页) */
loadMore: () => Promise<void>;
/** 加载上一页(双向分页) */
loadPrevious: () => Promise<void>;
/** 刷新数据(重新从第一页加载) */
refresh: () => Promise<void>;
/** 重置状态 */
reset: () => void;
/** 设置数据(用于外部数据注入) */
setItems: (items: T[], nextCursor: string | null, prevCursor: string | null, hasMore: boolean) => void;
}
/**
* 游标分页 Hook 返回值
*/
export interface UseCursorPaginationReturn<T> extends CursorPaginationState<T>, CursorPaginationActions<T> {}
/**
* 游标分页数据获取函数
*/
export type CursorFetchFunction<T, P = void> = (params: {
cursor?: string;
direction: CursorDirection;
pageSize: number;
/** 额外参数 */
extraParams?: P;
}) => Promise<{
items: T[];
next_cursor: string | null;
prev_cursor: string | null;
has_more: boolean;
}>;