fix: Use event delegation for switcher clicks
All checks were successful
Botlib CI / build (push) Successful in 7s
BotServer / build (push) Successful in 3m56s
Bottest CI / build (push) Successful in 35s
BotUI CI / build (push) Successful in 45s

- Event listeners were lost when renderSwitchers() re-created DOM
- Now using event delegation on parent container
- Listener attached once, persists across re-renders
- Added logging to verify active_switchers payload

Fixes switcher toggle not persisting and LLM modifier not being sent.
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-24 16:57:30 +00:00
parent 840175d540
commit d453eef57d

View file

@ -1,5 +1,5 @@
// Chat Switchers Module - Manages format switchers (tables, infographic, cards, etc.)
// This module handles both UI rendering and state management for switchers
// Uses event delegation for better reliability with dynamic content
var activeSwitchers = new Set();
var switcherDefinitions = [];
@ -47,11 +47,19 @@ function renderSwitchers() {
);
}).join('');
container.querySelectorAll('.switcher-chip').forEach(function(chip) {
chip.addEventListener('click', function() {
toggleSwitcher(this.getAttribute('data-switch-id'));
// Event delegation - attach once to parent
if (!container.dataset.hasClickHandler) {
container.addEventListener('click', function(e) {
var chip = e.target.closest('.switcher-chip');
if (chip) {
var switcherId = chip.getAttribute('data-switch-id');
if (switcherId) {
toggleSwitcher(switcherId);
}
}
});
});
container.dataset.hasClickHandler = 'true';
}
}
function toggleSwitcher(switcherId) {
@ -65,11 +73,15 @@ function toggleSwitcher(switcherId) {
console.log('Added switcher:', switcherId);
}
console.log('activeSwitchers after:', Array.from(activeSwitchers));
// Re-render to show active state
renderSwitchers();
}
function getActiveSwitcherIds() {
return Array.from(activeSwitchers);
var ids = Array.from(activeSwitchers);
console.log('getActiveSwitcherIds returning:', ids);
return ids;
}
function clearSwitchers() {