Skip to content

Commit

Permalink
fix(pagination): fix incorrect pagination after refreshing (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanfei authored Jan 10, 2025
1 parent ad9a8bf commit ea3928a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 25 additions & 8 deletions src/hooks/usePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
State,
getPagination,
objStore,
recordScroll,
recoverScroll,
me,
} from "~/store"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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),
}
}
2 changes: 1 addition & 1 deletion src/store/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { log } from "~/utils"

export const ScrollMap = new Map<string, number>()

export const recordScroll = (path: string, scroll: number) => {
export const recordScroll = (path: string, scroll: number = window.scrollY) => {
ScrollMap.set(path, scroll)
log("recordScroll", path, scroll)
}
Expand Down

0 comments on commit ea3928a

Please sign in to comment.