Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

make commits immutable after a new commit is added

Abi Raja0ff42d908367
diff --git a/frontend/src/components/commits/types.ts b/frontend/src/components/commits/types.ts
index 173663a6..eb9ea123 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 874f67ea..640f83e9 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<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,
+ };
}
diff --git a/frontend/src/components/variants/Variants.tsx b/frontend/src/components/variants/Variants.tsx
index 9a9fc454..1a1eb437 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 <div className="mt-2"></div>;
}
diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts
index 3830232d..b9e26a9c 100644
--- a/frontend/src/store/project-store.ts
+++ b/frontend/src/store/project-store.ts
@@ -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 }),