Archived
1
0

Simplify dashboard

This commit is contained in:
Asher
2020-03-16 12:43:32 -05:00
parent d832f61d5b
commit d192726e80
14 changed files with 205 additions and 304 deletions

View File

@ -7,8 +7,14 @@ export interface Application {
readonly icon?: string
readonly installed?: boolean
readonly name: string
/**
* Path if this is a browser app (like VS Code).
*/
readonly path?: string
readonly sessionId?: string
/**
* PID if this is a process.
*/
readonly pid?: number
readonly version?: string
}
@ -17,19 +23,18 @@ export interface ApplicationsResponse {
}
export enum SessionError {
NotFound = 4000,
FailedToStart,
Starting,
InvalidState,
Unknown,
FailedToStart = 4000,
Starting = 4001,
InvalidState = 4002,
Unknown = 4003,
}
export interface SessionResponse {
/**
* Whether the session was created or an existing one was returned.
* Whether the process was spawned or an existing one was returned.
*/
created: boolean
sessionId: string
pid: number
}
export interface RecentResponse {
@ -37,10 +42,6 @@ export interface RecentResponse {
readonly workspaces: string[]
}
export interface RunningResponse {
readonly applications: ReadonlyArray<Application>
}
export interface HealthRequest {
readonly event: "health"
}

View File

@ -17,9 +17,8 @@ export class HttpError extends Error {
export enum ApiEndpoint {
applications = "/applications",
process = "/process",
recent = "/recent",
run = "/run",
running = "/running",
session = "/session",
status = "/status",
}

View File

@ -1,10 +1,10 @@
import { logger } from "@coder/logger"
import { logger, field } from "@coder/logger"
export interface Options {
base: string
commit: string
logLevel: number
sessionId?: string
pid?: number
}
/**
@ -34,14 +34,12 @@ export const normalize = (url: string, keepTrailing = false): string => {
}
/**
* Get options embedded in the HTML from the server.
* Get options embedded in the HTML or query params.
*/
export const getOptions = <T extends Options>(): T => {
if (typeof document === "undefined") {
return {} as T
}
const el = document.getElementById("coder-options")
let options: T
try {
const el = document.getElementById("coder-options")
if (!el) {
throw new Error("no options element")
}
@ -49,19 +47,31 @@ export const getOptions = <T extends Options>(): T => {
if (!value) {
throw new Error("no options value")
}
const options = JSON.parse(value)
if (typeof options.logLevel !== "undefined") {
logger.level = options.logLevel
}
const parts = window.location.pathname.replace(/^\//g, "").split("/")
parts[parts.length - 1] = options.base
const url = new URL(window.location.origin + "/" + parts.join("/"))
return {
...options,
base: normalize(url.pathname, true),
}
options = JSON.parse(value)
} catch (error) {
logger.warn(error.message)
return {} as T
options = {} as T
}
const params = new URLSearchParams(location.search)
const queryOpts = params.get("options")
if (queryOpts) {
options = {
...options,
...JSON.parse(queryOpts),
}
}
if (typeof options.logLevel !== "undefined") {
logger.level = options.logLevel
}
if (options.base) {
const parts = location.pathname.replace(/^\//g, "").split("/")
parts[parts.length - 1] = options.base
const url = new URL(location.origin + "/" + parts.join("/"))
options.base = normalize(url.pathname, true)
}
logger.debug("got options", field("options", options))
return options
}