Skip to content

Commit

Permalink
make commits immutable after a new commit is added
Browse files Browse the repository at this point in the history
  • Loading branch information
abi committed Aug 29, 2024
1 parent f09b1c3 commit 0ff42d9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 38 deletions.
1 change: 1 addition & 0 deletions frontend/src/components/commits/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type BaseCommit = {
hash: CommitHash;
parentHash: CommitHash | null;
dateCreated: Date;
isCommitted: boolean;
variants: Variant[];
selectedVariantIndex: number;
};
Expand Down
23 changes: 19 additions & 4 deletions frontend/src/components/commits/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
8 changes: 5 additions & 3 deletions frontend/src/components/variants/Variants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand Down
94 changes: 63 additions & 31 deletions frontend/src/store/project-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 }),
Expand Down

0 comments on commit 0ff42d9

Please sign in to comment.