Frontend: scroll fix, SSE reconnect, toast global, stale states, theming
5 bloques en una pasada:
A. Scroll fixes (síntoma reportado por el usuario):
- run-timeline: nuevo flag _userScrolledUp con detección por rAF en
bindScroll. Auto-scroll al final SOLO si el usuario está abajo
(umbral 150px) o si él mismo disparó un optimistic bubble. Cuando
el usuario manda mensaje, se resetea el flag y se scrollea.
- conversation-inspector: mismo patrón. ui:chatScroll respeta el
flag userScrolledUp del emisor para no perseguir al usuario que
lee arriba.
- .bubble: + min-width:0, overflow-wrap:anywhere para URLs/JSON
largos sin espacios.
- <pre>: + overflow-x:auto, max-width:100%.
- chat-simulator: textarea con resize:vertical, min/max heights
viewport-friendly. inputs-col con overflow-y:auto.
B. Stale state options:
- conversation-list y conversations-crud: dropdowns ahora muestran
IDLE / CART / SHIPPING / AWAITING_HUMAN. Quitados BROWSING,
BUILDING_ORDER, WAITING_ADDRESS, WAITING_PAYMENT, COMPLETED.
- main.js: simulated plan.next_state pasa a CART.
C. SSE resilience (lib/sse.js):
- connect() con backoff exponencial (1s → 30s).
- safeParse() helper: cada evento envuelve JSON.parse en try-catch
para que un payload malformado no rompa otros listeners.
- reset retryDelay al primer "hello" exitoso.
- ops-shell: indicador con dot (verde "En vivo" / naranja pulsante
"Reconectando…") en lugar de texto plano.
D. Toast service global (public/lib/toast.js):
- API simple: toast({ kind, text, ms }). Apila, animación slide-in,
auto-dismiss 4s. Click para cerrar.
- safeFetch en api.js: wrapper que dispara toast en network error y
non-OK. Migrados simEvolution + retryLast.
- chat-simulator usa toast en lugar de status text efímero.
E. Theming con CSS vars:
- public/styles/theme.css con paleta completa (panels, borders,
text, accents, bubbles, charts, radii, shadows). Linkeado desde
index.html.
- Migrados a var(--*) los 5 componentes más visibles:
run-timeline, chat-simulator, conversation-inspector,
conversation-list, home-dashboard. Custom properties heredan a
través del shadow DOM, así que los demás componentes pueden
migrar gradualmente sin cambios estructurales.
- home-dashboard ya tenía vars locales: ahora apuntan a las globales.
Backend: 192/192 tests pasando. Sin cambios de API.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,34 +14,36 @@ class ConversationInspector extends HTMLElement {
|
||||
this._playing = false;
|
||||
this._playIdx = 0;
|
||||
this._timer = null;
|
||||
this._userScrolledUp = false;
|
||||
this._scrollRaf = null;
|
||||
|
||||
this.shadowRoot.innerHTML = `
|
||||
<style>
|
||||
:host { display:block; padding:12px; height:100%; overflow:hidden; }
|
||||
.box { background:#121823; border:1px solid #1e2a3a; border-radius:10px; padding:10px; height:100%; display:flex; flex-direction:column; min-height:0; box-sizing:border-box; }
|
||||
.box { background:var(--panel); border:1px solid var(--border); border-radius:var(--r-lg); padding:10px; height:100%; display:flex; flex-direction:column; min-height:0; box-sizing:border-box; }
|
||||
.row { display:flex; gap:8px; align-items:center; }
|
||||
.muted { color:#8aa0b5; font-size:12px; }
|
||||
.muted { color:var(--text-muted); font-size:12px; }
|
||||
.title { font-weight:800; }
|
||||
.toolbar { display:flex; gap:8px; margin-top:8px; align-items:center; }
|
||||
button { cursor:pointer; background:#0f1520; color:#e7eef7; border:1px solid #253245; border-radius:8px; padding:8px; font-size:13px; }
|
||||
button { cursor:pointer; background:var(--panel-2); color:var(--text); border:1px solid var(--border-hi); border-radius:var(--r-md); padding:8px; font-size:13px; }
|
||||
.list { display:flex; flex-direction:column; gap:0; overflow-y:auto; padding-right:6px; margin-top:8px; flex:1; min-height:0; }
|
||||
.item { border:1px solid #253245; border-radius:12px; padding:8px 10px; background:#0f1520; font-size:12px; margin-bottom:12px; box-sizing:border-box; }
|
||||
.item.in { background:#0f1520; border-color:#2a3a55; }
|
||||
.item.out { background:#111b2a; border-color:#2a3a55; }
|
||||
.item.active { outline:2px solid #1f6feb; box-shadow: 0 0 0 2px rgba(31,111,235,.25); }
|
||||
.item { border:1px solid var(--border-hi); border-radius:12px; padding:8px 10px; background:var(--panel-2); font-size:12px; margin-bottom:12px; box-sizing:border-box; }
|
||||
.item.in { background:var(--panel-2); border-color:var(--bot-border); }
|
||||
.item.out { background:var(--bot-bubble); border-color:var(--bot-border); }
|
||||
.item.active { outline:2px solid var(--accent); box-shadow: 0 0 0 2px rgba(31,111,235,.25); }
|
||||
.item-row { display:flex; gap:8px; }
|
||||
.item-left { flex:1; min-width:0; }
|
||||
.item-right { display:flex; flex-direction:column; gap:4px; align-items:flex-end; justify-content:flex-start; min-width:60px; }
|
||||
.kv { display:grid; grid-template-columns:55px 1fr; gap:4px 6px; }
|
||||
.k { color:#8aa0b5; font-size:10px; letter-spacing:.3px; text-transform:uppercase; }
|
||||
.v { font-size:11px; color:#e7eef7; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
||||
.k { color:var(--text-muted); font-size:10px; letter-spacing:.3px; text-transform:uppercase; }
|
||||
.v { font-size:11px; color:var(--text); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
||||
.chips { display:flex; flex-direction:column; gap:3px; align-items:flex-end; }
|
||||
.chip { display:inline-flex; align-items:center; gap:3px; padding:2px 5px; border-radius:999px; background:#1d2a3a; border:1px solid #243247; font-size:9px; color:#8aa0b5; white-space:nowrap; }
|
||||
.chip { display:inline-flex; align-items:center; gap:3px; padding:2px 5px; border-radius:999px; background:#1d2a3a; border:1px solid #243247; font-size:9px; color:var(--text-muted); white-space:nowrap; }
|
||||
.chip .dot { flex-shrink:0; }
|
||||
.cart { margin-top:4px; font-size:10px; color:#c7d8ee; line-height:1.3; }
|
||||
.tool { margin-top:6px; font-size:11px; color:#8aa0b5; }
|
||||
.cart { margin-top:4px; font-size:10px; color:var(--bot-name); line-height:1.3; }
|
||||
.tool { margin-top:6px; font-size:11px; color:var(--text-muted); }
|
||||
.dot { width:8px; height:8px; border-radius:50%; }
|
||||
.ok { background:#2ecc71; } .warn { background:#f1c40f; } .err { background:#e74c3c; }
|
||||
.ok { background:var(--ok); } .warn { background:var(--warn); } .err { background:var(--err); }
|
||||
</style>
|
||||
|
||||
<div class="box">
|
||||
@@ -83,8 +85,11 @@ class ConversationInspector extends HTMLElement {
|
||||
this.applyHeights();
|
||||
});
|
||||
|
||||
this._unsubScroll = on("ui:chatScroll", ({ chat_id, scrollTop }) => {
|
||||
this._unsubScroll = on("ui:chatScroll", ({ chat_id, scrollTop, userScrolledUp }) => {
|
||||
if (!this.chatId || chat_id !== this.chatId) return;
|
||||
// Si el otro panel está scrolleado-up, sincronizamos. Si no, dejamos
|
||||
// a este panel manejar su propio scroll para evitar saltos cruzados.
|
||||
if (!userScrolledUp) return;
|
||||
const list = this.shadowRoot.getElementById("list");
|
||||
list.scrollTop = scrollTop || 0;
|
||||
});
|
||||
@@ -336,8 +341,24 @@ class ConversationInspector extends HTMLElement {
|
||||
this.rowOrder.push(msgId);
|
||||
}
|
||||
|
||||
// Auto-scroll al final
|
||||
list.scrollTop = list.scrollHeight;
|
||||
// Auto-scroll al final, salvo que el usuario esté leyendo arriba.
|
||||
if (!this._userScrolledUp) {
|
||||
list.scrollTop = list.scrollHeight;
|
||||
}
|
||||
this._bindScroll(list);
|
||||
}
|
||||
|
||||
_bindScroll(list) {
|
||||
if (this._scrollBound) return;
|
||||
this._scrollBound = true;
|
||||
list.addEventListener("scroll", () => {
|
||||
if (this._scrollRaf) return;
|
||||
this._scrollRaf = requestAnimationFrame(() => {
|
||||
this._scrollRaf = null;
|
||||
const distFromBottom = list.scrollHeight - list.scrollTop - list.clientHeight;
|
||||
this._userScrolledUp = distFromBottom > 150;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
applyHeights() {
|
||||
@@ -438,7 +459,9 @@ class ConversationInspector extends HTMLElement {
|
||||
`;
|
||||
|
||||
list.appendChild(el);
|
||||
// Optimistic: el usuario acaba de mandar — forzamos al final.
|
||||
list.scrollTop = list.scrollHeight;
|
||||
this._userScrolledUp = false;
|
||||
|
||||
this.rowMap.set(msg.message_id, el);
|
||||
this.rowOrder.push(msg.message_id);
|
||||
|
||||
Reference in New Issue
Block a user