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; hash: CommitHash;
parentHash: CommitHash | null; parentHash: CommitHash | null;
dateCreated: Date; dateCreated: Date;
isCommitted: boolean;
variants: Variant[]; variants: Variant[];
selectedVariantIndex: number; selectedVariantIndex: number;
}; };

View File

@ -8,10 +8,25 @@ import {
export function createCommit( export function createCommit(
commit: commit:
| Omit<AiCreateCommit, "hash" | "dateCreated" | "selectedVariantIndex"> | Omit<
| Omit<AiEditCommit, "hash" | "dateCreated" | "selectedVariantIndex"> AiCreateCommit,
| Omit<CodeCreateCommit, "hash" | "dateCreated" | "selectedVariantIndex"> "hash" | "dateCreated" | "selectedVariantIndex" | "isCommitted"
>
| Omit<
AiEditCommit,
"hash" | "dateCreated" | "selectedVariantIndex" | "isCommitted"
>
| Omit<
CodeCreateCommit,
"hash" | "dateCreated" | "selectedVariantIndex" | "isCommitted"
>
): Commit { ): Commit {
const hash = nanoid(); 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; return null;
} }
const variants = commits[head].variants; const commit = commits[head];
const selectedVariantIndex = commits[head].selectedVariantIndex; 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>; return <div className="mt-2"></div>;
} }

View File

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