nandi/oripublic Fork 0
7895c1d1c9bb1048807dc04f7246dc456c47e025
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

main.js · 165 lines · 4.2 KBJavaScript Blame HistoryRaw
✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme 76d62ac k33g yesterday1// 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
12const $ = (id) => document.getElementById(id);
13const 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. */
30function app() {
31 return window.go?.main?.App || null;
32}
33
34function setStatus(text, kind) {
35 els.status.textContent = text || "";
36 els.status.className = kind ? `status ${kind}` : "status";
37}
38
39function setBusy(busy) {
40 els.check.disabled = busy;
41 els.connect.disabled = busy;
42 els.url.disabled = busy;
43}
44
45function setTitle(title) {
46 if (window.runtime?.WindowSetTitle) {
47 window.runtime.WindowSetTitle(title);
48 }
49 document.title = title;
50}
51
52// ---- screens --------------------------------------------------------
53
54function 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
62function 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
74async 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
89async 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
106async 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. */
117function reloadFrame() {
118 const current = els.frame.src;
119 els.frame.src = current;
120}
121
122// ---- startup --------------------------------------------------------
123
124async 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
165document.addEventListener("DOMContentLoaded", init);