From ea3928a282f6d938ed75d87a1dd5a42ff4a7d535 Mon Sep 17 00:00:00 2001 From: Jealous Date: Fri, 10 Jan 2025 20:44:54 +0800 Subject: [PATCH] fix(pagination): fix incorrect pagination after refreshing (#224) --- src/hooks/usePath.ts | 33 +++++++++++++++++++++++++-------- src/store/scroll.ts | 2 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/hooks/usePath.ts b/src/hooks/usePath.ts index 0d0a276738..2f896ffd6a 100644 --- a/src/hooks/usePath.ts +++ b/src/hooks/usePath.ts @@ -6,6 +6,7 @@ import { State, getPagination, objStore, + recordScroll, recoverScroll, me, } from "~/store" @@ -141,9 +142,9 @@ export const usePath = () => { retry_pass = rp ?? false handleErr("") if (IsDirRecord[path]) { - handleFolder(path, globalPage, undefined, undefined, force) + return handleFolder(path, globalPage, undefined, undefined, force) } else { - handleObj(path) + return handleObj(path) } } @@ -228,18 +229,34 @@ export const usePath = () => { } } const pageChange = (index?: number, size?: number, append = false) => { - handleFolder(pathname(), index, size, append) + return handleFolder(pathname(), index, size, append) + } + const loadMore = () => { + return pageChange(globalPage + 1, undefined, true) } return { handlePathChange: handlePathChange, setPathAs: setPathAs, - refresh: (retry_pass?: boolean, force?: boolean) => { - handlePathChange(pathname(), retry_pass, force) + refresh: async (retry_pass?: boolean, force?: boolean) => { + const path = pathname() + recordScroll(path) + if ( + pagination.type === "load_more" || + pagination.type === "auto_load_more" + ) { + const page = globalPage + resetGlobalPage() + await handlePathChange(path, retry_pass, force) + while (globalPage < page) { + await loadMore() + } + } else { + await handlePathChange(path, retry_pass, force) + } + recoverScroll(path) }, pageChange: pageChange, - loadMore: () => { - pageChange(globalPage + 1, undefined, true) - }, + loadMore: loadMore, allLoaded: () => globalPage >= Math.ceil(objStore.total / pagination.size), } } diff --git a/src/store/scroll.ts b/src/store/scroll.ts index e5c9540788..177dad5703 100644 --- a/src/store/scroll.ts +++ b/src/store/scroll.ts @@ -2,7 +2,7 @@ import { log } from "~/utils" export const ScrollMap = new Map() -export const recordScroll = (path: string, scroll: number) => { +export const recordScroll = (path: string, scroll: number = window.scrollY) => { ScrollMap.set(path, scroll) log("recordScroll", path, scroll) }