From 0ff42d90836744b32130e5cf004041c1f790e41f Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Thu, 29 Aug 2024 17:57:52 +0200 Subject: [PATCH] make commits immutable after a new commit is added --- frontend/src/components/commits/types.ts | 1 + frontend/src/components/commits/utils.ts | 23 ++++- frontend/src/components/variants/Variants.tsx | 8 +- frontend/src/store/project-store.ts | 94 +++++++++++++------ 4 files changed, 88 insertions(+), 38 deletions(-) diff --git a/frontend/src/components/commits/types.ts b/frontend/src/components/commits/types.ts index 173663a..eb9ea12 100644 --- a/frontend/src/components/commits/types.ts +++ b/frontend/src/components/commits/types.ts @@ -8,6 +8,7 @@ export type BaseCommit = { hash: CommitHash; parentHash: CommitHash | null; dateCreated: Date; + isCommitted: boolean; variants: Variant[]; selectedVariantIndex: number; }; diff --git a/frontend/src/components/commits/utils.ts b/frontend/src/components/commits/utils.ts index 874f67e..640f83e 100644 --- a/frontend/src/components/commits/utils.ts +++ b/frontend/src/components/commits/utils.ts @@ -8,10 +8,25 @@ import { export function createCommit( commit: - | Omit - | Omit - | Omit + | 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, + }; } diff --git a/frontend/src/components/variants/Variants.tsx b/frontend/src/components/variants/Variants.tsx index 9a9fc45..1a1eb43 100644 --- a/frontend/src/components/variants/Variants.tsx +++ b/frontend/src/components/variants/Variants.tsx @@ -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
; } diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts index 3830232..b9e26a9 100644 --- a/frontend/src/store/project-store.ts +++ b/frontend/src/store/project-store.ts @@ -49,8 +49,17 @@ export const useProjectStore = create((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((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 }),