π OpenHands: AI-Driven Development
[ALL-596] fix(frontend): Fix import project from sending request before runtime is active (#4513)
sp.wack7b81df2a94fa
diff --git a/frontend/src/routes/_oh._index/task-form.tsx b/frontend/src/routes/_oh._index/task-form.tsxindex e60e46aabe..4f6a68dd99 100644--- a/frontend/src/routes/_oh._index/task-form.tsx+++ b/frontend/src/routes/_oh._index/task-form.tsx@@ -1,15 +1,26 @@import React from "react";-import { Form, useFetcher, useNavigation } from "@remix-run/react";+import { Form, useNavigation } from "@remix-run/react";import { useDispatch, useSelector } from "react-redux";import Send from "#/assets/send.svg?react";import Clip from "#/assets/clip.svg?react";import { cn } from "#/utils/utils";import { RootState } from "#/store";-import { addFile } from "#/state/initial-query-slice";+import { addFile, setImportedProjectZip } from "#/state/initial-query-slice";import { SuggestionBubble } from "#/components/suggestion-bubble";import { SUGGESTIONS } from "#/utils/suggestions";import { convertImageToBase64 } from "#/utils/convert-image-to-base-64";+const convertZipToBase64 = async (file: File) => {+ const reader = new FileReader();++ return new Promise<string>((resolve) => {+ reader.onload = () => {+ resolve(reader.result as string);+ };+ reader.readAsDataURL(file);+ });+};+interface MainTextareaInputProps {disabled: boolean;placeholder: string;@@ -88,7 +99,6 @@ interface TaskFormProps {export function TaskForm({ importedProjectZip, textareaRef }: TaskFormProps) {const dispatch = useDispatch();const navigation = useNavigation();- const fetcher = useFetcher();const { selectedRepository } = useSelector((state: RootState) => state.initalQuery,@@ -134,16 +144,12 @@ export function TaskForm({ importedProjectZip, textareaRef }: TaskFormProps) {setText(e.target.value);};- const handleSubmitForm = () => {- // This is submitted on top of the form submission- const formData = new FormData();+ const handleSubmitForm = async () => {+ // This is handled on top of the form submissionif (importedProjectZip) {- formData.append("imported-project", importedProjectZip);- fetcher.submit(formData, {- method: "POST",- action: "/upload-initial-files",- encType: "multipart/form-data",- });+ dispatch(+ setImportedProjectZip(await convertZipToBase64(importedProjectZip)),+ );}};diff --git a/frontend/src/routes/_oh.app.tsx b/frontend/src/routes/_oh.app.tsxindex 97b76ce56f..9e009fcd23 100644--- a/frontend/src/routes/_oh.app.tsx+++ b/frontend/src/routes/_oh.app.tsx@@ -35,6 +35,7 @@ import { createChatMessage } from "#/services/chatService";import {clearFiles,clearSelectedRepository,+ setImportedProjectZip,} from "#/state/initial-query-slice";import { isGitHubErrorReponse, retrieveLatestGitHubCommit } from "#/api/github";import OpenHands from "#/api/open-hands";@@ -66,19 +67,10 @@ export const clientLoader = async () => {const repo =store.getState().initalQuery.selectedRepository ||localStorage.getItem("repo");- const importedProject = store.getState().initalQuery.importedProjectZip;const settings = getSettings();const token = localStorage.getItem("token");- if (token && importedProject) {- const blob = base64ToBlob(importedProject);- const file = new File([blob], "imported-project.zip", {- type: blob.type,- });- await OpenHands.uploadFiles(token, [file]);- }-if (repo) localStorage.setItem("repo", repo);let lastCommit: GitHubCommit | null = null;@@ -116,7 +108,9 @@ export const clientAction = async ({ request }: ClientActionFunctionArgs) => {function App() {const dispatch = useDispatch();- const { files } = useSelector((state: RootState) => state.initalQuery);+ const { files, importedProjectZip } = useSelector(+ (state: RootState) => state.initalQuery,+ );const { start, send, setRuntimeIsInitialized, runtimeActive } = useSocket();const { settings, token, ghToken, repo, q, lastCommit } =useLoaderData<typeof clientLoader>();@@ -223,12 +217,30 @@ function App() {});React.useEffect(() => {- // Export if the user valid, this could happen mid-session so it is handled here- if (userId && ghToken && runtimeActive) {+ if (runtimeActive && userId && ghToken) {+ // Export if the user valid, this could happen mid-session so it is handled heresend(getGitHubTokenCommand(ghToken));}}, [userId, ghToken, runtimeActive]);+ React.useEffect(() => {+ (async () => {+ if (runtimeActive && token && importedProjectZip) {+ // upload files action+ try {+ const blob = base64ToBlob(importedProjectZip);+ const file = new File([blob], "imported-project.zip", {+ type: blob.type,+ });+ await OpenHands.uploadFiles(token, [file]);+ dispatch(setImportedProjectZip(null));+ } catch (error) {+ toast.error("Failed to upload project files.");+ }+ }+ })();+ }, [runtimeActive, token, importedProjectZip]);+const {isOpen: securityModalIsOpen,onOpen: onSecurityModalOpen,diff --git a/frontend/src/routes/upload-initial-files.ts b/frontend/src/routes/upload-initial-files.tsdeleted file mode 100644index d87b250951..0000000000--- a/frontend/src/routes/upload-initial-files.ts+++ /dev/null@@ -1,33 +0,0 @@-import { ClientActionFunctionArgs, json } from "@remix-run/react";-import { setImportedProjectZip } from "#/state/initial-query-slice";-import store from "#/store";--const convertZipToBase64 = async (file: File) => {- const reader = new FileReader();-- return new Promise<string>((resolve) => {- reader.onload = () => {- resolve(reader.result as string);- };- reader.readAsDataURL(file);- });-};--export const clientAction = async ({ request }: ClientActionFunctionArgs) => {- const isMultipart = !!request.headers- .get("Content-Type")- ?.includes("multipart");-- if (isMultipart) {- const formData = await request.formData();- const importedProject = formData.get("imported-project");-- if (importedProject instanceof File) {- store.dispatch(- setImportedProjectZip(await convertZipToBase64(importedProject)),- );- }- }-- return json(null);-};