Skip to content

Commit

Permalink
Document path (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
csansoon authored Jul 19, 2024
1 parent 549805f commit 70a37d7
Show file tree
Hide file tree
Showing 14 changed files with 844 additions and 151 deletions.
6 changes: 2 additions & 4 deletions apps/web/src/actions/documents/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use server'

import { createDocumentVersion, DocumentType } from '@latitude-data/core'
import { createDocumentVersion } from '@latitude-data/core'
import { z } from 'zod'

import { withProject } from '../procedures'
Expand All @@ -9,10 +9,8 @@ export const createDocumentVersionAction = withProject
.createServerAction()
.input(
z.object({
name: z.string(),
path: z.string(),
commitUuid: z.string(),
parentId: z.number().optional(),
documentType: z.nativeEnum(DocumentType).optional(),
}),
{ type: 'json' },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { AppLayout, BreadcrumpBadge } from '@latitude-data/web-ui/browser'
import { findCommit, findProject } from '$/app/(private)/_data-access'
import { NAV_LINKS } from '$/app/(private)/_lib/constants'
import { ProjectPageParams } from '$/app/(private)/projects/[projectId]/page'
import Sidebar from '$/components/Sidebar'
import { getCurrentUser, SessionData } from '$/services/auth/getCurrentUser'
import { notFound } from 'next/navigation'

Expand Down Expand Up @@ -67,7 +66,8 @@ export default async function CommitLayout({
>
<main className='flex flex-row w-full'>
<div className='w-[280px]'>
<Sidebar commitUuid={commit.uuid} projectId={project.id} />
{/* TODO: commented out until fixed toTree methods to new path schema */}
{/* <Sidebar commitUuid={commit.uuid} projectId={project.id} /> */}
</div>
<div className='flex-1'>{children}</div>
</main>
Expand Down
26 changes: 6 additions & 20 deletions apps/web/src/components/Sidebar/DocumentTree/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
'use client'

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

import { Node, useTree } from '../toTree'

function generateName() {
return faker.science.chemicalElement().name
}

export function CreateNode({ parentId }: { parentId?: number }) {
return (
<div className='flex flex-row items-center gap-1'>
Expand All @@ -34,9 +29,7 @@ function CreateFolder({ parentId }: { parentId?: number }) {
onClick={() => {
if (!isDraft) return
create({
parentId,
documentType: 'folder' as DocumentType.Folder,
name: generateName(),
path: String(parentId), // TODO: This makes no sense, it's just so the linter doesn't complain
})
}}
>
Expand All @@ -58,7 +51,9 @@ function CreateDocument({ parentId }: { parentId?: number }) {
disabled={!isDraft}
onClick={() => {
if (!isDraft) return
create({ parentId, name: generateName() })
create({
path: String(parentId),
}) // TODO: This makes no sense, it's just so the linter doesn't complain
}}
>
+D
Expand All @@ -71,16 +66,7 @@ function TreeNode({ node, level = 0 }: { node: Node; level?: number }) {
<div key={node.doc?.id || 'root'}>
<div className='flex flex-col gap-2' style={{ paddingLeft: level * 2 }}>
{!!node.doc && (
<div className='flex flex-row align-items justify-between'>
{node.doc.documentType === 'folder' ? (
<>
<p>{node.doc.name}</p>
<CreateNode parentId={node.doc.id} />
</>
) : (
<p className='font-bold'>{node.doc.name}</p>
)}
</div>
<div className='flex flex-row align-items justify-between'></div>
)}
{node.children.map((node, idx) => (
<TreeNode key={idx} node={node} level={level + 1} />
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/components/Sidebar/toTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export class Node {
export function useTree({ documents }: { documents: DocumentVersion[] }) {
return useMemo(() => {
function iterate(node: Node) {
node.children = documents
.filter((doc) => doc.parentId === (node.isRoot ? null : node.doc?.id))
.map((doc) => new Node(doc))
node.children = documents.map((doc) => new Node(doc))

node.children.forEach(iterate)
return node
Expand Down
10 changes: 3 additions & 7 deletions apps/web/src/stores/documentVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useCallback } from 'react'

import { DocumentVersion } from '@latitude-data/core'
import { HEAD_COMMIT, type DocumentType } from '@latitude-data/core/browser'
import { HEAD_COMMIT } from '@latitude-data/core/browser'
import { createDocumentVersionAction } from '$/actions/documents/create'
import useSWR, { SWRConfiguration } from 'swr'
import { useServerAction } from 'zsa-react'
Expand All @@ -28,15 +28,11 @@ export default function useDocumentVersions(
const documents = data ?? []
const { execute } = useServerAction(createDocumentVersionAction)
const create = useCallback(
async (payload: {
name: string
documentType?: DocumentType
parentId?: number
}) => {
async (payload: { path: string }) => {
const [document] = await execute({
...payload,
projectId,
name: payload.name!,
path: payload.path,
commitUuid: commitUuid || HEAD_COMMIT,
})
const prev = documents ?? []
Expand Down
22 changes: 22 additions & 0 deletions packages/core/drizzle/0005_right_magik.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DROP TABLE "latitude"."document_hierarchies";--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" DROP CONSTRAINT "document_versions_parent_id_document_versions_id_fk";
--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" ALTER COLUMN "content" SET DEFAULT '';--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" ALTER COLUMN "content" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" ADD COLUMN "path" varchar NOT NULL;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "document_uuid_commit_id_idx" ON "latitude"."document_versions" USING btree ("document_uuid","commit_id");--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" DROP COLUMN IF EXISTS "name";--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" DROP COLUMN IF EXISTS "document_type";--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" DROP COLUMN IF EXISTS "parent_id";--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" ADD CONSTRAINT "unique_document_uuid_commit_id" UNIQUE("document_uuid","commit_id");--> statement-breakpoint
ALTER TABLE "latitude"."document_versions" ADD CONSTRAINT "unique_path_commit_id" UNIQUE("path","commit_id");

-- Drop triggers
DROP TRIGGER IF EXISTS after_document_versions_insert ON "latitude"."document_versions";
DROP TRIGGER IF EXISTS after_document_versions_delete ON "latitude"."document_versions";
DROP TRIGGER IF EXISTS after_document_versions_update ON "latitude"."document_versions";

-- Drop functions
DROP FUNCTION IF EXISTS document_versions_insert_trigger();
DROP FUNCTION IF EXISTS document_versions_delete_trigger();
DROP FUNCTION IF EXISTS document_versions_update_trigger();
Loading

0 comments on commit 70a37d7

Please sign in to comment.