-
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
16 changed files
with
194 additions
and
71 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,11 @@ | ||
import { DocumentEditor } from '@latitude-data/web-ui' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export default function Editor() { | ||
return ( | ||
<div className='w-full h-full relative'> | ||
<DocumentEditor document={`This is a commit`} /> | ||
</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,32 @@ | ||
import { ReactNode } from 'react' | ||
|
||
// import Sidebar from '$/components/Sidebar' | ||
import { CommitProvider } from '@latitude-data/web-ui' | ||
import { getNextCommitId, getPreviousCommitId } 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 nextCommitId = await getNextCommitId({ uuid: commitUuid }) | ||
const previousCommitId = await getPreviousCommitId({ uuid: commitUuid }) | ||
|
||
const isDraft = | ||
nextCommitId.unwrap() === null && previousCommitId.unwrap() === null | ||
|
||
return ( | ||
<CommitProvider commitUuid={commitUuid} isDraft={isDraft}> | ||
<main className='flex flex-row w-full'> | ||
<div className='w-[280px]'>{/* <Sidebar /> */}</div> | ||
<div className='flex-1'>{children}</div> | ||
</main> | ||
</CommitProvider> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,94 @@ | ||
import { database } from '$core/client' | ||
import { HEAD_COMMIT } from '$core/constants' | ||
import { HEAD_COMMIT, Result, TypedResult } from '$core/constants' | ||
import { LatitudeError, NotFoundError } from '$core/lib/errors' | ||
import { commits } from '$core/schema' | ||
import { desc, eq, isNull } from 'drizzle-orm' | ||
import { and, desc, eq, isNotNull, isNull } from 'drizzle-orm' | ||
|
||
const selectCondition = (uuid?: Exclude<string, 'HEAD'>) => { | ||
if (!uuid) return isNull(commits.nextCommitId) | ||
export async function findHeadCommit( | ||
tx = database, | ||
): Promise<TypedResult<number, LatitudeError>> { | ||
// Commits that are being pointed at by other commits. | ||
const childCommits = tx | ||
.$with('child_commits') | ||
.as( | ||
tx | ||
.select({ id: commits.nextCommitId }) | ||
.from(commits) | ||
.where(isNotNull(commits.nextCommitId)) | ||
.orderBy(desc(commits.id)) | ||
.limit(1), | ||
) | ||
|
||
return eq(commits.uuid, uuid) | ||
} | ||
// Commits that have previous commits but no next commits. | ||
const headCommits = await tx | ||
.with(childCommits) | ||
.select({ id: commits.id }) | ||
.from(commits) | ||
.leftJoin(childCommits, eq(commits.id, childCommits.id)) | ||
.where(and(isNull(commits.nextCommitId), isNotNull(childCommits.id))) | ||
|
||
if (headCommits.length < 1) { | ||
return Result.error(new NotFoundError('No head commit found')) | ||
} | ||
|
||
export async function findCommit({ uuid }: { uuid?: string }, tx = database) { | ||
if (uuid === HEAD_COMMIT) { | ||
return tx.query.commits.findFirst({ orderBy: desc(commits.id) }) | ||
if (headCommits.length > 1) { | ||
return Result.error(new LatitudeError('Multiple head commits found')) | ||
} | ||
|
||
return tx.query.commits.findFirst({ where: selectCondition(uuid) }) | ||
const headCommit = headCommits[0]! | ||
return Result.ok(headCommit.id) | ||
} | ||
|
||
export async function findCommit( | ||
{ uuid }: { uuid: string }, | ||
tx = database, | ||
): Promise<TypedResult<number, LatitudeError>> { | ||
if (uuid === HEAD_COMMIT) return findHeadCommit(tx) | ||
|
||
const res = await tx | ||
.select() | ||
.from(commits) | ||
.where(eq(commits.uuid, uuid)) | ||
.limit(1) | ||
|
||
if (!res.length) return Result.error(new NotFoundError('Commit not found')) | ||
const commit = res[0]! | ||
return Result.ok(commit.id) | ||
} | ||
|
||
export async function listCommits() { | ||
return database.select().from(commits) | ||
} | ||
|
||
export async function listStagedCommits() { | ||
return database.select().from(commits).where(isNull(commits.nextCommitId)) | ||
export async function getNextCommitId( | ||
{ uuid }: { uuid: string }, | ||
tx = database, | ||
): Promise<TypedResult<number | null, LatitudeError>> { | ||
if (uuid === HEAD_COMMIT) return Result.ok(null) | ||
const res = await tx | ||
.select({ nextCommitId: commits.nextCommitId }) | ||
.from(commits) | ||
.where(eq(commits.uuid, uuid)) | ||
.limit(1) | ||
|
||
if (!res.length) return Result.error(new NotFoundError('Commit not found')) | ||
|
||
return Result.ok(res[0]!.nextCommitId) | ||
} | ||
|
||
export async function getPreviousCommitId( | ||
{ uuid }: { uuid: string }, | ||
tx = database, | ||
): Promise<TypedResult<number | null, LatitudeError>> { | ||
const commitIdResult = await findCommit({ uuid }, tx) | ||
if (!Result.isOk(commitIdResult)) return commitIdResult | ||
const commitId = commitIdResult.unwrap() | ||
|
||
const res = await tx | ||
.select({ id: commits.id }) | ||
.from(commits) | ||
.where(eq(commits.nextCommitId, commitId)) | ||
|
||
if (!res.length) return Result.ok(null) | ||
return Result.ok(res[0]!.id) | ||
} |
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
15 changes: 2 additions & 13 deletions
15
packages/core/src/services/documentVersions/materializeAtCommit.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
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.