Skip to content

πŸ™Œ OpenHands: AI-Driven Development

feat(frontend): Make loader synchronous (#4689)

sp.wack387c8f1df336
diff --git a/frontend/src/components/github-repositories-suggestion-box.tsx b/frontend/src/components/github-repositories-suggestion-box.tsx
new file mode 100644
index 0000000000..c39abe11a9
--- /dev/null
+++ b/frontend/src/components/github-repositories-suggestion-box.tsx
@@ -0,0 +1,94 @@
+import React from "react";
+import {
+ isGitHubErrorReponse,
+ retrieveAllGitHubUserRepositories,
+} from "#/api/github";
+import { SuggestionBox } from "#/routes/_oh._index/suggestion-box";
+import { ConnectToGitHubModal } from "./modals/connect-to-github-modal";
+import { ModalBackdrop } from "./modals/modal-backdrop";
+import { GitHubRepositorySelector } from "#/routes/_oh._index/github-repo-selector";
+import ModalButton from "./buttons/ModalButton";
+import GitHubLogo from "#/assets/branding/github-logo.svg?react";
+
+interface GitHubAuthProps {
+ onConnectToGitHub: () => void;
+ repositories: GitHubRepository[];
+ isLoggedIn: boolean;
+}
+
+function GitHubAuth({
+ onConnectToGitHub,
+ repositories,
+ isLoggedIn,
+}: GitHubAuthProps) {
+ if (isLoggedIn) {
+ return <GitHubRepositorySelector repositories={repositories} />;
+ }
+
+ return (
+ <ModalButton
+ text="Connect to GitHub"
+ icon={<GitHubLogo width={20} height={20} />}
+ className="bg-[#791B80] w-full"
+ onClick={onConnectToGitHub}
+ />
+ );
+}
+
+interface GitHubRepositoriesSuggestionBoxProps {
+ repositories: Awaited<
+ ReturnType<typeof retrieveAllGitHubUserRepositories>
+ > | null;
+ gitHubAuthUrl: string | null;
+ user: GitHubErrorReponse | GitHubUser | null;
+}
+
+export function GitHubRepositoriesSuggestionBox({
+ repositories,
+ gitHubAuthUrl,
+ user,
+}: GitHubRepositoriesSuggestionBoxProps) {
+ const [connectToGitHubModalOpen, setConnectToGitHubModalOpen] =
+ React.useState(false);
+
+ const handleConnectToGitHub = () => {
+ if (gitHubAuthUrl) {
+ window.location.href = gitHubAuthUrl;
+ } else {
+ setConnectToGitHubModalOpen(true);
+ }
+ };
+
+ if (isGitHubErrorReponse(repositories)) {
+ return (
+ <SuggestionBox
+ title="Error Fetching Repositories"
+ content={
+ <p className="text-danger text-center">{repositories.message}</p>
+ }
+ />
+ );
+ }
+
+ return (
+ <>
+ <SuggestionBox
+ title="Open a Repo"
+ content={
+ <GitHubAuth
+ isLoggedIn={!!user && !isGitHubErrorReponse(user)}
+ repositories={repositories || []}
+ onConnectToGitHub={handleConnectToGitHub}
+ />
+ }
+ />
+ {connectToGitHubModalOpen && (
+ <ModalBackdrop onClose={() => setConnectToGitHubModalOpen(false)}>
+ <ConnectToGitHubModal
+ onClose={() => setConnectToGitHubModalOpen(false)}
+ />
+ </ModalBackdrop>
+ )}
+ </>
+ );
+}
diff --git a/frontend/src/mocks/handlers.ts b/frontend/src/mocks/handlers.ts
index 91a1c5d7ac..bde27d21f7 100644
--- a/frontend/src/mocks/handlers.ts
+++ b/frontend/src/mocks/handlers.ts
@@ -70,7 +70,9 @@ const openHandsHandlers = [
export const handlers = [
...openHandsHandlers,
- http.get("https://api.github.com/user/repos", ({ request }) => {
+ http.get("https://api.github.com/user/repos", async ({ request }) => {
+ await delay(3500);
+
const token = request.headers
.get("Authorization")
?.replace("Bearer", "")
diff --git a/frontend/src/routes/_oh._index/route.tsx b/frontend/src/routes/_oh._index/route.tsx
index edc22c6dfc..b2285e6011 100644
--- a/frontend/src/routes/_oh._index/route.tsx
+++ b/frontend/src/routes/_oh._index/route.tsx
@@ -1,54 +1,23 @@
import {
+ Await,
ClientActionFunctionArgs,
ClientLoaderFunctionArgs,
- json,
+ defer,
redirect,
useLoaderData,
useRouteLoaderData,
} from "@remix-run/react";
-import React from "react";
+import React, { Suspense } from "react";
import { SuggestionBox } from "./suggestion-box";
import { TaskForm } from "./task-form";
import { HeroHeading } from "./hero-heading";
-import { GitHubRepositorySelector } from "./github-repo-selector";
-import {
- isGitHubErrorReponse,
- retrieveAllGitHubUserRepositories,
-} from "#/api/github";
-import ModalButton from "#/components/buttons/ModalButton";
-import GitHubLogo from "#/assets/branding/github-logo.svg?react";
-import { ConnectToGitHubModal } from "#/components/modals/connect-to-github-modal";
-import { ModalBackdrop } from "#/components/modals/modal-backdrop";
+import { retrieveAllGitHubUserRepositories } from "#/api/github";
import store from "#/store";
import { setInitialQuery } from "#/state/initial-query-slice";
import { clientLoader as rootClientLoader } from "#/routes/_oh";
import OpenHands from "#/api/open-hands";
import { generateGitHubAuthUrl } from "#/utils/generate-github-auth-url";
-
-interface GitHubAuthProps {
- onConnectToGitHub: () => void;
- repositories: GitHubRepository[];
- isLoggedIn: boolean;
-}
-
-function GitHubAuth({
- onConnectToGitHub,
- repositories,
- isLoggedIn,
-}: GitHubAuthProps) {
- if (isLoggedIn) {
- return <GitHubRepositorySelector repositories={repositories} />;
- }
-
- return (
- <ModalButton
- text="Connect to GitHub"
- icon={<GitHubLogo width={20} height={20} />}
- className="bg-[#791B80] w-full"
- onClick={onConnectToGitHub}
- />
- );
-}
+import { GitHubRepositoriesSuggestionBox } from "#/components/github-repositories-suggestion-box";
export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
let isSaas = false;
@@ -67,12 +36,12 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
const token = localStorage.getItem("token");
if (token) return redirect("/app");
- let repositories: GitHubRepository[] = [];
+ let repositories: ReturnType<
+ typeof retrieveAllGitHubUserRepositories
+ > | null = null;
if (ghToken) {
- const data = await retrieveAllGitHubUserRepositories(ghToken);
- if (!isGitHubErrorReponse(data)) {
- repositories = data;
- }
+ const data = retrieveAllGitHubUserRepositories(ghToken);
+ repositories = data;
}
let githubAuthUrl: string | null = null;
@@ -81,7 +50,7 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
githubAuthUrl = generateGitHubAuthUrl(githubClientId, requestUrl);
}
- return json({ repositories, githubAuthUrl });
+ return defer({ repositories, githubAuthUrl });
};
export const clientAction = async ({ request }: ClientActionFunctionArgs) => {
@@ -95,18 +64,8 @@ export const clientAction = async ({ request }: ClientActionFunctionArgs) => {
function Home() {
const rootData = useRouteLoaderData<typeof rootClientLoader>("routes/_oh");
const { repositories, githubAuthUrl } = useLoaderData<typeof clientLoader>();
- const [connectToGitHubModalOpen, setConnectToGitHubModalOpen] =
- React.useState(false);
const [importedFile, setImportedFile] = React.useState<File | null>(null);
- const handleConnectToGitHub = () => {
- if (githubAuthUrl) {
- window.location.href = githubAuthUrl;
- } else {
- setConnectToGitHubModalOpen(true);
- }
- };
-
return (
<div className="bg-root-secondary h-full rounded-xl flex flex-col items-center justify-center relative overflow-y-auto">
<HeroHeading />
@@ -115,18 +74,24 @@ function Home() {
<TaskForm importedProjectZip={importedFile} />
</div>
<div className="flex gap-4 w-full">
- <SuggestionBox
- title="Open a Repo"
- content={
- <GitHubAuth
- isLoggedIn={
- !!rootData?.user && !isGitHubErrorReponse(rootData.user)
- }
- repositories={repositories}
- onConnectToGitHub={handleConnectToGitHub}
+ <Suspense
+ fallback={
+ <SuggestionBox
+ title="Open a Repo"
+ content="Loading repositories..."
/>
}
- />
+ >
+ <Await resolve={repositories}>
+ {(resolvedRepositories) => (
+ <GitHubRepositoriesSuggestionBox
+ repositories={resolvedRepositories}
+ gitHubAuthUrl={githubAuthUrl}
+ user={rootData?.user || null}
+ />
+ )}
+ </Await>
+ </Suspense>
<SuggestionBox
title={importedFile ? "Project Loaded" : "+ Import Project"}
content={
@@ -159,13 +124,6 @@ function Home() {
/>
</div>
</div>
- {connectToGitHubModalOpen && (
- <ModalBackdrop onClose={() => setConnectToGitHubModalOpen(false)}>
- <ConnectToGitHubModal
- onClose={() => setConnectToGitHubModalOpen(false)}
- />
- </ModalBackdrop>
- )}
</div>
);
}