@rig-works/dashboard 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs ADDED
@@ -0,0 +1,598 @@
1
+ import { spawn } from "node:child_process";
2
+ import { readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { Hono } from "hono";
5
+ import { z } from "zod";
6
+ import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime";
7
+ // src/components/util.ts
8
+ const obj = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : {};
9
+ const arr = (v) => Array.isArray(v) ? v : [];
10
+ const str = (v) => v === void 0 || v === null ? "" : String(v);
11
+ const opt = (v) => v === void 0 || v === null ? void 0 : String(v);
12
+ /** 컨트롤 하나가 공통으로 쓰는 값. id는 화면 안 위치(k)에서 만들고, ev는 이벤트 id다(없으면 읽기 전용처럼 동작한다). */
13
+ const controlProps = (it, k) => {
14
+ const id = `c-${k.replace(/\./g, "-")}`;
15
+ return {
16
+ id,
17
+ ev: opt(it.id),
18
+ disabled: it.disabled === true,
19
+ invalid: it.error ? {
20
+ "aria-invalid": "true",
21
+ "aria-describedby": `${id}-err`
22
+ } : {}
23
+ };
24
+ };
25
+ // src/components/side/Side.tsx
26
+ /** @jsxImportSource hono/jsx */
27
+ /** shared "dashboard:page" 공간에서 화면 목록을 뽑는다. items 배열이 있는 값만 화면으로 본다. */
28
+ function pageList(space) {
29
+ return Object.entries(space).filter(([, v]) => Array.isArray(obj(v).items)).map(([key, v]) => ({
30
+ key,
31
+ title: str(obj(v).title) || key
32
+ })).sort((a, b) => a.title.localeCompare(b.title));
33
+ }
34
+ const menuItem = z.object({
35
+ name: z.string().min(1),
36
+ page: z.string().min(1),
37
+ order: z.number().optional()
38
+ });
39
+ /** shared "dashboard:menu" 공간에서 사이드바 메뉴를 뽑는다 ({ name, page, order? }). 화면(dashboard:page)과는 독립이라 없는 화면을 가리켜도 그대로 둔다. */
40
+ function menuList(space) {
41
+ return Object.values(space).flatMap((v) => {
42
+ const r = menuItem.safeParse(v);
43
+ return r.success ? [r.data] : [];
44
+ }).sort((a, b) => (a.order ?? 0) - (b.order ?? 0) || a.name.localeCompare(b.name));
45
+ }
46
+ /** 왼쪽 메뉴. 각 항목은 화면 key(page)로 가고, current는 지금 보고 있는 화면의 key. */
47
+ const Side = ({ base, menu, current }) => /* @__PURE__ */ jsxs("aside", {
48
+ class: "rig-side",
49
+ children: [/* @__PURE__ */ jsx("h2", {
50
+ class: "rig-side-title",
51
+ children: "Rig"
52
+ }), /* @__PURE__ */ jsx("nav", {
53
+ "aria-label": "메뉴",
54
+ children: /* @__PURE__ */ jsx("ul", {
55
+ class: "rig-nav-list",
56
+ children: menu.map((m) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("a", {
57
+ class: "rig-nav-item",
58
+ href: `${base}${encodeURIComponent(m.page)}`,
59
+ "aria-current": m.page === current ? "page" : void 0,
60
+ children: m.name
61
+ }) }))
62
+ })
63
+ })]
64
+ });
65
+ // src/components/page/Badge.tsx
66
+ /** @jsxImportSource hono/jsx */
67
+ const tone = (v) => [
68
+ "success",
69
+ "warning",
70
+ "danger"
71
+ ].includes(str(v)) ? ` rig-badge--${str(v)}` : "";
72
+ const Badge = ({ b }) => /* @__PURE__ */ jsx("span", {
73
+ class: `rig-badge${tone(b.tone)}`,
74
+ children: str(b.text)
75
+ });
76
+ // src/components/page/Button.tsx
77
+ /** @jsxImportSource hono/jsx */
78
+ const Button = ({ it, k }) => {
79
+ const { ev, disabled } = controlProps(it, k);
80
+ const variant = ["primary", "danger"].includes(str(it.variant)) ? ` rig-button--${str(it.variant)}` : "";
81
+ return /* @__PURE__ */ jsx("button", {
82
+ class: `rig-button${variant}`,
83
+ type: "button",
84
+ "data-ev": ev,
85
+ disabled,
86
+ children: str(it.text ?? it.label)
87
+ });
88
+ };
89
+ // src/components/page/Row.tsx
90
+ /** 설정 한 줄: 왼쪽에 라벨(옆에 부연 meta)과 설명, 오른쪽에 컨트롤. */
91
+ const Row = ({ k, it, block, labelFor, children }) => /* @__PURE__ */ jsxs("div", {
92
+ class: `rig-row${block ? " rig-row--block" : ""}`,
93
+ "data-k": k,
94
+ children: [/* @__PURE__ */ jsxs("div", {
95
+ class: "rig-row-info",
96
+ children: [/* @__PURE__ */ jsxs("div", {
97
+ class: "rig-row-head",
98
+ children: [labelFor ? /* @__PURE__ */ jsx("label", {
99
+ class: "rig-row-label",
100
+ for: labelFor,
101
+ children: str(it.label)
102
+ }) : /* @__PURE__ */ jsx("span", {
103
+ class: "rig-row-label",
104
+ children: str(it.label)
105
+ }), it.meta ? /* @__PURE__ */ jsx("span", {
106
+ class: "rig-row-meta",
107
+ children: str(it.meta)
108
+ }) : null]
109
+ }), it.description ? /* @__PURE__ */ jsx("p", {
110
+ class: "rig-row-desc",
111
+ children: str(it.description)
112
+ }) : null]
113
+ }), /* @__PURE__ */ jsx("div", {
114
+ class: "rig-row-control",
115
+ children
116
+ })]
117
+ });
118
+ /** 입력 하나를 오류 문구와 함께 감싼다. error가 없으면 그대로 둔다. */
119
+ const Field = ({ id, it, children }) => it.error ? /* @__PURE__ */ jsxs("div", {
120
+ class: "rig-field",
121
+ children: [children, /* @__PURE__ */ jsx("p", {
122
+ class: "rig-error",
123
+ id: `${id}-err`,
124
+ children: str(it.error)
125
+ })]
126
+ }) : /* @__PURE__ */ jsx(Fragment, { children });
127
+ // src/components/page/Input.tsx
128
+ /** @jsxImportSource hono/jsx */
129
+ const Input = ({ it, k }) => {
130
+ const { id, ev, disabled, invalid } = controlProps(it, k);
131
+ const type = ["number", "password"].includes(str(it.inputType)) ? str(it.inputType) : "text";
132
+ return /* @__PURE__ */ jsx(Field, {
133
+ id,
134
+ it,
135
+ children: /* @__PURE__ */ jsx("input", {
136
+ class: "rig-input",
137
+ type,
138
+ id,
139
+ "data-ev": ev,
140
+ value: opt(it.value),
141
+ placeholder: opt(it.placeholder),
142
+ min: opt(it.min),
143
+ max: opt(it.max),
144
+ pattern: opt(it.pattern),
145
+ required: it.required === true,
146
+ disabled,
147
+ ...invalid
148
+ })
149
+ });
150
+ };
151
+ // src/components/page/List.tsx
152
+ /** @jsxImportSource hono/jsx */
153
+ const List = ({ it }) => /* @__PURE__ */ jsx("ul", {
154
+ class: "rig-list",
155
+ children: arr(it.items).map((r) => {
156
+ const row = obj(r);
157
+ return /* @__PURE__ */ jsxs("li", { children: [/* @__PURE__ */ jsx("span", { children: str(row.label) }), row.badge ? /* @__PURE__ */ jsx(Badge, { b: obj(row.badge) }) : null] });
158
+ })
159
+ });
160
+ // src/components/page/Panel.tsx
161
+ /** 제목이 있는 접이식 묶음. 안의 항목은 children으로 받는다 (Item과 순환 import를 피하려고). */
162
+ const Panel = ({ it, children }) => /* @__PURE__ */ jsxs("details", {
163
+ class: "rig-section",
164
+ open: it.open !== false,
165
+ children: [
166
+ /* @__PURE__ */ jsx("summary", { children: str(it.title) }),
167
+ it.description ? /* @__PURE__ */ jsx("p", {
168
+ class: "rig-section-desc",
169
+ children: str(it.description)
170
+ }) : null,
171
+ children
172
+ ]
173
+ });
174
+ // src/components/page/Select.tsx
175
+ /** @jsxImportSource hono/jsx */
176
+ const Select = ({ it, k }) => {
177
+ const { id, ev, disabled, invalid } = controlProps(it, k);
178
+ return /* @__PURE__ */ jsx(Field, {
179
+ id,
180
+ it,
181
+ children: /* @__PURE__ */ jsx("select", {
182
+ class: "rig-select",
183
+ id,
184
+ "data-ev": ev,
185
+ disabled,
186
+ ...invalid,
187
+ children: arr(it.options).map((o) => {
188
+ const { value, label } = typeof o === "object" ? obj(o) : {
189
+ value: o,
190
+ label: o
191
+ };
192
+ return /* @__PURE__ */ jsx("option", {
193
+ value: str(value),
194
+ selected: str(value) === str(it.value),
195
+ children: str(label ?? value)
196
+ });
197
+ })
198
+ })
199
+ });
200
+ };
201
+ // src/components/page/Table.tsx
202
+ /** @jsxImportSource hono/jsx */
203
+ const Table = ({ it }) => /* @__PURE__ */ jsx("div", {
204
+ class: "rig-table-wrap",
205
+ children: /* @__PURE__ */ jsxs("table", {
206
+ class: "rig-table",
207
+ children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: arr(it.columns).map((c) => /* @__PURE__ */ jsx("th", {
208
+ scope: "col",
209
+ children: str(c)
210
+ })) }) }), /* @__PURE__ */ jsx("tbody", { children: arr(it.rows).map((r) => /* @__PURE__ */ jsx("tr", { children: arr(r).map((c) => /* @__PURE__ */ jsx("td", {
211
+ class: typeof c === "number" ? "rig-num" : void 0,
212
+ children: str(c)
213
+ })) })) })]
214
+ })
215
+ });
216
+ // src/components/page/Text.tsx
217
+ /** @jsxImportSource hono/jsx */
218
+ const Heading = ({ it, k }) => it.level === 3 ? /* @__PURE__ */ jsx("h3", {
219
+ class: "rig-heading",
220
+ "data-k": k,
221
+ children: str(it.text)
222
+ }) : /* @__PURE__ */ jsx("h2", {
223
+ class: "rig-heading",
224
+ "data-k": k,
225
+ children: str(it.text)
226
+ });
227
+ const Text = ({ it, k }) => /* @__PURE__ */ jsx("p", {
228
+ class: `rig-text${it.muted ? " rig-text--muted" : ""}`,
229
+ "data-k": k,
230
+ children: str(it.text)
231
+ });
232
+ const Divider = ({ k }) => /* @__PURE__ */ jsx("hr", {
233
+ class: "rig-divider",
234
+ "data-k": k
235
+ });
236
+ // src/components/page/Textarea.tsx
237
+ /** @jsxImportSource hono/jsx */
238
+ const Textarea = ({ it, k }) => {
239
+ const { id, ev, disabled, invalid } = controlProps(it, k);
240
+ return /* @__PURE__ */ jsx(Field, {
241
+ id,
242
+ it,
243
+ children: /* @__PURE__ */ jsx("textarea", {
244
+ class: "rig-textarea",
245
+ id,
246
+ "data-ev": ev,
247
+ rows: Number(it.rows) || 3,
248
+ required: it.required === true,
249
+ disabled,
250
+ ...invalid,
251
+ children: str(it.value)
252
+ })
253
+ });
254
+ };
255
+ // src/components/page/Toggle.tsx
256
+ /** @jsxImportSource hono/jsx */
257
+ const Toggle = ({ it, k }) => {
258
+ const { id, ev, disabled, invalid } = controlProps(it, k);
259
+ return /* @__PURE__ */ jsx(Field, {
260
+ id,
261
+ it,
262
+ children: /* @__PURE__ */ jsx("input", {
263
+ class: "rig-toggle",
264
+ type: "checkbox",
265
+ role: "switch",
266
+ id,
267
+ "data-ev": ev,
268
+ checked: it.value === true,
269
+ disabled,
270
+ ...invalid
271
+ })
272
+ });
273
+ };
274
+ // src/components/page/Page.tsx
275
+ const CONTROLS = {
276
+ toggle: Toggle,
277
+ input: Input,
278
+ textarea: Textarea,
279
+ select: Select,
280
+ button: Button,
281
+ list: List,
282
+ table: Table
283
+ };
284
+ /** 라벨이 가리킬 수 있는 입력 컨트롤. */
285
+ const LABELLED = /* @__PURE__ */ new Set([
286
+ "toggle",
287
+ "input",
288
+ "textarea",
289
+ "select"
290
+ ]);
291
+ /** 라벨 아래에 한 줄을 다 쓰는 컨트롤. */
292
+ const BLOCK = /* @__PURE__ */ new Set([
293
+ "textarea",
294
+ "list",
295
+ "table"
296
+ ]);
297
+ /** 라벨, 부연, 설명은 it에서 읽고, 오른쪽(블록이면 아래)에 컨트롤들을 놓는다. 컨트롤이 아닌 type은 건너뛴다. */
298
+ const RowItem = ({ it, k, kids }) => {
299
+ const known = kids.flatMap(([c, ck]) => {
300
+ const Control = CONTROLS[str(c.type)];
301
+ return Control ? [{
302
+ Control,
303
+ c,
304
+ ck
305
+ }] : [];
306
+ });
307
+ const first = known.find(({ c }) => LABELLED.has(str(c.type)));
308
+ return /* @__PURE__ */ jsx(Row, {
309
+ k,
310
+ it,
311
+ block: known.some(({ c }) => BLOCK.has(str(c.type))),
312
+ labelFor: first && controlProps(first.c, first.ck).id,
313
+ children: known.map(({ Control, c, ck }) => /* @__PURE__ */ jsx(Control, {
314
+ it: c,
315
+ k: ck
316
+ }))
317
+ });
318
+ };
319
+ /** 항목 JSON의 type에 맞는 컴포넌트를 고른다. */
320
+ const Item = ({ it: raw, k }) => {
321
+ const it = obj(raw);
322
+ switch (it.type) {
323
+ case "heading": return /* @__PURE__ */ jsx(Heading, {
324
+ it,
325
+ k
326
+ });
327
+ case "text": return /* @__PURE__ */ jsx(Text, {
328
+ it,
329
+ k
330
+ });
331
+ case "divider": return /* @__PURE__ */ jsx(Divider, { k });
332
+ case "badge": return /* @__PURE__ */ jsx("span", {
333
+ "data-k": k,
334
+ children: /* @__PURE__ */ jsx(Badge, { b: it })
335
+ });
336
+ case "section": return /* @__PURE__ */ jsx(Panel, {
337
+ it,
338
+ children: arr(it.items).map((c, i) => /* @__PURE__ */ jsx(Item, {
339
+ it: c,
340
+ k: `${k}.${i}`
341
+ }))
342
+ });
343
+ case "row": return /* @__PURE__ */ jsx(RowItem, {
344
+ it,
345
+ k,
346
+ kids: arr(it.items).map((c, i) => [obj(c), `${k}.${i}`])
347
+ });
348
+ default: return CONTROLS[str(it.type)] ? /* @__PURE__ */ jsx(RowItem, {
349
+ it,
350
+ k,
351
+ kids: [[it, k]]
352
+ }) : null;
353
+ }
354
+ };
355
+ /** 화면 본문. 제목, 상태 배지, 설명 아래에 항목들을 그린다. pageKey가 없으면 안내 문구를 보인다. */
356
+ const Page = ({ pageKey, page }) => {
357
+ const status = obj(page.status);
358
+ return /* @__PURE__ */ jsx("main", {
359
+ class: "rig-page",
360
+ children: pageKey ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("header", {
361
+ "data-k": "h",
362
+ style: "display:grid;gap:4px",
363
+ children: [/* @__PURE__ */ jsxs("div", {
364
+ class: "rig-actions",
365
+ style: "justify-content:space-between;align-items:center",
366
+ children: [/* @__PURE__ */ jsx("h1", {
367
+ class: "rig-heading",
368
+ children: str(page.title) || pageKey
369
+ }), status.text ? /* @__PURE__ */ jsx(Badge, { b: status }) : null]
370
+ }), page.description ? /* @__PURE__ */ jsx("p", {
371
+ class: "rig-text rig-text--muted",
372
+ children: str(page.description)
373
+ }) : null]
374
+ }), arr(page.items).map((it, i) => /* @__PURE__ */ jsx(Item, {
375
+ it,
376
+ k: String(i)
377
+ }))] }) : /* @__PURE__ */ jsx("p", {
378
+ class: "rig-text rig-text--muted",
379
+ "data-k": "empty",
380
+ children: "표시할 화면이 없습니다."
381
+ })
382
+ });
383
+ };
384
+ // src/render.tsx
385
+ /** @jsxImportSource hono/jsx */
386
+ const Shell = ({ base, space, menu, current }) => {
387
+ const pages = pageList(space);
388
+ const key = pages.find((p) => p.key === current)?.key ?? pages[0]?.key;
389
+ const page = key ? obj(space[key]) : {};
390
+ return /* @__PURE__ */ jsxs("html", {
391
+ lang: "ko",
392
+ children: [/* @__PURE__ */ jsxs("head", { children: [
393
+ /* @__PURE__ */ jsx("meta", { charset: "utf-8" }),
394
+ /* @__PURE__ */ jsx("meta", {
395
+ name: "viewport",
396
+ content: "width=device-width, initial-scale=1"
397
+ }),
398
+ /* @__PURE__ */ jsx("title", { children: key ? `${str(page.title) || key} — Rig` : "Rig" }),
399
+ /* @__PURE__ */ jsx("link", {
400
+ rel: "stylesheet",
401
+ href: `${base}rig-ui.css`
402
+ })
403
+ ] }), /* @__PURE__ */ jsxs("body", {
404
+ class: "rig-shell",
405
+ "data-page": key,
406
+ "data-base": base,
407
+ children: [
408
+ /* @__PURE__ */ jsx(Side, {
409
+ base,
410
+ menu: menuList(menu),
411
+ current: key
412
+ }),
413
+ /* @__PURE__ */ jsx(Page, {
414
+ pageKey: key,
415
+ page
416
+ }),
417
+ /* @__PURE__ */ jsx("script", {
418
+ src: `${base}client.js`,
419
+ defer: true
420
+ })
421
+ ]
422
+ })]
423
+ });
424
+ };
425
+ // src/plugins-page.ts
426
+ const plugins = [
427
+ {
428
+ id: "systray",
429
+ name: "Systray",
430
+ version: "0.0.0",
431
+ author: "buwon",
432
+ description: "시스템 트레이 메뉴",
433
+ active: true
434
+ },
435
+ {
436
+ id: "browser",
437
+ name: "Browser",
438
+ version: "0.0.0",
439
+ author: "buwon",
440
+ description: "브라우저 자동화 (stub)",
441
+ active: true
442
+ },
443
+ {
444
+ id: "dashboard",
445
+ name: "Dashboard",
446
+ version: "0.0.0",
447
+ author: "buwon",
448
+ description: "선언형 플러그인 화면",
449
+ active: true
450
+ },
451
+ {
452
+ id: "storage",
453
+ name: "Storage",
454
+ version: "0.0.0",
455
+ author: "buwon",
456
+ description: "플러그인 데이터 저장소",
457
+ active: false
458
+ }
459
+ ];
460
+ const pluginsPage = {
461
+ title: "Plugins",
462
+ description: "설치된 플러그인을 켜고 끕니다.",
463
+ status: {
464
+ text: `${plugins.filter((p) => p.active).length}/${plugins.length} 활성`,
465
+ tone: "success"
466
+ },
467
+ items: [{
468
+ type: "section",
469
+ title: "설치된 플러그인",
470
+ description: `플러그인 ${plugins.length}개`,
471
+ items: plugins.map((p) => ({
472
+ type: "row",
473
+ label: p.name,
474
+ meta: `v${p.version} · ${p.author}`,
475
+ description: p.description,
476
+ items: [{
477
+ type: "toggle",
478
+ id: `toggle-${p.id}`,
479
+ value: p.active
480
+ }, {
481
+ type: "button",
482
+ id: `remove-${p.id}`,
483
+ text: "삭제",
484
+ variant: "danger",
485
+ disabled: p.active
486
+ }]
487
+ }))
488
+ }]
489
+ };
490
+ // src/index.ts
491
+ const PATH = "/dashboard/";
492
+ const read = (f) => readFileSync(join(import.meta.dirname, f), "utf8");
493
+ const name = z.string().regex(/^[A-Za-z0-9_.-]{1,64}$/);
494
+ const eventBody = z.object({
495
+ page: name,
496
+ id: name,
497
+ value: z.unknown().optional()
498
+ });
499
+ const SETTLE_MS = 1e3;
500
+ const plugin = (ctx) => {
501
+ const css = read("rig-ui.css");
502
+ const js = read("client.js");
503
+ const clients = /* @__PURE__ */ new Set();
504
+ const send = (chunk) => {
505
+ for (const c of [...clients]) try {
506
+ c.enqueue(chunk);
507
+ } catch {
508
+ clients.delete(c);
509
+ }
510
+ };
511
+ const changed = ctx.timer.debounce(() => send("data: changed\n\n"), SETTLE_MS);
512
+ ctx.signal.on("shared:dashboard:page", changed);
513
+ ctx.signal.on("shared:dashboard:menu", changed);
514
+ ctx.timer.setInterval(() => send(": ping\n\n"), 5e3);
515
+ const app = new Hono();
516
+ const text = (body, type) => new Response(body, { headers: {
517
+ "content-type": `${type}; charset=utf-8`,
518
+ "cache-control": "no-cache"
519
+ } });
520
+ app.get("/rig-ui.css", () => text(css, "text/css"));
521
+ app.get("/client.js", () => text(js, "text/javascript"));
522
+ app.get("/api/events", () => {
523
+ let self;
524
+ const stream = new ReadableStream({
525
+ start(controller) {
526
+ self = controller;
527
+ clients.add(controller);
528
+ controller.enqueue(": ok\n\n");
529
+ },
530
+ cancel() {
531
+ clients.delete(self);
532
+ }
533
+ });
534
+ return new Response(stream.pipeThrough(new TextEncoderStream()), { headers: {
535
+ "content-type": "text/event-stream",
536
+ "cache-control": "no-cache"
537
+ } });
538
+ });
539
+ app.post("/api/event", async (c) => {
540
+ const r = eventBody.safeParse(await c.req.json().catch(() => null));
541
+ if (!r.success) return c.json({ error: "bad request" }, 400);
542
+ const b = r.data;
543
+ const owner = await ctx.shared.owner("dashboard:page", b.page);
544
+ if (!owner) return c.json({ error: "not found" }, 404);
545
+ try {
546
+ ctx.signal.emit(`${owner}:${b.page}:${b.id}`, { value: b.value ?? null });
547
+ } catch (e) {
548
+ ctx.log.error("cannot emit ui event:", e);
549
+ return c.json({ error: "bad request" }, 400);
550
+ }
551
+ return c.body(null, 204);
552
+ });
553
+ const page = async (current) => {
554
+ const space = await ctx.shared.get("dashboard:page");
555
+ const menu = await ctx.shared.get("dashboard:menu");
556
+ if (current !== void 0 && !pageList(space).some((p) => p.key === current)) return new Response("not found", { status: 404 });
557
+ const { host, port } = await ctx.shared.get("http");
558
+ const base = host && port ? `http://${host}:${port}${PATH}` : PATH;
559
+ return new Response("<!doctype html>" + await Shell({
560
+ base,
561
+ space,
562
+ menu,
563
+ current
564
+ }).toString(), { headers: { "content-type": "text/html; charset=utf-8" } });
565
+ };
566
+ app.get("/", () => page());
567
+ app.get("/:page", (c) => page(c.req.param("page")));
568
+ ctx.http.fetch((req) => app.fetch(req));
569
+ ctx.shared.set("http", "/", "/dashboard").catch((e) => ctx.log.error("cannot set home page:", e));
570
+ ctx.shared.set("dashboard:page", "plugins", pluginsPage).catch((e) => ctx.log.error("cannot set plugins page:", e));
571
+ ctx.shared.set("tray:menu", "dashboard", {
572
+ id: "dashboard",
573
+ name: "Dashboard",
574
+ signal: "dashboard:open",
575
+ order: 100
576
+ }).catch((e) => ctx.log.error("cannot add tray item:", e));
577
+ ctx.signal.on("dashboard:open", async () => {
578
+ const { host, port } = await ctx.shared.get("http");
579
+ if (!host || !port) return ctx.log.error("cannot open browser: shared \"http\" has no host/port");
580
+ const url = `http://${host}:${port}${PATH}`;
581
+ const [cmd, ...args] = process.platform === "win32" ? [
582
+ "cmd",
583
+ "/c",
584
+ "start",
585
+ "",
586
+ url
587
+ ] : process.platform === "darwin" ? ["open", url] : ["xdg-open", url];
588
+ spawn(cmd, args, {
589
+ stdio: "ignore",
590
+ detached: true
591
+ }).on("error", (e) => ctx.log.error("cannot open browser:", e)).unref();
592
+ });
593
+ return () => {
594
+ for (const c of clients) c.close();
595
+ clients.clear();
596
+ };
597
+ };
598
+ export { plugin as default };