-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Feat: pagination #7653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feat: pagination #7653
Changes from all commits
c39833f
319fd6c
1d19cf5
322b86d
c97a457
437d9b8
27b22dd
dba88f6
76c33d1
297a9a9
d48ed1f
3bb4124
a029291
6f750b0
2876d04
8b02688
52444da
447d5c6
7d01d75
79b17d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -80,23 +80,24 @@ function deleteFile(path: string, tree: RepoTree) { | |||||
| unset(tree, path.split('/')); | ||||||
| } | ||||||
|
|
||||||
| const pageSize = 10; | ||||||
| const DEFAULT_PAGE_SIZE = 10; | ||||||
|
|
||||||
| function getCursor( | ||||||
| folder: string, | ||||||
| extension: string, | ||||||
| entries: ImplementationEntry[], | ||||||
| index: number, | ||||||
| depth: number, | ||||||
| pageSize = DEFAULT_PAGE_SIZE, | ||||||
| ) { | ||||||
| const count = entries.length; | ||||||
| const pageCount = Math.floor(count / pageSize); | ||||||
| const pageCount = Math.ceil(count / pageSize); | ||||||
| return Cursor.create({ | ||||||
| actions: [ | ||||||
| ...(index < pageCount ? ['next', 'last'] : []), | ||||||
| ...(index > 0 ? ['prev', 'first'] : []), | ||||||
| ], | ||||||
| meta: { index, count, pageSize, pageCount }, | ||||||
| meta: { index, page: index + 1, count, pageSize, pageCount }, | ||||||
| data: { folder, extension, index, pageCount, depth }, | ||||||
| }); | ||||||
| } | ||||||
|
|
@@ -173,6 +174,9 @@ export default class TestBackend implements Implementation { | |||||
| pageCount: number; | ||||||
| depth: number; | ||||||
| }; | ||||||
| const meta = cursor.meta!; | ||||||
| const currentPageSize = meta.get('pageSize', DEFAULT_PAGE_SIZE); | ||||||
|
|
||||||
| const newIndex = (() => { | ||||||
| if (action === 'next') { | ||||||
| return (index as number) + 1; | ||||||
|
|
@@ -194,19 +198,38 @@ export default class TestBackend implements Implementation { | |||||
| data: f.content as string, | ||||||
| file: { path: f.path, id: f.path }, | ||||||
| })); | ||||||
| const entries = allEntries.slice(newIndex * pageSize, newIndex * pageSize + pageSize); | ||||||
| const newCursor = getCursor(folder, extension, allEntries, newIndex, depth); | ||||||
| const entries = allEntries.slice( | ||||||
| newIndex * currentPageSize, | ||||||
| newIndex * currentPageSize + currentPageSize, | ||||||
| ); | ||||||
| const newCursor = getCursor(folder, extension, allEntries, newIndex, depth, currentPageSize); | ||||||
| return Promise.resolve({ entries, cursor: newCursor }); | ||||||
| } | ||||||
|
|
||||||
| entriesByFolder(folder: string, extension: string, depth: number) { | ||||||
| entriesByFolder( | ||||||
| folder: string, | ||||||
| extension: string, | ||||||
| depth: number, | ||||||
| options?: { | ||||||
| page?: number; | ||||||
| pageSize?: number; | ||||||
| pagination?: boolean; | ||||||
| }, | ||||||
| ) { | ||||||
| const files = folder ? getFolderFiles(window.repoFiles, folder, extension, depth) : []; | ||||||
| const entries = files.map(f => ({ | ||||||
| data: f.content as string, | ||||||
| file: { path: f.path, id: f.path }, | ||||||
| })); | ||||||
| const cursor = getCursor(folder, extension, entries, 0, depth); | ||||||
| const ret = take(entries, pageSize); | ||||||
| const pageSize = options?.pageSize ?? DEFAULT_PAGE_SIZE; | ||||||
| const page = options?.page ?? 1; | ||||||
| const usePagination = options?.pagination ?? true; | ||||||
|
|
||||||
| const cursor = getCursor(folder, extension, entries, page - 1, depth, pageSize); | ||||||
|
|
||||||
| // If pagination is enabled, return only the requested page | ||||||
| // Otherwise, return all entries (for backward compatibility) | ||||||
| const ret = usePagination ? take(entries.slice((page - 1) * pageSize), pageSize) : entries; | ||||||
|
||||||
| const ret = usePagination ? take(entries.slice((page - 1) * pageSize), pageSize) : entries; | |
| const ret = usePagination ? entries.slice((page - 1) * pageSize, page * pageSize) : entries; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Division by zero is possible if pageSize is 0. Although the default is 10, the function accepts a pageSize parameter that could be 0 or negative. Add validation:
if (pageSize <= 0) throw new Error('pageSize must be positive');at the start of the getCursor function.