From df143a2805a8c3aba7c7a6431b428b59735ccdcb Mon Sep 17 00:00:00 2001 From: andresgutgon Date: Fri, 27 Sep 2024 19:32:12 +0200 Subject: [PATCH] feat: wip --- .../DocumentLogs/DocumentLogsTable.tsx | 12 +++++++++ .../logs/_components/DocumentLogs/index.tsx | 3 +-- .../documents/[documentUuid]/logs/page.tsx | 22 +++++---------- packages/core/src/lib/pagination/paginate.ts | 27 +++++++------------ 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx index fe180536d..d3b242a7a 100644 --- a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx +++ b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx @@ -5,6 +5,7 @@ import { Table, TableBody, TableCell, + TableFooter, TableHead, TableHeader, TableRow, @@ -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 ( @@ -94,6 +99,13 @@ export const DocumentLogsTable = ({ ))} + + + + + + +
) } diff --git a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/index.tsx b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/index.tsx index 77d368a43..ad7737fc5 100644 --- a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/index.tsx +++ b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/index.tsx @@ -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' @@ -31,8 +30,8 @@ export function DocumentLogs({ documentLogs={documentLogs} selectedLog={selectedLog} setSelectedLog={setSelectedLog} + pagination={pagination} /> - {selectedLog && (
diff --git a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/page.tsx b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/page.tsx index 8abfa4824..8d086b704 100644 --- a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/page.tsx +++ b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/page.tsx @@ -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 - - const title = `${pagination.count} logs (page ${pagination.currentPage} of ${pagination.totalPages})` - return (
{!rows.length && ( diff --git a/packages/core/src/lib/pagination/paginate.ts b/packages/core/src/lib/pagination/paginate.ts index 175b2e877..def7c80ac 100644 --- a/packages/core/src/lib/pagination/paginate.ts +++ b/packages/core/src/lib/pagination/paginate.ts @@ -32,19 +32,19 @@ function queryParamsWithDefaults({ * https://orm.drizzle.team/docs/dynamic-query-building */ async function paginateQuerySql({ - 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`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 } } @@ -52,31 +52,24 @@ async function paginateQuerySql({ /** * TODO: Add tests */ -export async function paginateQuery< - TTableName extends string | undefined, - TSelection extends ColumnsSelection, ->({ - query, +export async function paginateQuery({ + dynamicQuery, pageUrl, searchParams, defaultPaginate, }: { + dynamicQuery: T pageUrl?: { base?: string; queryParams?: Record } - query: Omit< - PgSelectBase, - 'where' | 'orderBy' | 'limit' | 'offset' - > searchParams?: ISearchParamsObject defaultPaginate?: Exclude }) { 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, pagination } + return { rows, pagination } }