Skip to content

Commit

Permalink
Added mergedAt attribute to commit (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
csansoon authored Jul 18, 2024
1 parent dfddce0 commit 52101f5
Show file tree
Hide file tree
Showing 25 changed files with 1,436 additions and 124 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,28 @@
import { ReactNode } from 'react'

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

export default async function PrivateLayout({
children,
params,
}: {
children: ReactNode
params: { commitUuid: string; projectId: number }
}) {
const { commitUuid, projectId } = params
const commitMergeTime = await getCommitMergedAt({ projectId, commitUuid })
const isDraft = commitMergeTime.unwrap() === null

return (
<CommitProvider commitUuid={commitUuid} isDraft={isDraft}>
<main className='flex flex-row w-full'>
<div className='w-[280px]'>
<Sidebar commitUuid={commitUuid} projectId={projectId} />
</div>
<div className='flex-1'>{children}</div>
</main>
</CommitProvider>
)
}
15 changes: 15 additions & 0 deletions apps/web/src/app/(private)/projects/[projectId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ReactNode } from 'react'

import { ProjectProvider } from '@latitude-data/web-ui'

export default async function PrivateLayout({
children,
params,
}: {
children: ReactNode
params: { projectId: number }
}) {
const { projectId } = params

return <ProjectProvider projectId={projectId}>{children}</ProjectProvider>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { materializeDocumentsAtCommit } from '@latitude-data/core'
import { NextRequest, NextResponse } from 'next/server'

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

return NextResponse.json(documents)
} catch (err: unknown) {
Expand Down
27 changes: 21 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, useCurrentProject } from '@latitude-data/web-ui'
import useDocumentVersions from '$/stores/documentVersions'

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

function CreateFolder({ parentId }: { parentId?: number }) {
const { create } = useDocumentVersions({ staged: true })
const { commitUuid, isDraft } = useCurrentCommit()
const { projectId } = useCurrentProject()
const { create } = useDocumentVersions({ projectId, 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 { projectId } = useCurrentProject()
const { create } = useDocumentVersions({ projectId, 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 +87,10 @@ export default function DocumentTree({
}: {
documents: DocumentVersion[]
}) {
const { commitUuid } = useCurrentCommit()
const { projectId } = useCurrentProject()
const { documents } = useDocumentVersions(
{ staged: true },
{ commitUuid, projectId },
{ fallbackData: serverDocuments },
)
const rootNode = useTree({ documents })
Expand Down
17 changes: 12 additions & 5 deletions apps/web/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { HEAD_COMMIT, materializeDocumentsAtCommit } from '@latitude-data/core'
import { materializeDocumentsAtCommit } from '@latitude-data/core'

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

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

return (
<div className='flex flex-col gap-4 p-4'>
Expand Down
18 changes: 6 additions & 12 deletions apps/web/src/stores/documentVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ import { createDocumentVersionAction } from '$/actions/documents/create'
import useSWR, { SWRConfiguration } from 'swr'
import { useServerAction } from 'zsa-react'

const FIXME_HARDCODED_PROJECT_ID = 1
export default function useDocumentVersions(
{
commitUuid = HEAD_COMMIT,
staged = false,
commitUuid,
projectId,
}: {
commitUuid?: string
staged?: boolean
projectId: number
},
opts?: SWRConfiguration,
) {
const key =
`/api/commits/${commitUuid}/documents?` +
new URLSearchParams({
staged: String(staged),
}).toString()
const key = `/api/projects/${projectId}/commits/${commitUuid ?? HEAD_COMMIT}/documents`

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

Expand Down
14 changes: 14 additions & 0 deletions packages/core/drizzle/0004_cold_ben_parker.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ALTER TABLE "latitude"."commits" DROP CONSTRAINT "commits_next_commit_id_commits_id_fk";
--> statement-breakpoint
ALTER TABLE "latitude"."commits" DROP CONSTRAINT "commits_project_id_workspaces_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
DO $$ BEGIN
ALTER TABLE "latitude"."commits" ADD CONSTRAINT "commits_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "latitude"."projects"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> 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 52101f5

Please sign in to comment.