function api() {
return window.LctxApi && window.LctxApi.apiFetch ? window.LctxApi.apiFetch : null;
}
function esc(value) {
return String(value == null ? '' : value).replace(/[&<>"']/g, function (ch) {
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ch];
});
}
var MAX_ANSWER = 2000;
var FREQUENCIES = [
{ id: 'daily', label: 'Daily' },
{ id: 'weekly', label: 'A few times a week' },
{ id: 'occasionally', label: 'Occasionally' },
{ id: 'new', label: 'Just started' },
];
var QUESTIONS = [
{
id: 'use_case',
label: 'What do you use lean-ctx for?',
hint: 'The work it does for you — in your words.',
},
{
id: 'likes_most',
label: 'What do you like most about it?',
hint: 'A tool, a mode, a behaviour — whatever you would miss first.',
},
{
id: 'wishes',
label: 'What is missing?',
hint: 'The feature you keep wishing were there.',
},
];
class CockpitFeedback extends HTMLElement {
constructor() {
super();
this._answers = { use_case: '', likes_most: '', wishes: '', contact: '' };
this._frequency = '';
this._notice = null;
this._sending = false;
this._sent = false;
}
connectedCallback() {
this.render();
}
_setNotice(kind, msg) {
this._notice = { kind: kind, msg: msg };
this.render();
}
async _send() {
if (this._sending) return;
var answered = ['use_case', 'likes_most', 'wishes'].some(
function (id) { return this._answers[id].trim() !== ''; },
this
);
if (!answered) {
this._setNotice('err', 'Answer at least one question before sending.');
return;
}
var apiFetch = api();
if (!apiFetch) {
this._setNotice('err', 'Dashboard API unavailable — reload the page.');
return;
}
this._sending = true;
this.render();
try {
var res = await apiFetch('/api/feedback', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
use_case: this._answers.use_case,
likes_most: this._answers.likes_most,
wishes: this._answers.wishes,
frequency: this._frequency,
contact: this._answers.contact,
}),
});
var data = null;
try {
data = await res.json();
} catch (e) {
data = null;
}
if (res && res.ok) {
this._sent = true;
this._answers = { use_case: '', likes_most: '', wishes: '', contact: '' };
this._frequency = '';
this._notice = { kind: 'ok', msg: 'Sent — thank you.' };
} else {
var reason = (data && (data.error || data.message)) || 'the server refused it';
this._notice = { kind: 'err', msg: 'Not sent: ' + reason };
}
} catch (e) {
this._notice = { kind: 'err', msg: 'Not sent: ' + (e && e.message ? e.message : 'network error') };
} finally {
this._sending = false;
this.render();
}
}
_bind() {
var self = this;
QUESTIONS.concat([{ id: 'contact' }]).forEach(function (q) {
var el = self.querySelector('#fb-' + q.id);
if (!el) return;
el.addEventListener('input', function () {
self._answers[q.id] = el.value;
var counter = self.querySelector('#fb-count-' + q.id);
if (counter) {
var n = el.value.length;
counter.textContent = n + ' / ' + MAX_ANSWER;
counter.style.color = n > MAX_ANSWER ? 'var(--red)' : '';
}
});
});
Array.prototype.forEach.call(self.querySelectorAll('[data-freq]'), function (btn) {
btn.addEventListener('click', function () {
var id = btn.getAttribute('data-freq');
self._frequency = self._frequency === id ? '' : id;
self.render();
});
});
var send = self.querySelector('#fb-send');
if (send) send.addEventListener('click', function () { self._send(); });
var again = self.querySelector('#fb-again');
if (again) {
again.addEventListener('click', function () {
self._sent = false;
self._notice = null;
self.render();
});
}
}
render() {
if (this._sent) {
this.innerHTML =
'<div class="card">' +
'<div class="card-header"><h3>Thank you</h3></div>' +
'<p class="hs" style="margin:0 0 12px;font-size:12px;opacity:.85">' +
'Your answers are with us. They shape what gets built next.</p>' +
'<button type="button" id="fb-again" class="filter-btn">Send more</button>' +
'</div>';
this._bind();
return;
}
var self = this;
var fields = QUESTIONS.map(function (q) {
var value = self._answers[q.id];
return (
'<div style="margin-bottom:16px">' +
'<label for="fb-' + q.id + '" class="hs" ' +
'style="display:block;font-size:12px;font-weight:600;margin-bottom:2px">' +
esc(q.label) + '</label>' +
'<p class="hs" style="margin:0 0 6px;font-size:11px;opacity:.7">' + esc(q.hint) + '</p>' +
'<textarea id="fb-' + q.id + '" rows="3" ' +
'style="width:100%;background:var(--bg);color:inherit;border:1px solid var(--border);' +
'border-radius:6px;padding:8px 10px;font:inherit;font-size:12px;resize:vertical">' +
esc(value) + '</textarea>' +
'<span id="fb-count-' + q.id + '" class="hs" ' +
'style="display:block;text-align:right;font-size:10px;opacity:.55;margin-top:2px">' +
value.length + ' / ' + MAX_ANSWER + '</span>' +
'</div>'
);
}).join('');
var freqButtons = FREQUENCIES.map(function (f) {
var on = self._frequency === f.id;
return (
'<button type="button" class="filter-btn' + (on ? ' active' : '') + '" ' +
'data-freq="' + esc(f.id) + '" aria-pressed="' + (on ? 'true' : 'false') + '">' +
esc(f.label) + '</button>'
);
}).join('');
var notice = '';
if (this._notice) {
var color = this._notice.kind === 'ok' ? 'var(--green)' : 'var(--red)';
notice =
'<p class="hs" style="margin:10px 0 0;font-size:11px;color:' + color + '">' +
esc(this._notice.msg) + '</p>';
}
this.innerHTML =
'<div class="card">' +
'<div class="card-header"><h3>Tell us how you use lean-ctx</h3></div>' +
'<p class="hs" style="margin:0 0 14px;font-size:12px;opacity:.8">' +
'Four questions, all optional. What you write here decides what gets built next.</p>' +
fields +
'<div style="margin-bottom:16px">' +
'<span class="hs" style="display:block;font-size:12px;font-weight:600;margin-bottom:6px">' +
'How often do you use it?</span>' +
'<div class="filter-row" style="display:flex;gap:6px;flex-wrap:wrap">' + freqButtons + '</div>' +
'</div>' +
'<div style="margin-bottom:16px">' +
'<label for="fb-contact" class="hs" ' +
'style="display:block;font-size:12px;font-weight:600;margin-bottom:2px">' +
'Email or handle <span style="opacity:.6;font-weight:400">(optional)</span></label>' +
'<p class="hs" style="margin:0 0 6px;font-size:11px;opacity:.7">' +
'Only if you want an answer back. Leave it empty to stay anonymous.</p>' +
'<input id="fb-contact" type="text" autocomplete="off" ' +
'value="' + esc(this._answers.contact) + '" ' +
'style="width:100%;background:var(--bg);color:inherit;border:1px solid var(--border);' +
'border-radius:6px;padding:8px 10px;font:inherit;font-size:12px">' +
'</div>' +
'<p class="hs" style="margin:0 0 10px;font-size:11px;opacity:.7">' +
'Pressing send transmits these answers to leanctx.com, together with your ' +
'lean-ctx version and the anonymous installation id. Nothing else — no code, ' +
'paths, prompts or usage data — and nothing at all until you press it.</p>' +
'<button type="button" id="fb-send" class="filter-btn active"' +
(this._sending ? ' disabled' : '') + '>' +
(this._sending ? 'Sending…' : 'Send feedback') + '</button>' +
notice +
'</div>';
this._bind();
}
}
customElements.define('cockpit-feedback', CockpitFeedback);