From 4cc181cedc6029cc4d7f00903568b92dffaa3a3d Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 5 Feb 2020 17:30:09 -0600 Subject: [PATCH] Make routing base path agnostic --- src/browser/api.ts | 10 +-- src/browser/app.tsx | 35 +++++--- src/browser/components/modal.tsx | 1 - src/browser/index.html | 12 +-- src/common/api.ts | 5 ++ src/common/util.ts | 5 +- src/node/api/server.ts | 80 ++++++++--------- src/node/app/server.tsx | 69 ++++++++------- src/node/http.ts | 126 ++++++++++++--------------- src/node/vscode/error.html | 7 +- src/node/vscode/server.ts | 55 ++++-------- src/node/vscode/workbench-build.html | 8 +- src/node/vscode/workbench.html | 6 +- 13 files changed, 198 insertions(+), 221 deletions(-) diff --git a/src/browser/api.ts b/src/browser/api.ts index 390dd74..77e76ca 100644 --- a/src/browser/api.ts +++ b/src/browser/api.ts @@ -1,3 +1,4 @@ +import { getBasepath } from "hookrouter" import { Application, ApplicationsResponse, CreateSessionResponse, FilesResponse, RecentResponse } from "../common/api" import { ApiEndpoint, HttpCode, HttpError } from "../common/http" @@ -18,7 +19,7 @@ export function setAuthed(authed: boolean): void { * Also set authed to false if the request returns unauthorized. */ const tryRequest = async (endpoint: string, options?: RequestInit): Promise => { - const response = await fetch("/api" + endpoint + "/", options) + const response = await fetch(getBasepath() + "/api" + endpoint + "/", options) if (response.status === HttpCode.Unauthorized) { setAuthed(false) } @@ -33,14 +34,9 @@ const tryRequest = async (endpoint: string, options?: RequestInit): Promise => { - let formBody: URLSearchParams | undefined - if (body) { - formBody = new URLSearchParams() - formBody.append("password", body.password) - } const response = await tryRequest(ApiEndpoint.login, { method: "POST", - body: formBody, + body: JSON.stringify({ ...body, basePath: getBasepath() }), headers: { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", }, diff --git a/src/browser/app.tsx b/src/browser/app.tsx index 2f12489..b3f1271 100644 --- a/src/browser/app.tsx +++ b/src/browser/app.tsx @@ -1,4 +1,4 @@ -import { getBasepath, navigate } from "hookrouter" +import { getBasepath, navigate, setBasepath } from "hookrouter" import * as React from "react" import { Application, isExecutableApplication } from "../common/api" import { HttpError } from "../common/http" @@ -11,25 +11,36 @@ export interface AppProps { } const App: React.FunctionComponent = (props) => { - const [authed, setAuthed] = React.useState(!!props.options.authed) + const [authed, setAuthed] = React.useState(props.options.authed) const [app, setApp] = React.useState(props.options.app) const [error, setError] = React.useState() + if (typeof window !== "undefined") { + const url = new URL(window.location.origin + window.location.pathname + props.options.basePath) + setBasepath(normalize(url.pathname)) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(window as any).setAuthed = (a: boolean): void => { + if (authed !== a) { + setAuthed(a) + // TEMP: Remove when no longer auto-loading VS Code. + if (a && !app) { + setApp({ + name: "VS Code", + path: "/", + embedPath: "/vscode-embed", + }) + } + } + } + } + React.useEffect(() => { if (app && !isExecutableApplication(app)) { navigate(normalize(`${getBasepath()}/${app.path}/`, true)) } }, [app]) - if (typeof window !== "undefined") { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ;(window as any).setAuthed = (a: boolean): void => { - if (authed !== a) { - setAuthed(a) - } - } - } - return ( <> {!app || !app.loaded ? ( @@ -41,7 +52,7 @@ const App: React.FunctionComponent = (props) => { )} {authed && app && app.embedPath ? ( - + ) : ( undefined )} diff --git a/src/browser/components/modal.tsx b/src/browser/components/modal.tsx index d57afc1..df8c188 100644 --- a/src/browser/components/modal.tsx +++ b/src/browser/components/modal.tsx @@ -128,7 +128,6 @@ export const Modal: React.FunctionComponent = (props) => {