Skip to content

Commit

Permalink
Current commit management
Browse files Browse the repository at this point in the history
  • Loading branch information
csansoon committed Jul 16, 2024
1 parent 393a84f commit 08fcb09
Show file tree
Hide file tree
Showing 21 changed files with 1,037 additions and 92 deletions.
10 changes: 1 addition & 9 deletions apps/web/src/app/(private)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ReactNode } from 'react'

import Sidebar from '$/components/Sidebar'
import { getSession } from '$/services/auth/getSession'
import { ROUTES } from '$/services/routes'
import { redirect } from 'next/navigation'
Expand All @@ -14,12 +13,5 @@ export default async function PrivateLayout({
if (!data.session) {
return redirect(ROUTES.auth.login)
}
return (
<main className='flex flex-row w-full'>
<div className='w-[280px]'>
<Sidebar />
</div>
<div className='flex-1'>{children}</div>
</main>
)
return children
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DocumentEditor } from '@latitude-data/web-ui'
import { getDocument } from '$core/data-access'

export const dynamic = 'force-dynamic'

export default async function Editor({
params,
}: {
params: { commitUuid: string; documentId: number }
}) {
const result = await getDocument({
commitUuid: params.commitUuid,
documentId: params.documentId,
})
const { content } = result.unwrap()

return (
<div className='w-full h-full relative'>
<DocumentEditor content={content} />
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ReactNode } from 'react'

import { CommitProvider } from '@latitude-data/web-ui'
import Sidebar from '$/components/Sidebar'
import { getCommitOrder } from '$core/data-access'

// import { getIsCommitEditableAction } from '$/actions/commits'

export default async function PrivateLayout({
children,
params,
}: {
children: ReactNode
params: { commitUuid: string }
}) {
const commitUuid = params.commitUuid
const commitOrder = await getCommitOrder({ uuid: commitUuid })
const isDraft = commitOrder.unwrap() === null

return (
<CommitProvider commitUuid={commitUuid} isDraft={isDraft}>
<main className='flex flex-row w-full'>
<div className='w-[280px]'>
<Sidebar commitUuid={commitUuid} />
</div>
<div className='flex-1'>{children}</div>
</main>
</CommitProvider>
)
}
5 changes: 2 additions & 3 deletions apps/web/src/app/api/commits/[commitUuid]/documents/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { materializeDocumentsAtCommit } from '@latitude-data/core'
import { NextRequest, NextResponse } from 'next/server'

export async function GET(
req: NextRequest,
_: NextRequest,
{ commitUuid }: { commitUuid: string },
) {
try {
const staged = Boolean(req.nextUrl.searchParams.get('staged') || false)
const documents = await materializeDocumentsAtCommit({ commitUuid, staged })
const documents = await materializeDocumentsAtCommit({ commitUuid })

return NextResponse.json(documents)
} catch (err: unknown) {
Expand Down
24 changes: 18 additions & 6 deletions apps/web/src/components/Sidebar/DocumentTree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { faker } from '@faker-js/faker'
import type { DocumentType, DocumentVersion } from '@latitude-data/core'
import { useCurrentCommit } from '@latitude-data/web-ui'
import useDocumentVersions from '$/stores/documentVersions'

import { Node, useTree } from '../toTree'
Expand All @@ -20,26 +21,36 @@ export function CreateNode({ parentId }: { parentId?: number }) {
}

function CreateFolder({ parentId }: { parentId?: number }) {
const { create } = useDocumentVersions({ staged: true })
const { commitUuid, isDraft } = useCurrentCommit()
const { create } = useDocumentVersions({ commitUuid: commitUuid })
return (
<button
onClick={() =>
disabled={!isDraft}
onClick={() => {
if (!isDraft) return
create({
parentId,
documentType: 'folder' as DocumentType.Folder,
name: generateName(),
})
}
}}
>
+F
</button>
)
}

function CreateDocument({ parentId }: { parentId?: number }) {
const { create } = useDocumentVersions({ staged: true })
const { commitUuid, isDraft } = useCurrentCommit()
const { create } = useDocumentVersions({ commitUuid: commitUuid })
return (
<button onClick={() => create({ parentId, name: generateName() })}>
<button
disabled={!isDraft}
onClick={() => {
if (!isDraft) return
create({ parentId, name: generateName() })
}}
>
+D
</button>
)
Expand Down Expand Up @@ -74,8 +85,9 @@ export default function DocumentTree({
}: {
documents: DocumentVersion[]
}) {
const { commitUuid } = useCurrentCommit()
const { documents } = useDocumentVersions(
{ staged: true },
{ commitUuid },
{ fallbackData: serverDocuments },
)
const rootNode = useTree({ documents })
Expand Down
7 changes: 3 additions & 4 deletions apps/web/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { HEAD_COMMIT, materializeDocumentsAtCommit } from '@latitude-data/core'
import { materializeDocumentsAtCommit } from '@latitude-data/core'

import DocumentTree, { CreateNode } from './DocumentTree'

export default async function Sidebar() {
export default async function Sidebar({ commitUuid }: { commitUuid: string }) {
const documents = await materializeDocumentsAtCommit({
commitUuid: HEAD_COMMIT,
staged: true,
commitUuid,
})

return (
Expand Down
13 changes: 3 additions & 10 deletions apps/web/src/stores/documentVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ import { useServerAction } from 'zsa-react'
const FIXME_HARDCODED_PROJECT_ID = 1
export default function useDocumentVersions(
{
commitUuid = HEAD_COMMIT,
staged = false,
commitUuid,
}: {
commitUuid?: string
staged?: boolean
},
opts?: SWRConfiguration,
) {
const key =
`/api/commits/${commitUuid}/documents?` +
new URLSearchParams({
staged: String(staged),
}).toString()
const key = `/api/commits/${commitUuid ?? HEAD_COMMIT}/documents`

const { mutate, data, ...rest } = useSWR<DocumentVersion[]>(
key,
Expand All @@ -34,7 +28,6 @@ export default function useDocumentVersions(
const { execute } = useServerAction(createDocumentVersionAction)
const create = useCallback(
async (payload: {
commitUuid?: string
name: string
documentType?: DocumentType
parentId?: number
Expand All @@ -43,7 +36,7 @@ export default function useDocumentVersions(
...payload,
projectId: FIXME_HARDCODED_PROJECT_ID,
name: payload.name!,
commitUuid: payload.commitUuid || HEAD_COMMIT,
commitUuid: commitUuid || HEAD_COMMIT,
})
const prev = documents ?? []

Expand Down
6 changes: 6 additions & 0 deletions packages/core/drizzle/0004_steep_blob.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE "latitude"."commits" DROP CONSTRAINT "commits_next_commit_id_commits_id_fk";
--> statement-breakpoint
DROP INDEX IF EXISTS "commit_next_commit_idx";--> statement-breakpoint
ALTER TABLE "latitude"."commits" ADD COLUMN "merged_at" timestamp;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "project_commit_order_idx" ON "latitude"."commits" USING btree ("merged_at","project_id");--> statement-breakpoint
ALTER TABLE "latitude"."commits" DROP COLUMN IF EXISTS "next_commit_id";
Loading

0 comments on commit 08fcb09

Please sign in to comment.