71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
////
|
|
// Each browser tab is a shell session. This means you can run multiple sessions
|
|
// in the same browser.
|
|
|
|
import type { SessionStartMessage, SessionUpdateMessage } from "@/shared/types"
|
|
import { randomId } from "../shared/utils"
|
|
import { apps } from "./webapp"
|
|
import { $ } from "./dom"
|
|
import { mode } from "./commands"
|
|
|
|
export const sessionId = randomId()
|
|
export const projectName = $("project-name") as HTMLAnchorElement
|
|
export const projectCwd = $("project-cwd") as HTMLAnchorElement
|
|
export const projectWww = $("project-www") as HTMLAnchorElement
|
|
export const sessionStore = new Map<string, string>()
|
|
|
|
export function initSession() {
|
|
window.addEventListener("apps:change", e =>
|
|
updateWww(sessionStore.get("project") || "root")
|
|
)
|
|
}
|
|
|
|
export function handleSessionStart(msg: SessionStartMessage) {
|
|
sessionStore.set("NOSE_DIR", msg.data.NOSE_DIR)
|
|
sessionStore.set("hostname", msg.data.hostname)
|
|
updateProjectName(msg.data.project)
|
|
updateCwd(msg.data.cwd)
|
|
mode(msg.data.mode)
|
|
}
|
|
|
|
export function handleSessionUpdate(msg: SessionUpdateMessage) {
|
|
const data = msg.data as Record<string, string>
|
|
|
|
if (data.project)
|
|
updateProjectName(data.project)
|
|
|
|
if (data.cwd)
|
|
updateCwd(data.cwd)
|
|
}
|
|
|
|
function updateProjectName(project: string) {
|
|
sessionStore.set("project", project)
|
|
projectName.textContent = project
|
|
updateWww(project)
|
|
updateCwd("/")
|
|
}
|
|
|
|
function updateCwd(cwd: string) {
|
|
cwd = displayProjectPath(cwd)
|
|
sessionStore.set("cwd", cwd)
|
|
projectCwd.textContent = cwd
|
|
}
|
|
|
|
function updateWww(project: string) {
|
|
if (!apps.includes(project)) {
|
|
projectWww.style.display = "none"
|
|
return
|
|
}
|
|
|
|
projectWww.style.display = ""
|
|
const hostname = sessionStore.get("hostname") || "localhost"
|
|
const s = hostname.startsWith("localhost") ? "" : "s"
|
|
projectWww.href = `http${s}://${project}.${hostname}`
|
|
}
|
|
|
|
function displayProjectPath(path: string): string {
|
|
let prefix = sessionStore.get("NOSE_DIR") || ""
|
|
prefix += "/" + sessionStore.get("project")
|
|
|
|
return path.replace(prefix, "") || "/"
|
|
} |