Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Sep 27, 2024
1 parent 1a85bbc commit df143a2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Table,
TableBody,
TableCell,
TableFooter,
TableHead,
TableHeader,
TableRow,
Expand All @@ -15,15 +16,19 @@ import {
formatDuration,
relativeTime,
} from '$/app/_lib/formatUtils'
import { IPagination } from '@latitude-data/core/lib/pagination/buildPagination'
import WebPagination from '$/components/WebPagination'

export const DocumentLogsTable = ({
documentLogs,
selectedLog,
setSelectedLog,
pagination,
}: {
documentLogs: DocumentLogWithMetadata[]
selectedLog: DocumentLogWithMetadata | undefined
setSelectedLog: (log: DocumentLogWithMetadata | undefined) => void
pagination: IPagination
}) => {
return (
<Table className='table-auto'>
Expand Down Expand Up @@ -94,6 +99,13 @@ export const DocumentLogsTable = ({
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow >
<TableCell colSpan={7}>
<WebPagination {...pagination} />
</TableCell>
</TableRow>
</TableFooter>
</Table>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useState } from 'react'

import { IPagination } from '@latitude-data/core/lib/pagination/buildPagination'
import { DocumentLogWithMetadata } from '@latitude-data/core/repositories'
import WebPagination from '$/components/WebPagination'
import useProviderLogs from '$/stores/providerLogs'

import { DocumentLogInfo } from './DocumentLogInfo'
Expand All @@ -31,8 +30,8 @@ export function DocumentLogs({
documentLogs={documentLogs}
selectedLog={selectedLog}
setSelectedLog={setSelectedLog}
pagination={pagination}
/>
<WebPagination {...pagination} />
</div>
{selectedLog && (
<div className='w-80 flex-shrink-0 flex flex-col border border-border rounded-lg px-4 pt-6 items-center'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,19 @@ export default async function DocumentPage({
const projectId = Number(params.projectId)
const commitUuid = params.commitUuid
const commit = await findCommitCached({ projectId, uuid: commitUuid })

const query = computeDocumentLogsWithMetadataQuery({
workspaceId: workspace.id,
documentUuid: params.documentUuid,
draft: commit,
})
const { rows: originalRows, pagination } = await paginateQuery({
const { rows, pagination } = await paginateQuery({
searchParams,
pageUrl: { base: pageUrl(params) },
query,
dynamicQuery: computeDocumentLogsWithMetadataQuery({
workspaceId: workspace.id,
documentUuid: params.documentUuid,
draft: commit,
}).$dynamic(),
})
// FIXME: The inference is not ok. Find a solution.
// ideally rows should be the same type as `const result = await query`
const rows = originalRows as Awaited<typeof query>

const title = `${pagination.count} logs (page ${pagination.currentPage} of ${pagination.totalPages})`

return (
<div className='flex flex-col w-full h-full overflow-hidden p-6 gap-2 min-w-0'>
<TableWithHeader
title={title}
title='Logs'
table={
<>
{!rows.length && (
Expand Down
27 changes: 10 additions & 17 deletions packages/core/src/lib/pagination/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,44 @@ function queryParamsWithDefaults({
* https://orm.drizzle.team/docs/dynamic-query-building
*/
async function paginateQuerySql<T extends PgSelect>({
query,
dynamicQuery,
page = 1,
pageSize = 20,
}: {
query: T
dynamicQuery: T
} & PaginationArgs) {
// @ts-ignore
query.config.fields = {
dynamicQuery.config.fields = {
// @ts-ignore
...query.config.fields,
...dynamicQuery.config.fields,
__count: sql<number>`count(*) over()`,
}
const rows = await query.limit(pageSize).offset((page - 1) * pageSize)
const rows = await dynamicQuery.limit(pageSize).offset((page - 1) * pageSize)
const count = rows[0]?.__count ? Number(rows[0]?.__count) : 0
return { rows, count }
}

/**
* TODO: Add tests
*/
export async function paginateQuery<
TTableName extends string | undefined,
TSelection extends ColumnsSelection,
>({
query,
export async function paginateQuery<T extends PgSelect>({
dynamicQuery,
pageUrl,
searchParams,
defaultPaginate,
}: {
dynamicQuery: T
pageUrl?: { base?: string; queryParams?: Record<string, unknown> }
query: Omit<
PgSelectBase<TTableName, TSelection, SelectMode, any, false, string>,
'where' | 'orderBy' | 'limit' | 'offset'
>
searchParams?: ISearchParamsObject
defaultPaginate?: Exclude<PaginationArgs, 'page'>
}) {
const params = queryParamsWithDefaults({ searchParams, defaultPaginate })
const dynamic = query.$dynamic()
const { rows, count } = await paginateQuerySql({ query: dynamic, ...params })
const { rows, count } = await paginateQuerySql({ dynamicQuery, ...params })
const pagination = buildPagination({
baseUrl: pageUrl?.base ?? '',
count,
...params,
})

return { rows: rows as Awaited<typeof query>, pagination }
return { rows, pagination }
}

0 comments on commit df143a2

Please sign in to comment.