feat: 翻页bugfix

This commit is contained in:
zdl
2025-11-05 19:28:17 +08:00
parent 612b58c983
commit 25a6ff164b

View File

@@ -193,22 +193,7 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t
cachedCount cachedCount
}); });
// 特殊处理:返回第一页 - 清空缓存重新加载 // 检查目标页缓存状态(统一处理,包括第一页)
if (newPage === 1) {
logger.debug('DynamicNewsCard', '返回第一页,清空缓存重新加载');
setCurrentPage(1);
dispatch(fetchDynamicNews({
mode: mode, // 传递 mode 参数
per_page: pageSize,
pageSize: pageSize,
clearCache: true, // 清空缓存
...filters, // 先展开筛选条件
page: 1, // 然后覆盖 page 参数
}));
return;
}
// 检查目标页缓存状态
const { isTargetPageCached, targetPageInfo } = checkTargetPageCache(newPage); const { isTargetPageCached, targetPageInfo } = checkTargetPageCache(newPage);
console.log(`%c🟡 [缓存检查] 目标页${newPage}缓存状态`, 'color: #EAB308; font-weight: bold;'); console.log(`%c🟡 [缓存检查] 目标页${newPage}缓存状态`, 'color: #EAB308; font-weight: bold;');
@@ -275,18 +260,23 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t
// 加载下一页(用于无限滚动) // 加载下一页(用于无限滚动)
const loadNextPage = useCallback(async () => { const loadNextPage = useCallback(async () => {
if (currentPage >= totalPages || loadingPage !== null) { // 修复:使用 hasMore 判断而不是 currentPage >= totalPages
// 原因:去重后 cachedCount 可能小于 total但 currentPage 已达到 totalPages
if (!hasMore || loadingPage !== null) {
logger.debug('DynamicNewsCard', '无法加载下一页', { logger.debug('DynamicNewsCard', '无法加载下一页', {
currentPage, currentPage,
totalPages, totalPages,
hasMore,
cachedCount,
total,
loadingPage, loadingPage,
reason: currentPage >= totalPages ? '已是最后一页' : '正在加载中' reason: !hasMore ? '已加载全部数据 (cachedCount >= total)' : '正在加载中'
}); });
return Promise.resolve(false); // 没有更多数据或正在加载 return Promise.resolve(false); // 没有更多数据或正在加载
} }
const nextPage = currentPage + 1; const nextPage = currentPage + 1;
logger.debug('DynamicNewsCard', '懒加载:加载下一页', { currentPage, nextPage }); logger.debug('DynamicNewsCard', '懒加载:加载下一页', { currentPage, nextPage, hasMore, cachedCount, total });
try { try {
await handlePageChange(nextPage); await handlePageChange(nextPage);
@@ -295,7 +285,7 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t
logger.error('DynamicNewsCard', '懒加载失败', error, { nextPage }); logger.error('DynamicNewsCard', '懒加载失败', error, { nextPage });
return false; return false;
} }
}, [currentPage, totalPages, loadingPage, handlePageChange]); }, [currentPage, totalPages, hasMore, cachedCount, total, loadingPage, handlePageChange]);
// 加载上一页(用于双向无限滚动) // 加载上一页(用于双向无限滚动)
const loadPrevPage = useCallback(async () => { const loadPrevPage = useCallback(async () => {
@@ -326,17 +316,7 @@ export const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, t
setMode(newMode); setMode(newMode);
setCurrentPage(PAGINATION_CONFIG.INITIAL_PAGE); setCurrentPage(PAGINATION_CONFIG.INITIAL_PAGE);
// pageSize 会根据 mode 自动重新计算第35-44行
const newPageSize = (() => {
switch (newMode) {
case DISPLAY_MODES.FOUR_ROW:
return PAGINATION_CONFIG.FOUR_ROW_PAGE_SIZE;
case DISPLAY_MODES.VERTICAL:
return PAGINATION_CONFIG.VERTICAL_PAGE_SIZE;
default:
return PAGINATION_CONFIG.VERTICAL_PAGE_SIZE;
}
})();
}, [mode]); }, [mode]);
return { return {