make commits immutable after a new commit is added

This commit is contained in:
Abi Raja 2024-08-29 17:57:52 +02:00
parent f09b1c3c7f
commit 0ff42d9083
4 changed files with 88 additions and 38 deletions

View File

@ -8,6 +8,7 @@ export type BaseCommit = {
hash: CommitHash;
parentHash: CommitHash | null;
dateCreated: Date;
isCommitted: boolean;
variants: Variant[];
selectedVariantIndex: number;
};

View File

@ -8,10 +8,25 @@ import {
export function createCommit(
commit:
| Omit<AiCreateCommit, "hash" | "dateCreated" | "selectedVariantIndex">
| Omit<AiEditCommit, "hash" | "dateCreated" | "selectedVariantIndex">
| Omit<CodeCreateCommit, "hash" | "dateCreated" | "selectedVariantIndex">
| Omit<
AiCreateCommit,
"hash" | "dateCreated" | "selectedVariantIndex" | "isCommitted"
>
| Omit<
AiEditCommit,
"hash" | "dateCreated" | "selectedVariantIndex" | "isCommitted"
>
| Omit<
CodeCreateCommit,
"hash" | "dateCreated" | "selectedVariantIndex" | "isCommitted"
>
): Commit {
const hash = nanoid();
return { ...commit, hash, dateCreated: new Date(), selectedVariantIndex: 0 };
return {
...commit,
hash,
isCommitted: false,
dateCreated: new Date(),
selectedVariantIndex: 0,
};
}

View File

@ -8,10 +8,12 @@ function Variants() {
return null;
}
const variants = commits[head].variants;
const selectedVariantIndex = commits[head].selectedVariantIndex;
const commit = commits[head];
const variants = commit.variants;
const selectedVariantIndex = commit.selectedVariantIndex;
if (variants.length <= 1) {
// If there is only one variant or the commit is already committed, don't show the variants
if (variants.length <= 1 || commit.isCommitted) {
return <div className="mt-2"></div>;
}

View File

@ -49,8 +49,17 @@ export const useProjectStore = create<ProjectStore>((set) => ({
head: null,
addCommit: (commit: Commit) => {
// When adding a new commit, make sure all existing commits are marked as committed
set((state) => ({
commits: { ...state.commits, [commit.hash]: commit },
commits: {
...Object.fromEntries(
Object.entries(state.commits).map(([hash, existingCommit]) => [
hash,
{ ...existingCommit, isCommitted: true },
])
),
[commit.hash]: commit,
},
}));
},
removeCommit: (hash: CommitHash) => {
@ -63,41 +72,64 @@ export const useProjectStore = create<ProjectStore>((set) => ({
resetCommits: () => set({ commits: {} }),
appendCommitCode: (hash: CommitHash, numVariant: number, code: string) =>
set((state) => ({
commits: {
...state.commits,
[hash]: {
...state.commits[hash],
variants: state.commits[hash].variants.map((variant, index) =>
index === numVariant
? { ...variant, code: variant.code + code }
: variant
),
set((state) => {
const commit = state.commits[hash];
// Don't update if the commit is already committed
if (commit.isCommitted) {
throw new Error("Attempted to append code to a committed commit");
}
return {
commits: {
...state.commits,
[hash]: {
...commit,
variants: commit.variants.map((variant, index) =>
index === numVariant
? { ...variant, code: variant.code + code }
: variant
),
},
},
},
})),
};
}),
setCommitCode: (hash: CommitHash, numVariant: number, code: string) =>
set((state) => ({
commits: {
...state.commits,
[hash]: {
...state.commits[hash],
variants: state.commits[hash].variants.map((variant, index) =>
index === numVariant ? { ...variant, code } : variant
),
set((state) => {
const commit = state.commits[hash];
// Don't update if the commit is already committed
if (commit.isCommitted) {
throw new Error("Attempted to set code of a committed commit");
}
return {
commits: {
...state.commits,
[hash]: {
...commit,
variants: commit.variants.map((variant, index) =>
index === numVariant ? { ...variant, code } : variant
),
},
},
},
})),
};
}),
updateSelectedVariantIndex: (hash: CommitHash, index: number) =>
set((state) => ({
commits: {
...state.commits,
[hash]: {
...state.commits[hash],
selectedVariantIndex: index,
set((state) => {
const commit = state.commits[hash];
// Don't update if the commit is already committed
if (commit.isCommitted) {
throw new Error(
"Attempted to update selected variant index of a committed commit"
);
}
return {
commits: {
...state.commits,
[hash]: {
...commit,
selectedVariantIndex: index,
},
},
},
})),
};
}),
setHead: (hash: CommitHash) => set({ head: hash }),
resetHead: () => set({ head: null }),