From 83c096b974eed96e3f124470fea8997e4fbb3511 Mon Sep 17 00:00:00 2001 From: "sp.wack" <83104063+amanape@users.noreply.github.com> Date: Thu, 17 Oct 2024 18:35:21 +0400 Subject: [PATCH] [ALL-551] chore(frontend): Retrieve `APP_MODE` from the server (#4423) --- containers/app/Dockerfile | 2 +- frontend/.env.sample | 1 - frontend/global.d.ts | 3 +++ frontend/public/config.json | 3 +++ frontend/src/api/open-hands.ts | 9 +++++++++ frontend/src/routes/_oh._index/route.tsx | 2 +- frontend/src/routes/_oh.tsx | 3 +++ openhands/server/listen.py | 2 +- 8 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 frontend/global.d.ts create mode 100644 frontend/public/config.json diff --git a/containers/app/Dockerfile b/containers/app/Dockerfile index ce2c0ba75c..507f45659b 100644 --- a/containers/app/Dockerfile +++ b/containers/app/Dockerfile @@ -8,7 +8,7 @@ RUN npm install -g npm@10.5.1 RUN npm ci COPY ./frontend ./ -RUN npm run make-i18n && npm run build +RUN npm run build FROM python:3.12.3-slim AS backend-builder diff --git a/frontend/.env.sample b/frontend/.env.sample index ba82f3b80b..9d6bf87a70 100644 --- a/frontend/.env.sample +++ b/frontend/.env.sample @@ -3,4 +3,3 @@ VITE_MOCK_API="false" # true or false # GitHub OAuth VITE_GITHUB_CLIENT_ID="" -VITE_APP_MODE="oss" # "oss" or "saas" diff --git a/frontend/global.d.ts b/frontend/global.d.ts new file mode 100644 index 0000000000..e71757d809 --- /dev/null +++ b/frontend/global.d.ts @@ -0,0 +1,3 @@ +interface Window { + __APP_MODE__?: "saas" | "oss"; +} diff --git a/frontend/public/config.json b/frontend/public/config.json new file mode 100644 index 0000000000..bdc29014a4 --- /dev/null +++ b/frontend/public/config.json @@ -0,0 +1,3 @@ +{ + "APP_MODE": "oss" +} diff --git a/frontend/src/api/open-hands.ts b/frontend/src/api/open-hands.ts index a0d3087de5..be41cf7967 100644 --- a/frontend/src/api/open-hands.ts +++ b/frontend/src/api/open-hands.ts @@ -60,6 +60,15 @@ class OpenHands { return response.json(); } + static async getConfig(): Promise<{ APP_MODE: "saas" | "oss" }> { + const response = await fetch(`${OpenHands.BASE_URL}/config.json`, { + headers: { + "Cache-Control": "no-cache", + }, + }); + return response.json(); + } + /** * Retrieve the list of files available in the workspace * @param token User token provided by the server diff --git a/frontend/src/routes/_oh._index/route.tsx b/frontend/src/routes/_oh._index/route.tsx index 6cf98852e4..21b6bc1979 100644 --- a/frontend/src/routes/_oh._index/route.tsx +++ b/frontend/src/routes/_oh._index/route.tsx @@ -113,7 +113,7 @@ function Home() { const { files } = useSelector((state: RootState) => state.initalQuery); const handleConnectToGitHub = () => { - const isSaas = import.meta.env.VITE_APP_MODE === "saas"; + const isSaas = window.__APP_MODE__ === "saas"; if (isSaas) { window.location.href = githubAuthUrl; diff --git a/frontend/src/routes/_oh.tsx b/frontend/src/routes/_oh.tsx index fc9793c453..0449443dbd 100644 --- a/frontend/src/routes/_oh.tsx +++ b/frontend/src/routes/_oh.tsx @@ -26,6 +26,9 @@ import NewProjectIcon from "#/assets/new-project.svg?react"; import DocsIcon from "#/assets/docs.svg?react"; export const clientLoader = async () => { + const config = await OpenHands.getConfig(); + window.__APP_MODE__ = config.APP_MODE; + let token = localStorage.getItem("token"); const ghToken = localStorage.getItem("ghToken"); diff --git a/openhands/server/listen.py b/openhands/server/listen.py index 32c93a117e..2f47282367 100644 --- a/openhands/server/listen.py +++ b/openhands/server/listen.py @@ -798,4 +798,4 @@ def github_callback(auth_code: AuthCode): ) -app.mount('/', StaticFiles(directory='./frontend/build', html=True), name='dist') +app.mount('/', StaticFiles(directory='./frontend/build/client', html=True), name='dist')