Skip to content
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

fix: Uncaught Error: Minified React error #215

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/web/src/app/(private)/_data-access/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const getFirstProjectCached = cache(
export const getActiveProjectsCached = cache(
async ({ workspaceId }: { workspaceId: number }) => {
const projectsScope = new ProjectsRepository(workspaceId)
const result = await projectsScope.findAllActive()
const result =
await projectsScope.findAllActiveDocumentsWithAgreggatedData()
const projects = result.unwrap()

return projects
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { DocumentVersion, Project } from '@latitude-data/core/browser'
import { Project } from '@latitude-data/core/browser'
import {
Icon,
Table,
Expand All @@ -16,18 +16,16 @@ import { relativeTime } from '$/lib/relativeTime'
import { ROUTES } from '$/services/routes'
import Link from 'next/link'

import { getDocumentsFromMergedCommitsCache } from '../../_data-access'

type ProjectWithAgreggatedData = Project & {
documentCount: number
lastCreatedAtDocument: Date | null
}
export function ProjectsTable({
documents,
projects,
}: {
documents: Awaited<ReturnType<typeof getDocumentsFromMergedCommitsCache>>
projects: Project[]
projects: ProjectWithAgreggatedData[]
}) {
const navigate = useNavigate()
const findDocuments = (projectId: number) =>
documents.filter((d) => d.projectId === projectId)
return (
<Table>
<TableHeader>
Expand All @@ -54,17 +52,12 @@ export function ProjectsTable({
</TableCell>
<TableCell>
<Text.H4 color='foregroundMuted'>
{findDocuments(project.id).length || '-'}
{project.documentCount || '-'}
</Text.H4>
</TableCell>
<TableCell>
<Text.H4 color='foregroundMuted'>
{relativeTime(
findDocuments(project.id).sort(
(a: DocumentVersion, b: DocumentVersion) =>
b.createdAt.getTime() - a.createdAt.getTime(),
)?.[0]?.createdAt,
)}
{relativeTime(project.lastCreatedAtDocument)}
</Text.H4>
</TableCell>
<TableCell>
Expand Down
10 changes: 2 additions & 8 deletions apps/web/src/app/(private)/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import { ROUTES } from '$/services/routes'
import Link from 'next/link'
import { redirect } from 'next/navigation'

import {
getActiveProjectsCached,
getDocumentsFromMergedCommitsCache,
} from '../_data-access'
import { getActiveProjectsCached } from '../_data-access'
import { NAV_LINKS } from '../_lib/constants'
import { ProjectsTable } from './_components/ProjectsTable'

Expand All @@ -31,7 +28,6 @@ export default async function DashboardLayout({

const { workspace, user } = await getCurrentUser()
const projects = await getActiveProjectsCached({ workspaceId: workspace.id })
const documents = await getDocumentsFromMergedCommitsCache(workspace.id)
const breadcrumbs = [
{
name: <Text.H5M>{workspace.name}</Text.H5M>,
Expand Down Expand Up @@ -59,9 +55,7 @@ export default async function DashboardLayout({
}
table={
<>
{projects.length > 0 && (
<ProjectsTable documents={documents} projects={projects} />
)}
{projects.length > 0 && <ProjectsTable projects={projects} />}
{projects.length === 0 && (
<TableBlankSlate
description='There are no projects yet. Create one to start adding your prompts.'
Expand Down
60 changes: 58 additions & 2 deletions packages/core/src/repositories/projectsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { eq, getTableColumns, isNull } from 'drizzle-orm'
import {
and,
count,
eq,
getTableColumns,
isNotNull,
isNull,
max,
} from 'drizzle-orm'

import { Project } from '../browser'
import { NotFoundError, Result } from '../lib'
import { projects } from '../schema'
import { commits, documentVersions, projects } from '../schema'
import Repository from './repository'

const NOT_FOUND_MSG = 'Project not found'
Expand Down Expand Up @@ -48,4 +56,52 @@ export class ProjectsRepository extends Repository<typeof tt, Project> {

return Result.ok(result)
}

async findAllActiveDocumentsWithAgreggatedData() {
const lastMergedCommit = this.db.$with('lastMergedCommit').as(
this.db
.select({
projectId: commits.projectId,
maxVersion: max(commits.version).as('maxVersion'),
})
.from(commits)
.where(isNotNull(commits.mergedAt))
.groupBy(commits.projectId),
)
const aggredatedData = this.db.$with('aggredatedData').as(
this.db
.with(lastMergedCommit)
.select({
id: this.scope.id,
documentCount: count(documentVersions.id).as('documentCount'),
lastCreatedAtDocument: max(documentVersions.createdAt).as(
'lastCreatedAtDocument',
),
})
.from(this.scope)
.innerJoin(commits, eq(commits.projectId, this.scope.id))
.innerJoin(
lastMergedCommit,
and(
eq(lastMergedCommit.projectId, this.scope.id),
eq(commits.version, lastMergedCommit.maxVersion),
),
)
.innerJoin(documentVersions, eq(documentVersions.commitId, commits.id))
.where(isNull(this.scope.deletedAt))
.groupBy(this.scope.id),
)

const result = await this.db
.with(aggredatedData)
.select({
...this.scope._.selectedFields,
documentCount: aggredatedData.documentCount,
lastCreatedAtDocument: aggredatedData.lastCreatedAtDocument,
})
andresgutgon marked this conversation as resolved.
Show resolved Hide resolved
.from(this.scope)
.innerJoin(aggredatedData, eq(aggredatedData.id, this.scope.id))

return Result.ok(result)
}
}
Loading