Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Jul 27, 2024
1 parent a3be7fa commit 0a34227
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function DocumentsLayout({
})
return (
<DocumentDetailWrapper>
<Sidebar commit={commit} documentPath={document?.path} />
<Sidebar commit={commit} currentDocument={document} />
{children}
</DocumentDetailWrapper>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import { useRouter } from 'next/navigation'

export default function ClientFilesTree({
documents: serverDocuments,
documentPath,
currentDocument,
}: {
documents: SidebarDocument[]
documentPath: string | undefined
currentDocument: SidebarDocument | undefined
}) {
const router = useRouter()
const { commit, isHead } = useCurrentCommit()
const { project } = useCurrentProject()

const documentPath = currentDocument?.path
const navigateToDocument = useCallback((documentUuid: string) => {
router.push(
ROUTES.projects
Expand All @@ -32,9 +32,13 @@ export default function ClientFilesTree({
.documents.detail({ uuid: documentUuid }).root,
)
}, [])
const { createFile, destroyFile, documents } = useDocumentVersions({
fallbackData: serverDocuments,
})
const { createFile, destroyFile, documents } = useDocumentVersions(
{ currentDocument },
{ fallbackData: serverDocuments },
)

console.log('DO_UUID', currentDocument?.documentUuid)
console.log('DO_PATH', currentDocument?.path)

return (
<FilesTree
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Commit, DocumentVersionsRepository } from '@latitude-data/core'
import { Commit, DocumentVersion, 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,
documentPath,
currentDocument,
}: {
commit: Commit
documentPath?: string
currentDocument?: DocumentVersion
}) {
const { workspace } = await getCurrentUser()
const docsScope = new DocumentVersionsRepository(workspace.id)
const documents = await docsScope.getDocumentsAtCommit({ commit })
return (
<DocumentSidebar>
<ClientFilesTree
documentPath={documentPath}
currentDocument={currentDocument}
documents={documents.unwrap()}
/>
</DocumentSidebar>
Expand Down
18 changes: 12 additions & 6 deletions apps/web/src/stores/documentVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback } from 'react'

import { DocumentVersion } from '@latitude-data/core'
import {
SidebarDocument,
useCurrentCommit,
useCurrentProject,
useToast,
Expand All @@ -16,7 +17,10 @@ import { useRouter } from 'next/navigation'
import useSWR, { SWRConfiguration } from 'swr'
import { useServerAction } from 'zsa-react'

export default function useDocumentVersions(opts?: SWRConfiguration) {
export default function useDocumentVersions(
{ currentDocument }: { currentDocument: SidebarDocument | undefined },
opts?: SWRConfiguration,
) {
const { toast } = useToast()
const router = useRouter()
const { project } = useCurrentProject()
Expand Down Expand Up @@ -98,11 +102,13 @@ export default function useDocumentVersions(opts?: SWRConfiguration) {
} else {
const prevDocuments = data || []
mutate(prevDocuments.filter((d) => d.documentUuid !== documentUuid))
router.push(
ROUTES.projects
.detail({ id: project.id })
.commits.detail({ uuid: commit.uuid }).documents.root,
)
if (currentDocument?.documentUuid === documentUuid) {
router.push(
ROUTES.projects
.detail({ id: project.id })
.commits.detail({ uuid: commit.uuid }).documents.root,
)
}
}
},
[executeDestroyDocument, mutate, data],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export class DocumentVersionsRepository extends Repository {
.from(this.scope)
.where(
and(
eq(documentVersions.commitId, commit.id),
eq(documentVersions.documentUuid, documentUuid),
eq(this.scope.commitId, commit.id),
eq(this.scope.documentUuid, documentUuid),
),
)
.limit(1)
Expand Down

0 comments on commit 0a34227

Please sign in to comment.