nandi/oripublic Fork 0
b6cd929ab1425a4ef6073ea9c189917cca02cf3c
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
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// ori-desktop frontend — plain JavaScript, no bundler.
//
// The Wails runtime injects two globals into this page:
//   window.go.main.App.*  — the Go methods bound in main.go (each returns a Promise)
//   window.runtime.*      — the Wails runtime API (window title, browser, ...)
// Calling them directly (rather than through the generated frontend/wailsjs
// wrappers) keeps this frontend free of generated files.
//
// Loaded as an ES module (see index.html): strict mode and a private scope
// come for free, and the script runs once the document has been parsed.

const $ = (id) => document.getElementById(id);
const els = {
	connectScreen: $("connect-screen"),
	viewerScreen: $("viewer-screen"),
	form: $("connect-form"),
	url: $("server-url"),
	check: $("check-btn"),
	connect: $("connect-btn"),
	status: $("status"),
	settingsPath: $("settings-path"),
	viewerUrl: $("viewer-url"),
	reload: $("reload-btn"),
	browser: $("browser-btn"),
	disconnect: $("disconnect-btn"),
	frame: $("ori-frame"),
};

/** The Go-bound App, or null when the page is opened outside Wails. */
function app() {
	return window.go?.main?.App || null;
}

function setStatus(text, kind) {
	els.status.textContent = text || "";
	els.status.className = kind ? `status ${kind}` : "status";
}

function setBusy(busy) {
	els.check.disabled = busy;
	els.connect.disabled = busy;
	els.url.disabled = busy;
}

function setTitle(title) {
	if (window.runtime?.WindowSetTitle) {
		window.runtime.WindowSetTitle(title);
	}
	document.title = title;
}

// ---- screens --------------------------------------------------------

function showViewer(baseURL) {
	els.viewerUrl.textContent = baseURL;
	els.frame.src = `${baseURL}/`;
	els.connectScreen.hidden = true;
	els.viewerScreen.hidden = false;
	setTitle(`ori — ${baseURL}`);
}

function showConnect(message, kind) {
	els.frame.src = "about:blank";
	els.viewerScreen.hidden = true;
	els.connectScreen.hidden = false;
	setTitle("ori");
	setStatus(message, kind);
	els.url.focus();
	els.url.select();
}

// ---- actions --------------------------------------------------------

async function check() {
	const a = app();
	if (!a) return;
	setBusy(true);
	setStatus(`Checking ${els.url.value.trim()}/healthz …`);
	try {
		const rep = await a.CheckHealth(els.url.value);
		setStatus(rep.detail, rep.ok ? "ok" : "err");
	} catch (err) {
		setStatus(String(err), "err");
	} finally {
		setBusy(false);
	}
}

async function connect() {
	const a = app();
	if (!a) return;
	setBusy(true);
	setStatus("Connecting …");
	try {
		const base = await a.Connect(els.url.value);
		els.url.value = base;
		setStatus("");
		showViewer(base);
	} catch (err) {
		setStatus(String(err), "err");
	} finally {
		setBusy(false);
	}
}

async function openInBrowser() {
	const a = app();
	if (!a) return;
	try {
		await a.OpenInBrowser(els.viewerUrl.textContent || els.url.value);
	} catch (err) {
		console.error(err);
	}
}

/** Reloads the embedded ori page by re-assigning the frame's URL. */
function reloadFrame() {
	const current = els.frame.src;
	els.frame.src = current;
}

// ---- startup --------------------------------------------------------

async function init() {
	els.form.addEventListener("submit", (e) => {
		e.preventDefault();
		connect();
	});
	els.check.addEventListener("click", check);
	els.reload.addEventListener("click", reloadFrame);
	els.browser.addEventListener("click", openInBrowser);
	els.disconnect.addEventListener("click", () =>
		showConnect("Disconnected.", ""),
	);

	const a = app();
	if (!a) {
		setStatus(
			"Wails runtime not found — open this page from the ori-desktop app (wails dev / wails build).",
			"err",
		);
		setBusy(true);
		return;
	}

	try {
		const cfg = await a.GetConfig();
		els.url.value = cfg.serverUrl || "http://localhost:8888";
	} catch (err) {
		els.url.value = "http://localhost:8888";
		setStatus(`Could not read saved settings: ${err}`, "err");
	}
	try {
		const path = await a.SettingsPath();
		els.settingsPath.textContent = path ? `Remembered in ${path}` : "";
	} catch (_) {
		/* cosmetic only */
	}

	// Auto-connect when the remembered server already answers; otherwise
	// stay on this screen with the reason, so the user can fix the URL.
	await connect();
}

document.addEventListener("DOMContentLoaded", init);