support import from code

This commit is contained in:
Abi Raja 2024-08-22 13:51:40 -04:00
parent 089f4302d2
commit c900201417
5 changed files with 43 additions and 34 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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<AiCreateCommit, "hash"> | Omit<AiEditCommit, "hash">
commit:
| Omit<AiCreateCommit, "hash">
| Omit<AiEditCommit, "hash">
| Omit<CodeCreateCommit, "hash">
): 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;

View File

@ -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);
// 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}`);

View File

@ -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 <div className="mt-2"></div>;
}
return (
<div className="mt-4 mb-4">