forked from bots-garden/ori
| ✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme | 1 | // ori-desktop frontend — plain JavaScript, no bundler. |
| 2 | // | |
| 3 | // The Wails runtime injects two globals into this page: | |
| 4 | // window.go.main.App.* — the Go methods bound in main.go (each returns a Promise) | |
| 5 | // window.runtime.* — the Wails runtime API (window title, browser, ...) | |
| 6 | // Calling them directly (rather than through the generated frontend/wailsjs | |
| 7 | // wrappers) keeps this frontend free of generated files. | |
| 8 | // | |
| 9 | // Loaded as an ES module (see index.html): strict mode and a private scope | |
| 10 | // come for free, and the script runs once the document has been parsed. | |
| 11 | ||
| 12 | const $ = (id) => document.getElementById(id); | |
| 13 | const els = { | |
| 14 | connectScreen: $("connect-screen"), | |
| 15 | viewerScreen: $("viewer-screen"), | |
| 16 | form: $("connect-form"), | |
| 17 | url: $("server-url"), | |
| 18 | check: $("check-btn"), | |
| 19 | connect: $("connect-btn"), | |
| 20 | status: $("status"), | |
| 21 | settingsPath: $("settings-path"), | |
| 22 | viewerUrl: $("viewer-url"), | |
| 23 | reload: $("reload-btn"), | |
| 24 | browser: $("browser-btn"), | |
| 25 | disconnect: $("disconnect-btn"), | |
| 26 | frame: $("ori-frame"), | |
| 27 | }; | |
| 28 | ||
| 29 | /** The Go-bound App, or null when the page is opened outside Wails. */ | |
| 30 | function app() { | |
| 31 | return window.go?.main?.App || null; | |
| 32 | } | |
| 33 | ||
| 34 | function setStatus(text, kind) { | |
| 35 | els.status.textContent = text || ""; | |
| 36 | els.status.className = kind ? `status ${kind}` : "status"; | |
| 37 | } | |
| 38 | ||
| 39 | function setBusy(busy) { | |
| 40 | els.check.disabled = busy; | |
| 41 | els.connect.disabled = busy; | |
| 42 | els.url.disabled = busy; | |
| 43 | } | |
| 44 | ||
| 45 | function setTitle(title) { | |
| 46 | if (window.runtime?.WindowSetTitle) { | |
| 47 | window.runtime.WindowSetTitle(title); | |
| 48 | } | |
| 49 | document.title = title; | |
| 50 | } | |
| 51 | ||
| 52 | // ---- screens -------------------------------------------------------- | |
| 53 | ||
| 54 | function showViewer(baseURL) { | |
| 55 | els.viewerUrl.textContent = baseURL; | |
| 56 | els.frame.src = `${baseURL}/`; | |
| 57 | els.connectScreen.hidden = true; | |
| 58 | els.viewerScreen.hidden = false; | |
| 59 | setTitle(`ori — ${baseURL}`); | |
| 60 | } | |
| 61 | ||
| 62 | function showConnect(message, kind) { | |
| 63 | els.frame.src = "about:blank"; | |
| 64 | els.viewerScreen.hidden = true; | |
| 65 | els.connectScreen.hidden = false; | |
| 66 | setTitle("ori"); | |
| 67 | setStatus(message, kind); | |
| 68 | els.url.focus(); | |
| 69 | els.url.select(); | |
| 70 | } | |
| 71 | ||
| 72 | // ---- actions -------------------------------------------------------- | |
| 73 | ||
| 74 | async function check() { | |
| 75 | const a = app(); | |
| 76 | if (!a) return; | |
| 77 | setBusy(true); | |
| 78 | setStatus(`Checking ${els.url.value.trim()}/healthz …`); | |
| 79 | try { | |
| 80 | const rep = await a.CheckHealth(els.url.value); | |
| 81 | setStatus(rep.detail, rep.ok ? "ok" : "err"); | |
| 82 | } catch (err) { | |
| 83 | setStatus(String(err), "err"); | |
| 84 | } finally { | |
| 85 | setBusy(false); | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 89 | async function connect() { | |
| 90 | const a = app(); | |
| 91 | if (!a) return; | |
| 92 | setBusy(true); | |
| 93 | setStatus("Connecting …"); | |
| 94 | try { | |
| 95 | const base = await a.Connect(els.url.value); | |
| 96 | els.url.value = base; | |
| 97 | setStatus(""); | |
| 98 | showViewer(base); | |
| 99 | } catch (err) { | |
| 100 | setStatus(String(err), "err"); | |
| 101 | } finally { | |
| 102 | setBusy(false); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | async function openInBrowser() { | |
| 107 | const a = app(); | |
| 108 | if (!a) return; | |
| 109 | try { | |
| 110 | await a.OpenInBrowser(els.viewerUrl.textContent || els.url.value); | |
| 111 | } catch (err) { | |
| 112 | console.error(err); | |
| 113 | } | |
| 114 | } | |
| 115 | ||
| 116 | /** Reloads the embedded ori page by re-assigning the frame's URL. */ | |
| 117 | function reloadFrame() { | |
| 118 | const current = els.frame.src; | |
| 119 | els.frame.src = current; | |
| 120 | } | |
| 121 | ||
| 122 | // ---- startup -------------------------------------------------------- | |
| 123 | ||
| 124 | async function init() { | |
| 125 | els.form.addEventListener("submit", (e) => { | |
| 126 | e.preventDefault(); | |
| 127 | connect(); | |
| 128 | }); | |
| 129 | els.check.addEventListener("click", check); | |
| 130 | els.reload.addEventListener("click", reloadFrame); | |
| 131 | els.browser.addEventListener("click", openInBrowser); | |
| 132 | els.disconnect.addEventListener("click", () => | |
| 133 | showConnect("Disconnected.", ""), | |
| 134 | ); | |
| 135 | ||
| 136 | const a = app(); | |
| 137 | if (!a) { | |
| 138 | setStatus( | |
| 139 | "Wails runtime not found — open this page from the ori-desktop app (wails dev / wails build).", | |
| 140 | "err", | |
| 141 | ); | |
| 142 | setBusy(true); | |
| 143 | return; | |
| 144 | } | |
| 145 | ||
| 146 | try { | |
| 147 | const cfg = await a.GetConfig(); | |
| 148 | els.url.value = cfg.serverUrl || "http://localhost:8888"; | |
| 149 | } catch (err) { | |
| 150 | els.url.value = "http://localhost:8888"; | |
| 151 | setStatus(`Could not read saved settings: ${err}`, "err"); | |
| 152 | } | |
| 153 | try { | |
| 154 | const path = await a.SettingsPath(); | |
| 155 | els.settingsPath.textContent = path ? `Remembered in ${path}` : ""; | |
| 156 | } catch (_) { | |
| 157 | /* cosmetic only */ | |
| 158 | } | |
| 159 | ||
| 160 | // Auto-connect when the remembered server already answers; otherwise | |
| 161 | // stay on this screen with the reason, so the user can fix the URL. | |
| 162 | await connect(); | |
| 163 | } | |
| 164 | ||
| 165 | document.addEventListener("DOMContentLoaded", init); |