Skip to content

Commit

Permalink
Save documents in DB from sidebar and make sidebar resizable
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Jul 27, 2024
1 parent dda49f1 commit a3be7fa
Show file tree
Hide file tree
Showing 36 changed files with 1,099 additions and 369 deletions.
35 changes: 35 additions & 0 deletions apps/web/src/actions/documents/destroyDocumentAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use server'

import {
CommitsRepository,
destroyDocument,
DocumentVersionsRepository,
} from '@latitude-data/core'
import { z } from 'zod'

import { withProject } from '../procedures'

export const destroyDocumentAction = withProject
.createServerAction()
.input(z.object({ documentUuid: z.string(), commitId: z.number() }), {
type: 'json',
})
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.then((r) => r.unwrap())
const docsScope = new DocumentVersionsRepository(ctx.project.workspaceId)
const document = await docsScope
.getDocumentByUuid({
commit,
documentUuid: input.documentUuid,
})
.then((r) => r.unwrap())
const result = await destroyDocument({
document,
commit,
workspaceId: ctx.project.workspaceId,
})
return result.unwrap()
})
24 changes: 24 additions & 0 deletions apps/web/src/actions/documents/getDocumentsAtCommitAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use server'

import {
CommitsRepository,
DocumentVersionsRepository,
} from '@latitude-data/core'
import { z } from 'zod'

import { withProject } from '../procedures'

export const getDocumentsAtCommitAction = withProject
.createServerAction()
.input(z.object({ commitId: z.number() }))
.handler(async ({ input, ctx }) => {
const commit = await new CommitsRepository(ctx.project.workspaceId)
.getCommitById(input.commitId)
.then((r) => r.unwrap())
const docsScope = new DocumentVersionsRepository(ctx.project.workspaceId)
const result = await docsScope.getDocumentsAtCommit({
commit,
})

return result.unwrap()
})
18 changes: 13 additions & 5 deletions apps/web/src/app/(private)/_data-access/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
Commit,
CommitsRepository,
DocumentVersionsRepository,
findWorkspaceFromCommit,
NotFoundError,
Project,
ProjectsRepository,
} from '@latitude-data/core'
import { getCurrentUser } from '$/services/auth/getCurrentUser'

export const getFirstProject = cache(
async ({ workspaceId }: { workspaceId: number }) => {
Expand Down Expand Up @@ -54,20 +54,28 @@ export const getDocumentByUuid = cache(
documentUuid: string
commit: Commit
}) => {
const workspace = await findWorkspaceFromCommit(commit)
const scope = new DocumentVersionsRepository(workspace!.id)
const { workspace } = await getCurrentUser()
const scope = new DocumentVersionsRepository(workspace.id)
const result = await scope.getDocumentAtCommit({ documentUuid, commit })
if (result.error) {
const error = result.error
if (error instanceof NotFoundError) {
return null
}

throw error
}

return result.unwrap()
},
)

export const getDocumentByPath = cache(
async ({ commit, path }: { commit: Commit; path: string }) => {
const workspace = await findWorkspaceFromCommit(commit)
const { workspace } = await getCurrentUser()
const docsScope = new DocumentVersionsRepository(workspace!.id)
const documents = await docsScope
.getDocumentsAtCommit(commit)
.getDocumentsAtCommit({ commit })
.then((r) => r.unwrap())

const document = documents.find((d) => d.path === path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ReactNode } from 'react'

import { DocumentVersion } from '@latitude-data/core'
import { DocumentDetailWrapper } from '@latitude-data/web-ui'
import { findCommit, findProject } from '$/app/(private)/_data-access'
import { getCurrentUser } from '$/services/auth/getCurrentUser'

import Sidebar from '../Sidebar'

export default async function DocumentsLayout({
children,
document,
commitUuid,
projectId,
}: {
children: ReactNode
document?: DocumentVersion
projectId: number
commitUuid: string
}) {
const session = await getCurrentUser()
const project = await findProject({
projectId,
workspaceId: session.workspace.id,
})
const commit = await findCommit({
project,
uuid: commitUuid,
})
return (
<DocumentDetailWrapper>
<Sidebar commit={commit} documentPath={document?.path} />
{children}
</DocumentDetailWrapper>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
type SidebarDocument,
} from '@latitude-data/web-ui'
import { ROUTES } from '$/services/routes'
import useDocumentVersions from '$/stores/documentVersions'
import { useRouter } from 'next/navigation'

export default function ClientFilesTree({
documents,
documents: serverDocuments,
documentPath,
}: {
documents: SidebarDocument[]
documentPath: string | undefined
documentUuid: string | undefined
}) {
const router = useRouter()
const { commit, isHead } = useCurrentCommit()
Expand All @@ -32,12 +32,17 @@ export default function ClientFilesTree({
.documents.detail({ uuid: documentUuid }).root,
)
}, [])
const { createFile, destroyFile, documents } = useDocumentVersions({
fallbackData: serverDocuments,
})

return (
<FilesTree
documents={documents}
currentPath={documentPath}
navigateToDocument={navigateToDocument}
createFile={createFile}
destroyFile={destroyFile}
/>
)
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import { Suspense } from 'react'

import {
Commit,
DocumentVersionsRepository,
findWorkspaceFromCommit,
} from '@latitude-data/core'
import { Commit, DocumentVersionsRepository } from '@latitude-data/core'
import { DocumentSidebar } from '@latitude-data/web-ui'
import { getCurrentUser } from '$/services/auth/getCurrentUser'

import ClientFilesTree from './ClientFilesTree'

export default async function Sidebar({
commit,
documentUuid,
documentPath,
}: {
commit: Commit
documentPath?: string
documentUuid?: string
}) {
const workspace = await findWorkspaceFromCommit(commit)
const docsScope = new DocumentVersionsRepository(workspace!.id)
const documents = await docsScope.getDocumentsAtCommit(commit)
const { workspace } = await getCurrentUser()
const docsScope = new DocumentVersionsRepository(workspace.id)
const documents = await docsScope.getDocumentsAtCommit({ commit })
return (
<Suspense fallback={<div>Loading...</div>}>
<DocumentSidebar>
<ClientFilesTree
documentPath={documentPath}
documents={documents.unwrap()}
documentUuid={documentUuid}
/>
</DocumentSidebar>
</Suspense>
<DocumentSidebar>
<ClientFilesTree
documentPath={documentPath}
documents={documents.unwrap()}
/>
</DocumentSidebar>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import { Suspense, useCallback, useRef } from 'react'

import { Commit, DocumentVersion } from '@latitude-data/core'
import { DocumentEditor, useToast } from '@latitude-data/web-ui'
import {
DocumentEditor,
DocumentTextEditorFallback,
useToast,
} from '@latitude-data/web-ui'
import { getDocumentContentByPathAction } from '$/actions/documents/getContentByPath'
import { updateDocumentContentAction } from '$/actions/documents/updateContent'
import { useServerAction } from 'zsa-react'
Expand Down Expand Up @@ -69,7 +73,7 @@ export default function ClientDocumentEditor({
)

return (
<Suspense fallback={<div>Loading...</div>}>
<Suspense fallback={<DocumentTextEditorFallback />}>
<DocumentEditor
document={document.content}
saveDocumentContent={saveDocumentContent}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
getDocumentByUuid,
} from '$/app/(private)/_data-access'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { notFound } from 'next/navigation'

import DocumentsLayout from '../../_components/DocumentsLayout'
import ClientDocumentEditor from './_components/DocumentEditor'

export default async function DocumentPage({
Expand All @@ -13,17 +15,27 @@ export default async function DocumentPage({
params: { projectId: string; commitUuid: string; documentUuid: string }
}) {
const session = await getCurrentUser()
const projectId = Number(params.projectId)
const commintUuid = params.commitUuid
const project = await findProject({
projectId: Number(params.projectId),
projectId,
workspaceId: session.workspace.id,
})
const commit = await findCommit({
project,
uuid: params.commitUuid,
})
const commit = await findCommit({ project, uuid: commintUuid })
const document = await getDocumentByUuid({
documentUuid: params.documentUuid,
commit,
})
return <ClientDocumentEditor commit={commit} document={document} />

if (!document) return notFound()

return (
<DocumentsLayout
projectId={projectId}
commitUuid={commintUuid}
document={document}
>
<ClientDocumentEditor commit={commit} document={document} />
</DocumentsLayout>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import DocumentsLayout from '../_components/DocumentsLayout'

export default async function DocumentsPage({
params,
}: {
params: { projectId: string; commitUuid: string; documentUuid: string }
}) {
const projectId = Number(params.projectId)
const commintUuid = params.commitUuid
return (
<DocumentsLayout projectId={projectId} commitUuid={commintUuid}>
<div>List of documents</div>
</DocumentsLayout>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function GET(
const commit = await commitsScope
.getCommitByUuid({ uuid: commitUuid, project })
.then((r) => r.unwrap())
const documents = await scope.getDocumentsAtCommit(commit)
const documents = await scope.getDocumentsAtCommit({ commit })

return NextResponse.json(documents.unwrap())
} catch (err: unknown) {
Expand Down
Loading

0 comments on commit a3be7fa

Please sign in to comment.