-
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.
- Loading branch information
Showing
14 changed files
with
292 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use server' | ||
|
||
import { getDocumentByPath } from '$/app/(private)/_data-access' | ||
import { z } from 'zod' | ||
|
||
import { withProject } from '../procedures' | ||
|
||
export const getDocumentContentByPathAction = withProject | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
commitId: z.number(), | ||
path: z.string(), | ||
}), | ||
{ type: 'json' }, | ||
) | ||
.handler(async ({ input }) => { | ||
const document = await getDocumentByPath({ | ||
commitId: input.commitId, | ||
path: input.path, | ||
}) | ||
return document.content | ||
}) |
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,25 @@ | ||
'use server' | ||
|
||
import { updateDocument } from '@latitude-data/core' | ||
import { z } from 'zod' | ||
|
||
import { withProject } from '../procedures' | ||
|
||
export const updateDocumentContentAction = withProject | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
documentUuid: z.string(), | ||
commitId: z.number(), | ||
content: z.string(), | ||
}), | ||
{ type: 'json' }, | ||
) | ||
.handler(async ({ input }) => { | ||
const result = await updateDocument({ | ||
commitId: input.commitId, | ||
documentUuid: input.documentUuid, | ||
content: input.content, | ||
}) | ||
return result.unwrap() | ||
}) |
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
80 changes: 80 additions & 0 deletions
80
...ctId]/versions/[commitUuid]/documents/[documentUuid]/_components/DocumentEditor/index.tsx
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 { Suspense, useCallback, useRef } from 'react' | ||
|
||
import { Commit, DocumentVersion } from '@latitude-data/core' | ||
import { DocumentEditor, useToast } from '@latitude-data/web-ui' | ||
import { getDocumentContentByPathAction } from '$/actions/documents/getContentByPath' | ||
import { updateDocumentContentAction } from '$/actions/documents/updateContent' | ||
import { useServerAction } from 'zsa-react' | ||
|
||
export default function ClientDocumentEditor({ | ||
commit, | ||
document, | ||
}: { | ||
commit: Commit | ||
document: DocumentVersion | ||
}) { | ||
const updateDocumentAction = useServerAction(updateDocumentContentAction) | ||
const readDocumentContentAction = useServerAction( | ||
getDocumentContentByPathAction, | ||
) | ||
const { toast } = useToast() | ||
|
||
const documentsByPathRef = useRef<{ [path: string]: string | undefined }>({}) | ||
|
||
const readDocument = useCallback( | ||
async (path: string) => { | ||
const documentsByPath = documentsByPathRef.current | ||
if (!(path in documentsByPath)) { | ||
const [content, error] = await readDocumentContentAction.execute({ | ||
projectId: commit.projectId, | ||
commitId: commit.id, | ||
path, | ||
}) | ||
documentsByPathRef.current = { | ||
...documentsByPath, | ||
[path]: error ? undefined : content, | ||
} | ||
} | ||
|
||
const documentContent = documentsByPath[path] | ||
if (documentContent === undefined) { | ||
throw new Error('Document not found') | ||
} | ||
|
||
return documentContent | ||
}, | ||
[readDocumentContentAction.status, commit.id], | ||
) | ||
|
||
const saveDocumentContent = useCallback( | ||
async (content: string) => { | ||
const [_, error] = await updateDocumentAction.execute({ | ||
projectId: commit.projectId, | ||
documentUuid: document.documentUuid, | ||
commitId: commit.id, | ||
content, | ||
}) | ||
|
||
if (error) { | ||
toast({ | ||
title: 'Could not save document', | ||
description: error.message, | ||
variant: 'destructive', | ||
}) | ||
} | ||
}, | ||
[commit, document, updateDocumentAction, toast], | ||
) | ||
|
||
return ( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<DocumentEditor | ||
document={document.content} | ||
saveDocumentContent={saveDocumentContent} | ||
readDocument={readDocument} | ||
/> | ||
</Suspense> | ||
) | ||
} |
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
16 changes: 14 additions & 2 deletions
16
...pp/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/page.tsx
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 |
---|---|---|
@@ -1,7 +1,19 @@ | ||
export default function DocumentPage({ | ||
import { findCommit, getDocumentByUuid } from '$/app/(private)/_data-access' | ||
|
||
import ClientDocumentEditor from './_components/DocumentEditor' | ||
|
||
export default async function DocumentPage({ | ||
params, | ||
}: { | ||
params: { projectId: string; commitUuid: string; documentUuid: string } | ||
}) { | ||
return <div>Documents {params.documentUuid}</div> | ||
const commit = await findCommit({ | ||
projectId: Number(params.projectId), | ||
uuid: params.commitUuid, | ||
}) | ||
const document = await getDocumentByUuid({ | ||
documentUuid: params.documentUuid, | ||
commitId: commit.id, | ||
}) | ||
return <ClientDocumentEditor commit={commit} document={document} /> | ||
} |
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
Oops, something went wrong.