fix: scroll chat — eliminar script duplicado, usar Livewire.hook oficial

- Eliminado script de scroll en public-chat.blade.php (conflictaba con el del layout)
- Reescrito script en chat-publico.blade.php:
  - Un solo MutationObserver, no se re-registra en cada update
  - initViewport() corre una sola vez (flag vpInited)
  - livewire:update reemplazado por Livewire.hook('message.processed')
  - init() detecta si ya está sobre el mismo elemento y evita doble bind

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro
2026-07-19 22:17:03 +00:00
co-authored by Claude Sonnet 4.6
parent 7cc696e04a
commit da9e5a0f68
2 changed files with 65 additions and 143 deletions
@@ -603,81 +603,3 @@
</div>
<script>
(function () {
var container, badge, badgeBtn, countEl;
var userScrolledUp = false;
var newMsgCount = 0;
var debounceTimer = null;
function atBottom() {
return container.scrollHeight - container.scrollTop - container.clientHeight < 120;
}
function scrollToBottom() {
container.scrollTop = container.scrollHeight;
}
function showBadge(n) {
if (!badge || !countEl) return;
countEl.textContent = n;
badge.classList.remove('hidden');
badge.classList.add('flex');
}
function hideBadge() {
if (!badge) return;
badge.classList.add('hidden');
badge.classList.remove('flex');
newMsgCount = 0;
}
function onUpdate() {
if (!userScrolledUp) {
scrollToBottom();
} else {
newMsgCount++;
showBadge(newMsgCount);
}
}
function debouncedUpdate() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(onUpdate, 80);
}
document.addEventListener('livewire:load', function () {
container = document.getElementById('pub-messages');
badge = document.getElementById('new-msg-badge');
badgeBtn = document.getElementById('new-msg-badge-btn');
countEl = document.getElementById('new-msg-count');
if (!container) return;
// Scroll inicial
scrollToBottom();
// Click en badge baja al fondo
if (badgeBtn) {
badgeBtn.addEventListener('click', function () {
container.scrollTo({ top: container.scrollHeight, behavior: 'smooth' });
hideBadge();
});
}
// Detectar scroll manual del usuario
container.addEventListener('scroll', function () {
if (atBottom()) {
userScrolledUp = false;
hideBadge();
} else {
userScrolledUp = true;
}
}, { passive: true });
// Hook oficial Livewire 2: se ejecuta después de cada update del componente
Livewire.hook('message.processed', function (message, component) {
debouncedUpdate();
});
});
})();
</script>