From c900201417dc908315984b8376b6d6b7c3de5505 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Thu, 22 Aug 2024 13:51:40 -0400 Subject: [PATCH] support import from code --- frontend/src/App.tsx | 31 +++++++------------ .../src/components/history/HistoryDisplay.tsx | 6 +++- .../src/components/history/history_types.ts | 13 ++++++-- frontend/src/components/history/utils.ts | 17 +++++----- frontend/src/components/variants/Variants.tsx | 10 ++++-- 5 files changed, 43 insertions(+), 34 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 7605b0d..f32b284 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -337,27 +337,20 @@ function App() { // Set input state setIsImportedFromCode(true); - console.log(code); - // Set up this project - // TODO* - // setGeneratedCode(code); setStack(stack); - // setAppHistory([ - // { - // type: "code_create", - // parentIndex: null, - // code, - // inputs: { code }, - // }, - // ]); - // setVariant(0, { - // type: "code_create", - // parentIndex: null, - // code, - // }); - // setCurrentVariantIndex(0); - // setCurrentVersion(0); + + // Set up the import commit + const commit = createCommit({ + type: "code_create", + parentHash: null, + date_created: new Date(), + variants: [{ code }], + selectedVariantIndex: 0, + inputs: null, + }); + addCommit(commit); + setHead(commit.hash); // Set the app state setAppState(AppState.CODE_READY); diff --git a/frontend/src/components/history/HistoryDisplay.tsx b/frontend/src/components/history/HistoryDisplay.tsx index 5b81f22..057c07d 100644 --- a/frontend/src/components/history/HistoryDisplay.tsx +++ b/frontend/src/components/history/HistoryDisplay.tsx @@ -22,7 +22,11 @@ export default function HistoryDisplay({ shouldDisableReverts }: Props) { // TODO: Clean this up const newHistory = Object.values(commits).flatMap((commit) => { - if (commit.type === "ai_create" || commit.type === "ai_edit") { + if ( + commit.type === "ai_create" || + commit.type === "ai_edit" || + commit.type === "code_create" + ) { return { type: commit.type, hash: commit.hash, diff --git a/frontend/src/components/history/history_types.ts b/frontend/src/components/history/history_types.ts index 183b1b6..e9171fc 100644 --- a/frontend/src/components/history/history_types.ts +++ b/frontend/src/components/history/history_types.ts @@ -17,8 +17,12 @@ export type BaseCommit = { import { nanoid } from "nanoid"; // TODO: Move to a different file +// TODO: Fix the type to be better export function createCommit( - commit: Omit | Omit + commit: + | Omit + | Omit + | Omit ): Commit { const hash = nanoid(); return { ...commit, hash }; @@ -38,7 +42,12 @@ export type AiEditCommit = BaseCommit & { }; }; -export type Commit = AiCreateCommit | AiEditCommit; +export type CodeCreateCommit = BaseCommit & { + type: "code_create"; + inputs: null; +}; + +export type Commit = AiCreateCommit | AiEditCommit | CodeCreateCommit; export type RenderedHistoryItem = { type: string; diff --git a/frontend/src/components/history/utils.ts b/frontend/src/components/history/utils.ts index e442925..965a342 100644 --- a/frontend/src/components/history/utils.ts +++ b/frontend/src/components/history/utils.ts @@ -8,23 +8,20 @@ export function extractHistory( let currentCommitHash: CommitHash | null = hash; while (currentCommitHash !== null) { - const commit: Commit = commits[currentCommitHash]; + const commit: Commit | null = commits[currentCommitHash]; if (commit) { - if (commit.type === "ai_create") { - // Don't include the image for ai_create - flatHistory.unshift(commit.variants[commit.selectedVariantIndex].code); - } else if (commit.type === "ai_edit") { - flatHistory.unshift(commit.variants[commit.selectedVariantIndex].code); + flatHistory.unshift(commit.variants[commit.selectedVariantIndex].code); + + // For edits, add the prompt to the history + if (commit.type === "ai_edit") { flatHistory.unshift(commit.inputs.prompt); } - // } else if (item.type === "code_create") { - // flatHistory.unshift(item.code); - // } // Move to the parent of the current item currentCommitHash = commit.parentHash; } else { + // TODO*: Send to Sentry throw new Error("Malformed history: missing parent index"); } } @@ -39,6 +36,8 @@ export function summarizeHistoryItem(commit: Commit) { return "Create"; case "ai_edit": return commit.inputs.prompt; + case "code_create": + return "Imported from code"; default: { const exhaustiveCheck: never = commitType; throw new Error(`Unhandled case: ${exhaustiveCheck}`); diff --git a/frontend/src/components/variants/Variants.tsx b/frontend/src/components/variants/Variants.tsx index 3ad9f98..9a9fc45 100644 --- a/frontend/src/components/variants/Variants.tsx +++ b/frontend/src/components/variants/Variants.tsx @@ -3,13 +3,17 @@ import { useProjectStore } from "../../store/project-store"; function Variants() { const { head, commits, updateSelectedVariantIndex } = useProjectStore(); - // TODO: Is HEAD null right? And check variants.length === 0 || + // TODO*: Is HEAD null right? And check variants.length === 0 || if (head === null) { return null; } - const variants = commits[head || ""].variants; - const selectedVariantIndex = commits[head || ""].selectedVariantIndex; + const variants = commits[head].variants; + const selectedVariantIndex = commits[head].selectedVariantIndex; + + if (variants.length <= 1) { + return
; + } return (