Files

264 lines
14 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--
conversation.details — the conversation's ask-form forms (the app's
FormsCard): "Forms (N)", one two-line row per form, oldest first — status
icon, title, then status pill · fields · age. A row scrolls the thread to
the form's card (P.scrollTo, else opens the form); Answer while pending,
copy the link (token included), open it. Live on the `askform` SSE event.
No form in this conversation: empty, 0 px.
-->
<style>
:root { color-scheme: light dark; }
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: transparent; }
body {
font: 13px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
color: hsl(var(--foreground, 222 47% 11%));
}
#root:empty { display: none; }
.card { border: 1px solid hsl(var(--border, 214 32% 91%)); background: hsl(var(--card, 0 0% 100%));
border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.hd { display: flex; align-items: center; gap: 6px; font-size: 10px; font-weight: 600; text-transform: uppercase;
letter-spacing: .05em; color: hsl(var(--muted-foreground, 215 16% 47%)); }
.hd svg { color: hsl(var(--primary, 221 83% 53%)); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-variant-numeric: tabular-nums; }
.list { display: flex; flex-direction: column; gap: 4px; }
.row { display: flex; align-items: center; gap: 2px; padding-right: 4px; border-radius: 6px; font-size: 12px;
border: 1px solid hsl(var(--border, 214 32% 91%) / .7); background: hsl(var(--muted, 210 40% 96%) / .2);
transition: background-color .15s; }
.row:hover { background: hsl(var(--muted, 210 40% 96%) / .5); }
.row.pending { border-color: rgb(245 158 11 / .5); }
.row.cancelled { opacity: .6; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
.main { flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; padding: 6px 8px; text-align: left; border-radius: 6px; }
.main svg { flex: none; }
.txt { flex: 1; min-width: 0; }
.title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub { display: flex; align-items: center; gap: 6px; font-size: 10px; color: hsl(var(--muted-foreground, 215 16% 47%)); }
.pill { border-radius: 4px; padding: 0 4px; font-weight: 600; }
.t-pending { color: #d97706; } :root.dark .t-pending { color: #fbbf24; }
.t-submitted { color: #10b981; }
.t-cancelled { color: #f87171; }
.p-pending { background: rgb(245 158 11 / .15); }
.p-submitted { background: rgb(16 185 129 / .15); }
.p-cancelled { background: rgb(239 68 68 / .15); }
.answer { flex: none; border-radius: 4px; padding: 2px 6px; font-size: 10px; font-weight: 600;
background: hsl(var(--primary, 221 83% 53%)); color: hsl(var(--primary-foreground, 0 0% 100%)); }
.answer:hover { opacity: .9; }
.icon { flex: none; display: inline-flex; padding: 6px; border-radius: 6px; color: hsl(var(--muted-foreground, 215 16% 47%)); }
.icon:hover { background: hsl(var(--muted, 210 40% 96%)); color: hsl(var(--foreground, 222 47% 11%)); }
.icon.ok { color: #10b981; }
</style>
</head>
<body>
<div id="root"></div>
<script>
(function () {
"use strict";
var P = window.aiAgentPlugin || {};
var init = P.init || {};
var root = document.getElementById("root");
// reuseFrame: the host keeps this frame across conversation switches; the
// session comes in init.update / onUpdate (init.sessionId on older hosts).
var sessionId = String((init.update && init.update.sessionId) || init.sessionId || "");
var LANG = "en";
function setLang(l) {
LANG = String(l || document.documentElement.lang || navigator.language || "en").slice(0, 2).toLowerCase() === "fr" ? "fr" : "en";
}
setLang(init.lang || (init.update && init.update.lang));
var FR = {
"Forms": "Formulaires", "Form": "Formulaire", "pending": "en attente", "submitted": "envoyé",
"cancelled": "annulé", "closed": "fermé", "declined": "refusé", "revoked": "révoqué", "expired": "expiré",
"{n} field": "{n} champ", "{n} fields": "{n} champs", "Answer": "Répondre",
"Show this form in the thread": "Voir ce formulaire dans le fil",
"Answer this form": "Répondre à ce formulaire", "Open this form": "Ouvrir ce formulaire",
"Answer on the form's page": "Répondre sur la page du formulaire", "Open the form's page": "Ouvrir la page du formulaire",
"Copy the form link (token included)": "Copier le lien du formulaire (jeton inclus)", "Copied": "Copié"
};
function t(s, v) {
var out = LANG === "fr" && FR[s] ? FR[s] : s;
if (v) for (var k in v) out = out.split("{" + k + "}").join(String(v[k]));
return out;
}
function esc(s) {
return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) {
return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c];
});
}
function relTime(iso) {
var ms = Date.parse(iso);
if (isNaN(ms)) return "";
var s = (ms - Date.now()) / 1000;
var rtf;
try { rtf = new Intl.RelativeTimeFormat(LANG, { numeric: "auto", style: "short" }); } catch (e) { return ""; }
var a = Math.abs(s);
if (a < 60) return rtf.format(Math.round(s), "second");
if (a < 3600) return rtf.format(Math.round(s / 60), "minute");
if (a < 86400) return rtf.format(Math.round(s / 3600), "hour");
if (a < 86400 * 30) return rtf.format(Math.round(s / 86400), "day");
return rtf.format(Math.round(s / (86400 * 30)), "month");
}
function safeLink(u) { return typeof u === "string" && /^https?:\/\//.test(u) ? u : ""; }
var ICON = {
list: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="8" y="2" width="8" height="4" rx="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><path d="M12 11h4M12 16h4M8 11h.01M8 16h.01"/></svg>',
pending: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 22h14M5 2h14M17 22v-4.172a2 2 0 0 0-.586-1.414L12 12l-4.414 4.414A2 2 0 0 0 7 17.828V22M7 2v4.172a2 2 0 0 0 .586 1.414L12 12l4.414-4.414A2 2 0 0 0 17 6.172V2"/></svg>',
submitted: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/></svg>',
cancelled: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6M9 9l6 6"/></svg>',
copy: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>',
check: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>',
ext: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h6v6M10 14 21 3M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></svg>',
max: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"/></svg>'
};
var TONE = { pending: "pending", submitted: "submitted", closed: "submitted" };
var empty = null, lastH = -1;
function report() {
var isEmpty = !root.children.length;
var h = isEmpty ? 0 : Math.ceil(root.getBoundingClientRect().height) + 2;
if (isEmpty !== empty) { empty = isEmpty; try { P.setEmpty && P.setEmpty(isEmpty); } catch (e) {} }
if (h !== lastH) { lastH = h; try { P.resize && P.resize(h); } catch (e) {} }
}
if (typeof ResizeObserver !== "undefined") new ResizeObserver(report).observe(root);
// A form can be framed in the app when its machine lists this origin; we
// can't see the host's origin from a sandbox, so a non-empty list counts.
function inApp(f) { return Array.isArray(f.embedOrigins) && f.embedOrigins.length > 0; }
var rows = [];
function toRows(forms) {
return forms
.filter(function (f) { return f && f.id && (!sessionId || f.sessionId === sessionId); })
.map(function (f) {
return {
id: String(f.id), title: f.title || t("Form"), tone: TONE[f.status] || "cancelled", status: String(f.status || ""),
fields: f.fieldCount || (Array.isArray(f.fields) ? f.fields.length : 0) || 0,
at: f.closedAt || f.createdAt || f.seenAt || "", link: safeLink(f.publicUrl) || safeLink(f.url),
page: !inApp(f), conversationId: f.conversationId || ""
};
})
.sort(function (a, b) { return a.at < b.at ? -1 : a.at > b.at ? 1 : 0; });
}
function render() {
if (!rows.length) { root.innerHTML = ""; return report(); }
var h = '<div class="card"><div class="hd">' + ICON.list + esc(t("Forms")) + ' <span class="mono">(' + rows.length + ")</span></div><div class=\"list\">";
rows.forEach(function (f, i) {
var pending = f.tone === "pending";
var openLabel = f.page ? (pending ? t("Answer on the form's page") : t("Open the form's page"))
: (pending ? t("Answer this form") : t("Open this form"));
h += '<div class="row ' + f.tone + '">'
+ '<button type="button" class="main" data-act="locate" data-i="' + i + '" title="' + esc(P.scrollTo ? t("Show this form in the thread") : openLabel) + '">'
+ '<span class="t-' + f.tone + '">' + ICON[f.tone] + "</span>"
+ '<span class="txt"><div class="title">' + esc(f.title) + '</div><div class="sub">'
+ '<span class="pill p-' + f.tone + " t-" + f.tone + '">' + esc(t(f.status) || f.status) + "</span>"
+ (f.fields > 0 ? "<span>" + esc(t(f.fields === 1 ? "{n} field" : "{n} fields", { n: f.fields })) + "</span>" : "")
+ (f.at ? "<span>" + esc(relTime(f.at)) + "</span>" : "")
+ "</div></span></button>"
+ (pending ? '<button type="button" class="answer" data-act="answer" data-i="' + i + '">' + esc(t("Answer")) + "</button>" : "")
+ (f.link ? '<button type="button" class="icon" data-act="copy" data-i="' + i + '" title="' + esc(t("Copy the form link (token included)")) + '" aria-label="' + esc(t("Copy the form link (token included)")) + '">' + ICON.copy + "</button>" : "")
+ ((!f.page || f.link) ? '<button type="button" class="icon" data-act="open" data-i="' + i + '" title="' + esc(openLabel) + '" aria-label="' + esc(openLabel) + '">' + (f.page ? ICON.ext : ICON.max) + "</button>" : "")
+ "</div>";
});
root.innerHTML = h + "</div></div>";
report();
}
function openForm(f) {
if (P.openForm) return Promise.resolve(P.openForm(f.id)).catch(function () {});
}
function locate(f) {
if (!P.scrollTo) return openForm(f);
Promise.resolve(P.scrollTo({ formId: f.id })).then(function (r) {
if (r && (r.ok === false || r.found === false)) openForm(f);
}).catch(function () { openForm(f); });
}
root.addEventListener("click", function (e) {
var b = e.target.closest && e.target.closest("button[data-act]");
if (!b) return;
var f = rows[+b.getAttribute("data-i")];
if (!f) return;
var act = b.getAttribute("data-act");
if (act === "locate") locate(f);
else if (act === "answer") (f.link ? openForm(f) : locate(f));
else if (act === "open") openForm(f);
else if (act === "copy" && P.copy) {
Promise.resolve(P.copy(f.link)).then(function () {
b.innerHTML = ICON.check; b.classList.add("ok"); b.title = t("Copied");
setTimeout(function () { b.innerHTML = ICON.copy; b.classList.remove("ok"); b.title = t("Copy the form link (token included)"); }, 1500);
}).catch(function () {});
}
});
function apply(r) {
var d = r && r.result && typeof r.result === "object" ? r.result : r;
var forms = d && Array.isArray(d.forms) ? d.forms : Array.isArray(d) ? d : [];
rows = toRows(forms);
render();
}
// P.query rides the host's shared read cache (and its batched Details
// preload: this payload is the manifest's slot "query" to the key);
// P.action is the fallback for hosts without it.
function read(payload, cb) {
if (P.query) return P.query("list", payload, cb);
var stopped = false, inflight = false, again = false;
function go() {
if (stopped || !P.action) return Promise.resolve();
if (inflight) { again = true; return Promise.resolve(); }
inflight = true;
return P.action("list", payload).then(function (r) {
if (!stopped) cb(r, { fromCache: false, fetching: false, at: Date.now() });
}, function (e) {
if (!stopped) cb(null, { error: String((e && e.message) || e) });
}).then(function () {
inflight = false;
if (again) { again = false; go(); }
});
}
go();
return { stop: function () { stopped = true; }, refresh: go };
}
var q = null, debounce = 0;
function start() {
if (q && q.stop) q.stop();
q = null;
clearTimeout(debounce);
if (!sessionId || !(P.query || P.action)) { rows = []; return render(); }
q = read({ sessionId: sessionId, limit: 200 }, function (d, meta) {
if (meta && meta.error && !d) return render();
if (d) apply(d);
});
}
function load() { if (q && q.refresh) q.refresh(); else start(); }
function soon() { clearTimeout(debounce); debounce = setTimeout(load, 250); }
if (P.onEvent) P.onEvent(function (ev) {
var type = ev && (ev.type || (ev.event && ev.event.type));
if (!type || type === "askform") soon();
});
if (P.onUpdate) P.onUpdate(function (u) {
if (!u || typeof u !== "object") return;
if (u.lang && u.lang !== LANG) { setLang(u.lang); render(); }
if (u.sessionId !== undefined && String(u.sessionId || "") !== sessionId) {
// Another conversation in the same frame: drop the last one's forms.
sessionId = String(u.sessionId || "");
rows = [];
render();
start();
}
});
// Ages move on; a form opened elsewhere without an event still shows up.
setInterval(function () { if (rows.length) render(); }, 60000);
setInterval(load, 120000);
render();
start();
})();
</script>
</body>
</html>