-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: sidebar implementation (#34)
This commits implements a wakky first version of the dashboard Sidebar and all the plumbing required: - client-side stores - db operations - server/client component splits where needed - server REST API
- Loading branch information
Showing
66 changed files
with
1,961 additions
and
587 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use server' | ||
|
||
import { createDocumentVersion, DocumentType } from '@latitude-data/core' | ||
import { z } from 'zod' | ||
|
||
import { authProcedure } from '../procedures' | ||
|
||
export const createDocumentVersionAction = authProcedure | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
commitUuid: z.string(), | ||
documentType: z | ||
.enum([ | ||
'folder' as DocumentType.Folder, | ||
'document' as DocumentType.Document, | ||
]) | ||
.optional(), | ||
name: z.string(), | ||
parentId: z.number().optional(), | ||
}), | ||
{ type: 'json' }, | ||
) | ||
.handler(async ({ input }) => createDocumentVersion(input)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
apps/web/src/app/api/commits/[commitUuid]/documents/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { materializeDocumentsAtCommit } from '@latitude-data/core' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function GET( | ||
req: NextRequest, | ||
{ commitUuid }: { commitUuid: string }, | ||
) { | ||
try { | ||
const staged = Boolean(req.nextUrl.searchParams.get('staged') || false) | ||
const nodes = await materializeDocumentsAtCommit({ commitUuid, staged }) | ||
|
||
return NextResponse.json(nodes) | ||
} catch (err: unknown) { | ||
const error = err as Error | ||
return NextResponse.json({ error: error.message }, { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default async function Home() { | ||
return ( | ||
<div className='p-4'> | ||
<h1>Main body</h1> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use client' | ||
|
||
import { faker } from '@faker-js/faker' | ||
import type { DocumentType, DocumentVersion } from '@latitude-data/core' | ||
import useDocumentVersions from '$/stores/documentVersions' | ||
|
||
import toTree, { Node } 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'> | ||
<CreateFolder parentId={parentId} /> | ||
<CreateDocument parentId={parentId} /> | ||
</div> | ||
) | ||
} | ||
|
||
function CreateFolder({ parentId }: { parentId?: number }) { | ||
const { create } = useDocumentVersions({ staged: true }) | ||
return ( | ||
<button | ||
onClick={() => | ||
create({ | ||
parentId, | ||
documentType: 'folder' as DocumentType.Folder, | ||
name: generateName(), | ||
}) | ||
} | ||
> | ||
+F | ||
</button> | ||
) | ||
} | ||
|
||
function CreateDocument({ parentId }: { parentId?: number }) { | ||
const { create } = useDocumentVersions({ staged: true }) | ||
return ( | ||
<button onClick={() => create({ parentId, name: generateName() })}> | ||
+D | ||
</button> | ||
) | ||
} | ||
|
||
function TreeNode({ node, level = 0 }: { node: Node; level?: number }) { | ||
return ( | ||
<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> | ||
)} | ||
{node.children.map((node) => ( | ||
<TreeNode node={node} level={level + 1} /> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default function DocumentTree({ nodes }: { nodes: DocumentVersion[] }) { | ||
const { data } = useDocumentVersions( | ||
{ staged: true }, | ||
{ fallbackData: nodes }, | ||
) | ||
const rootNode = toTree(data) | ||
|
||
return <TreeNode node={rootNode} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HEAD_COMMIT, materializeDocumentsAtCommit } from '@latitude-data/core' | ||
|
||
import DocumentTree, { CreateNode } from './DocumentTree' | ||
|
||
export default async function Sidebar() { | ||
const nodes = await materializeDocumentsAtCommit({ | ||
commitUuid: HEAD_COMMIT, | ||
staged: true, | ||
}) | ||
|
||
return ( | ||
<div className='flex flex-col gap-4 p-4'> | ||
<div className='flex flex-row align-items justify-between'> | ||
<h2>Prompts</h2> | ||
<div className='flex flex-row gap-2 align-items'> | ||
<CreateNode /> | ||
</div> | ||
</div> | ||
<DocumentTree nodes={nodes} /> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { DocumentVersion } from '@latitude-data/core' | ||
|
||
export class Node { | ||
public doc?: DocumentVersion | ||
public children: Node[] = [] | ||
public isRoot: boolean = false | ||
|
||
constructor( | ||
doc?: DocumentVersion, | ||
children: Node[] = [], | ||
isRoot: boolean = false, | ||
) { | ||
this.doc = doc | ||
this.children = children | ||
this.isRoot = isRoot | ||
} | ||
} | ||
|
||
export default function toTree(docs: DocumentVersion[]) { | ||
function iterate(node: Node) { | ||
let children | ||
if (node.isRoot) { | ||
children = Object.values(docs) | ||
.filter((doc) => !doc.parentId) | ||
.map((doc) => new Node(doc)) | ||
} else { | ||
children = docs | ||
.filter((doc) => doc.parentId === node.doc!.id) | ||
.map((doc) => new Node(doc)) | ||
} | ||
|
||
node.children = children | ||
node.children.forEach(iterate) | ||
|
||
return node | ||
} | ||
|
||
return iterate(new Node(undefined, [], true)) | ||
} |
Oops, something went wrong.