From 993ff88e2be6b88d2343439a5e880c052ae57507 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Thu, 25 Jul 2024 14:35:04 -0400 Subject: [PATCH] abstract into more components --- frontend/src/App.tsx | 273 ++---------------- frontend/src/components/preview/Preview.tsx | 114 ++++++++ .../PreviewComponent.tsx} | 8 +- frontend/src/components/sidebar/Sidebar.tsx | 174 +++++++++++ frontend/src/store/app-store.ts | 21 ++ frontend/src/store/project-store.ts | 13 + 6 files changed, 346 insertions(+), 257 deletions(-) create mode 100644 frontend/src/components/preview/Preview.tsx rename frontend/src/components/{Preview.tsx => preview/PreviewComponent.tsx} (87%) create mode 100644 frontend/src/components/sidebar/Sidebar.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index ae58b89..bbd55f5 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,21 +1,6 @@ -import { useEffect, useRef, useState } from "react"; +import { useEffect, useRef } from "react"; import ImageUpload from "./components/ImageUpload"; -import CodePreview from "./components/CodePreview"; -import Preview from "./components/Preview"; import { generateCode } from "./generateCode"; -import Spinner from "./components/Spinner"; -import classNames from "classnames"; -import { - FaCode, - FaDesktop, - FaDownload, - FaMobile, - FaUndo, -} from "react-icons/fa"; -import { Switch } from "./components/ui/switch"; -import { Button } from "@/components/ui/button"; -import { Textarea } from "@/components/ui/textarea"; -import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs"; import SettingsDialog from "./components/SettingsDialog"; import { AppState, CodeGenerationParams, EditorTheme, Settings } from "./types"; import { IS_RUNNING_ON_CLOUD } from "./config"; @@ -26,7 +11,6 @@ import { UrlInputSection } from "./components/UrlInputSection"; import TermsOfServiceDialog from "./components/TermsOfServiceDialog"; import html2canvas from "html2canvas"; import { USER_CLOSE_WEB_SOCKET_CODE } from "./constants"; -import CodeTab from "./components/CodeTab"; import OutputSettingsSection from "./components/OutputSettingsSection"; import { History } from "./components/history/history_types"; import HistoryDisplay from "./components/history/HistoryDisplay"; @@ -36,17 +20,14 @@ import ImportCodeSection from "./components/ImportCodeSection"; import { Stack } from "./lib/stacks"; import { CodeGenerationModel } from "./lib/models"; import ModelSettingsSection from "./components/ModelSettingsSection"; -import { extractHtml } from "./components/preview/extractHtml"; import useBrowserTabIndicator from "./hooks/useBrowserTabIndicator"; import TipLink from "./components/core/TipLink"; -import SelectAndEditModeToggleButton from "./components/select-and-edit/SelectAndEditModeToggleButton"; import { useAppStore } from "./store/app-store"; -import KeyboardShortcutBadge from "./components/core/KeyboardShortcutBadge"; import { useProjectStore } from "./store/project-store"; +import Sidebar from "./components/sidebar/Sidebar"; +import Preview from "./components/preview/Preview"; function App() { - const [appState, setAppState] = useState(AppState.INITIAL); - const { // Inputs inputMode, @@ -57,20 +38,22 @@ function App() { setReferenceImages, // Outputs - generatedCode, setGeneratedCode, + setExecutionConsole, currentVersion, setCurrentVersion, appHistory, setAppHistory, } = useProjectStore(); - const [executionConsole, setExecutionConsole] = useState([]); - const [updateInstruction, setUpdateInstruction] = useState(""); - - const textareaRef = useRef(null); - - const { disableInSelectAndEditMode } = useAppStore(); + const { + disableInSelectAndEditMode, + setUpdateInstruction, + appState, + setAppState, + shouldIncludeResultImage, + setShouldIncludeResultImage, + } = useAppStore(); // Settings const [settings, setSettings] = usePersistedState( @@ -93,9 +76,6 @@ function App() { const selectedCodeGenerationModel = settings.codeGenerationModel || CodeGenerationModel.GPT_4_VISION; - const [shouldIncludeResultImage, setShouldIncludeResultImage] = - useState(false); - const wsRef = useRef(null); const showBetterModelMessage = @@ -139,23 +119,6 @@ function App() { return png; }; - const downloadCode = () => { - // Create a blob from the generated code - const blob = new Blob([generatedCode], { type: "text/html" }); - const url = URL.createObjectURL(blob); - - // Create an anchor element and set properties for download - const a = document.createElement("a"); - a.href = url; - a.download = "index.html"; // Set the file name for download - document.body.appendChild(a); // Append to the document - a.click(); // Programmatically click the anchor to trigger download - - // Clean up by removing the anchor and revoking the Blob URL - document.body.removeChild(a); - URL.revokeObjectURL(url); - }; - const reset = () => { setAppState(AppState.INITIAL); setGeneratedCode(""); @@ -194,11 +157,6 @@ function App() { cancelCodeGenerationAndReset(); }; - const previewCode = - inputMode === "video" && appState === AppState.CODING - ? extractHtml(generatedCode) - : generatedCode; - const cancelCodeGenerationAndReset = () => { // When this is the first version, reset the entire app state if (currentVersion === null) { @@ -257,7 +215,7 @@ function App() { inputs: { prompt: params.history ? params.history[params.history.length - 1] - : updateInstruction, + : "", }, }, ]; @@ -407,13 +365,6 @@ function App() { setAppState(AppState.CODE_READY); } - // When coding is complete, focus on the update instruction textarea - useEffect(() => { - if (appState === AppState.CODE_READY && textareaRef.current) { - textareaRef.current.focus(); - } - }, [appState]); - return (
{IS_RUNNING_ON_CLOUD && } @@ -461,135 +412,12 @@ function App() { {(appState === AppState.CODING || appState === AppState.CODE_READY) && ( - <> - {/* Show code preview only when coding */} - {appState === AppState.CODING && ( -
- {/* Speed disclaimer for video mode */} - {inputMode === "video" && ( -
- Code generation from videos can take 3-4 minutes. We do - multiple passes to get the best result. Please be patient. -
- )} - -
- - {executionConsole.slice(-1)[0]} -
- - - -
- -
-
- )} - - {appState === AppState.CODE_READY && ( -
-
-