// Honcho admin config page (page_id `config`, admin_only). // // The plugin's dedicated admin surface, richer than the generic // `#plugin-detail` form: connection config + a "Test connection" check against // the *current draft* before saving. Persistence reuses the core plugin // endpoints — `GET /api/plugins` to read the row, `PUT /api/plugins/honcho` to // save `{enabled, config}` — so nothing is stored through this fragment's own // backend. Default-exports the element class; the host registers it. import { html, nothing } from 'lit'; import { HonchoBase, jf, t } from './common.js'; const P = 'plugin.honcho'; const ID = 'honcho'; export default class HonchoConfigPage extends HonchoBase { static get properties() { return { _plugin: { state: true }, // PluginInfo | null _draft: { state: true }, // { base_url, api_key, workspace_id } _status: { state: true }, // { ok?, err? } for save _test: { state: true }, // { busy?, ok?, err? } for the connection test _error: { state: true }, _loading: { state: true }, }; } constructor() { super(); this._plugin = null; this._draft = { base_url: '', api_key: '', workspace_id: '' }; this._status = {}; this._test = {}; this._error = null; this._loading = true; } connectedCallback() { super.connectedCallback(); this._load(); } async _load() { this._loading = true; this._error = null; try { const all = await jf('/api/plugins'); const p = (all ?? []).find(x => x.id === ID) ?? null; if (!p) { this._error = t(`${P}.config.not_found`); this._plugin = null; return; } this._plugin = p; this._draft = { base_url: p.config?.base_url ?? '', api_key: p.config?.api_key ?? '', workspace_id: p.config?.workspace_id ?? '', }; } catch (e) { this._error = e.message; } finally { this._loading = false; } } _set(key, value) { this._draft = { ...this._draft, [key]: value }; this._status = {}; this._test = {}; } async _save(enabled) { this._status = {}; if (!this._draft.base_url?.trim()) { this._status = { err: t(`${P}.config.required`) }; return; } try { await jf(`/api/plugins/${ID}`, { method: 'PUT', body: JSON.stringify({ enabled, config: this._draft }), }); this._status = { ok: t(`${P}.config.saved`) }; await this._load(); window.dispatchEvent(new CustomEvent('plugins-changed')); } catch (e) { this._status = { err: e.message }; } } async _testConnection() { this._test = { busy: true }; try { const r = await jf(`${this.api}/admin/test`, { method: 'POST', body: JSON.stringify({ base_url: this._draft.base_url, api_key: this._draft.api_key }), }); this._test = { ok: t(`${P}.config.test_ok`, { n: r?.workspaces ?? 0 }) }; } catch (e) { this._test = { err: e.message }; } } render() { const p = this._plugin; return html`
${t(`${P}.config.intro`)}