Skip to content

Commit

Permalink
[ALL-571] chore(frontend): Move saas-related configs to `config.jso…
Browse files Browse the repository at this point in the history
…n` (#4496)
  • Loading branch information
amanape authored Oct 21, 2024
1 parent 520586a commit 6fe5482
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 13 deletions.
3 changes: 0 additions & 3 deletions frontend/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
VITE_BACKEND_BASE_URL="localhost:3000" # Backend URL without protocol (e.g. localhost:3000)
VITE_MOCK_API="false" # true or false

# GitHub OAuth
VITE_GITHUB_CLIENT_ID=""
1 change: 1 addition & 0 deletions frontend/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
interface Window {
__APP_MODE__?: "saas" | "oss";
__GITHUB_CLIENT_ID__?: string | null;
}
3 changes: 2 additions & 1 deletion frontend/public/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"APP_MODE": "oss"
"APP_MODE": "oss",
"GITHUB_CLIENT_ID": ""
}
3 changes: 2 additions & 1 deletion frontend/src/api/open-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FeedbackResponse,
GitHubAccessTokenResponse,
ErrorResponse,
GetConfigResponse,
} from "./open-hands.types";

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ class OpenHands {
return response.json();
}

static async getConfig(): Promise<{ APP_MODE: "saas" | "oss" }> {
static async getConfig(): Promise<GetConfigResponse> {
const response = await fetch(`${OpenHands.BASE_URL}/config.json`, {
headers: {
"Cache-Control": "no-cache",
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/api/open-hands.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ export interface Feedback {
permissions: "public" | "private";
trajectory: unknown[];
}

export interface GetConfigResponse {
APP_MODE: "saas" | "oss";
GITHUB_CLIENT_ID: string | null;
}
2 changes: 1 addition & 1 deletion frontend/src/components/modals/connect-to-github-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function ConnectToGitHubModal({ onClose }: ConnectToGitHubModalProps) {
<span>
Get your token{" "}
<a
href="https://github.com/settings/tokens/new?description=openhands-app&scopes=repo,user"
href="https://github.com/settings/tokens/new?description=openhands-app&scopes=repo,user,workflow"
target="_blank"
rel="noreferrer noopener"
className="text-[#791B80] underline"
Expand Down
15 changes: 8 additions & 7 deletions frontend/src/routes/_oh._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
}
}

const clientId = import.meta.env.VITE_GITHUB_CLIENT_ID;
const requestUrl = new URL(request.url);
const redirectUri = `${requestUrl.origin}/oauth/github/callback`;
const githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=repo,user,workflow`;
let githubAuthUrl: string | null = null;
if (window.__APP_MODE__ === "saas") {
const clientId = window.__GITHUB_CLIENT_ID__;
const requestUrl = new URL(request.url);
const redirectUri = `${requestUrl.origin}/oauth/github/callback`;
githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=repo,user,workflow`;
}

return json({ repositories, githubAuthUrl });
};
Expand All @@ -110,9 +113,7 @@ function Home() {
const { files } = useSelector((state: RootState) => state.initalQuery);

const handleConnectToGitHub = () => {
const isSaas = window.__APP_MODE__ === "saas";

if (isSaas) {
if (githubAuthUrl) {
window.location.href = githubAuthUrl;
} else {
setConnectToGitHubModalOpen(true);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/routes/_oh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export const clientLoader = async () => {
try {
const config = await OpenHands.getConfig();
window.__APP_MODE__ = config.APP_MODE;
window.__GITHUB_CLIENT_ID__ = config.GITHUB_CLIENT_ID;
} catch (error) {
window.__APP_MODE__ = "oss";
window.__GITHUB_CLIENT_ID__ = null;
}

let token = localStorage.getItem("token");
Expand Down

0 comments on commit 6fe5482

Please sign in to comment.