-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paginate document logs table to improve page performance
- Loading branch information
1 parent
5eacfe1
commit 7798b60
Showing
16 changed files
with
706 additions
and
429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 33 additions & 10 deletions
43
...rivate)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { IPagination } from '@latitude-data/core/lib/buildPagination' | ||
import { | ||
Pagination, | ||
PaginationContent, | ||
PaginationEllipsis, | ||
PaginationItem, | ||
PaginationLink, | ||
PaginationNext, | ||
PaginationPrevious, | ||
} from '@latitude-data/web-ui' | ||
import Link from 'next/link' | ||
|
||
export default function WebPagination({ | ||
currentPage, | ||
prevPage, | ||
pageItems, | ||
nextPage, | ||
}: IPagination) { | ||
return ( | ||
<Pagination aria-label='Pagination'> | ||
<PaginationContent> | ||
{prevPage && ( | ||
<Link href={prevPage.url}> | ||
<PaginationPrevious /> | ||
</Link> | ||
)} | ||
{pageItems.map((item, idx) => { | ||
return ( | ||
<PaginationItem key={idx}> | ||
{item.type === 'page' ? ( | ||
<Link href={item.url}> | ||
<PaginationLink isActive={item.value === currentPage}> | ||
{item.value} | ||
</PaginationLink> | ||
</Link> | ||
) : ( | ||
<PaginationEllipsis /> | ||
)} | ||
</PaginationItem> | ||
) | ||
})} | ||
{nextPage && ( | ||
<Link href={nextPage.url}> | ||
<PaginationNext /> | ||
</Link> | ||
)} | ||
</PaginationContent> | ||
</Pagination> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { isNumber } from 'lodash-es' | ||
|
||
type ItemType = 'page' | 'ellipsis' | ||
type PageItem<T extends ItemType> = T extends 'page' | ||
? { type: T; value: number; url: string } | ||
: { type: T; value: '...' } | ||
export type IPagination = { | ||
count: number | ||
totalPages: number | ||
currentPage: number | ||
prevPage?: PageItem<'page'> | ||
pageItems: (PageItem<'page'> | PageItem<'ellipsis'>)[] | ||
nextPage?: PageItem<'page'> | ||
} | ||
|
||
function buildUrl({ | ||
baseUrl, | ||
page, | ||
limit, | ||
}: { | ||
baseUrl: string | ||
page: number | ||
limit: number | ||
}) { | ||
return `${baseUrl}?page=${page}&limit=${limit}` | ||
} | ||
|
||
export function buildPagination({ | ||
baseUrl, | ||
count, | ||
page, | ||
pageSize, | ||
pageItemsCount = 10, | ||
}: { | ||
baseUrl: string | ||
count: number | ||
page: number | ||
pageSize: number | ||
pageItemsCount?: number | ||
}) { | ||
const totalPages = Math.ceil(count / pageSize) | ||
const pagination: IPagination = { | ||
count, | ||
totalPages, | ||
currentPage: page, | ||
prevPage: | ||
page > 1 | ||
? { | ||
type: 'page', | ||
value: page - 1, | ||
url: buildUrl({ baseUrl, page: page - 1, limit: pageSize }), | ||
} | ||
: undefined, | ||
pageItems: [], | ||
nextPage: | ||
page < totalPages | ||
? { | ||
type: 'page', | ||
value: page + 1, | ||
url: buildUrl({ baseUrl, page: page + 1, limit: pageSize }), | ||
} | ||
: undefined, | ||
} | ||
|
||
for (let i = page - 1; i >= 1 && pagination.pageItems.length < 2; i--) { | ||
pagination.pageItems.unshift({ | ||
type: 'page', | ||
url: buildUrl({ baseUrl, page: i, limit: pageSize }), | ||
value: i, | ||
}) | ||
} | ||
|
||
const prevItem = pagination.pageItems[0] | ||
const prevPage = isNumber(prevItem?.value) ? prevItem.value : undefined | ||
if (pagination.pageItems.length > 0 && prevPage && prevPage < page - 2) { | ||
pagination.pageItems.push({ type: 'ellipsis', value: '...' }) | ||
} | ||
|
||
pagination.pageItems.push({ | ||
type: 'page', | ||
value: page, | ||
url: buildUrl({ baseUrl, page, limit: pageSize }), | ||
}) | ||
|
||
for ( | ||
let i = page + 1; | ||
i <= totalPages && pagination.pageItems.length < pageItemsCount; | ||
i++ | ||
) { | ||
pagination.pageItems.push({ | ||
type: 'page', | ||
value: i, | ||
url: buildUrl({ baseUrl, page: i, limit: pageSize }), | ||
}) | ||
} | ||
|
||
const nextItem = pagination.pageItems[pagination.pageItems.length - 1] | ||
const nextPage = isNumber(nextItem?.value) ? nextItem.value : undefined | ||
if (pagination.pageItems.length > 0 && nextPage && nextPage > page + 1) { | ||
pagination.pageItems.push({ type: 'ellipsis', value: '...' }) | ||
} | ||
|
||
return pagination | ||
} | ||
|
||
export function parsePage( | ||
page: string | string[] | number | undefined, | ||
): number { | ||
return typeof page === 'string' ? Number(page) : 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.