// compare/scoreboard.js — vote history display import Storage from '../storage.js'; import state from './state.js'; import { VOTES_STORAGE_KEY } from './icons.js'; import themeModule from '../theme.js'; import uiModule from '../ui.js'; const escapeHtml = uiModule.esc; // Type icons for the mode tabs — match the Compare selector's tab icons. const _TYPE_ICONS = { chat: '', agent: '', search: '', research: '', }; /** Detect search provider names to fix legacy votes without mode. */ const _searchProviderNames = new Set(['brave search', 'duckduckgo', 'google', 'searxng', 'bing', 'tavily']); /** Guess the compare mode for a vote record (legacy votes lack a mode field). */ function _guessVoteMode(v) { if (v.mode) return v.mode; // Legacy vote — check if models look like search providers if (v.models && v.models.some(m => _searchProviderNames.has(m.toLowerCase()))) return 'search'; return 'chat'; } export function showScoreboard() { // Remove existing overlay if present const existing = document.getElementById('scoreboard-overlay'); if (existing) existing.remove(); const votes = Storage.getJSON(VOTES_STORAGE_KEY, []); // Build modal const overlay = document.createElement('div'); overlay.id = 'scoreboard-overlay'; overlay.className = 'modal'; overlay.style.zIndex = '10001'; overlay.addEventListener('click', (e) => { if (e.target === overlay) overlay.remove(); }); // Esc handling lives in the global "close topmost popup" handler (app.js) // so the scoreboard closes first without also dismissing the compare // window beneath it. const content = document.createElement('div'); content.className = 'modal-content'; content.style.maxWidth = '520px'; const header = document.createElement('div'); header.className = 'modal-header'; const title = document.createElement('h3'); title.innerHTML = 'Scoreboard'; title.style.margin = '0'; const closeX = document.createElement('button'); closeX.className = 'close-btn'; closeX.innerHTML = '✖'; closeX.addEventListener('click', () => overlay.remove()); header.appendChild(title); header.appendChild(closeX); content.appendChild(header); const body = document.createElement('div'); body.className = 'modal-body'; body.style.padding = '12px 16px'; // Mobile: add bottom padding so the Clear History button isn't hidden behind // Firefox's bottom URL bar / the home-indicator safe area. if (window.innerWidth <= 768) { body.style.paddingBottom = 'calc(env(safe-area-inset-bottom, 0px) + 72px)'; body.style.overflowY = 'auto'; } // Mode tabs const modes = ['chat', 'agent', 'search', 'research']; const modeLabels = { chat: 'Chat', agent: 'Agent', search: 'Search', research: 'Research' }; const tabBar = document.createElement('div'); tabBar.className = 'compare-mode-tabs'; tabBar.style.marginBottom = '12px'; let activeMode = 'chat'; function renderScoreTable() { // Clear previous table const prev = body.querySelector('.scoreboard-wrap'); if (prev) { // The Clear button was moved INTO the wrap on a prior render — rescue it // back to the body before removing the wrap, otherwise it's destroyed // with the wrap and never re-found (it vanished after visiting an empty // mode like Images and switching back). const clr = prev.querySelector('.scoreboard-clear-btn'); if (clr) body.appendChild(clr); prev.remove(); } const wrap = document.createElement('div'); wrap.className = 'scoreboard-wrap'; const filtered = votes.filter(v => _guessVoteMode(v) === activeMode); // Aggregate const stats = {}; for (const v of filtered) { for (let mi = 0; mi < v.models.length; mi++) { const m = v.models[mi]; if (!stats[m]) stats[m] = { wins: 0, losses: 0, ties: 0, games: 0, totalCost: 0, costCount: 0 }; stats[m].games++; if (v.winner === 'tie') stats[m].ties++; else if (v.winner === m) stats[m].wins++; else stats[m].losses++; if (v.costs && v.costs[mi] != null) { stats[m].totalCost += v.costs[mi]; stats[m].costCount++; } } } const sorted = Object.entries(stats).sort((a, b) => { const rateA = a[1].games ? a[1].wins / a[1].games : 0; const rateB = b[1].games ? b[1].wins / b[1].games : 0; return rateB - rateA; }); if (sorted.length === 0) { const empty = document.createElement('p'); empty.style.cssText = 'color:color-mix(in srgb, var(--fg) 50%, transparent);text-align:center;padding:24px 0;'; empty.textContent = 'No ' + activeMode + ' votes yet. Run a comparison and vote!'; wrap.appendChild(empty); } else { const table = document.createElement('table'); table.className = 'scoreboard-table'; const thead = document.createElement('thead'); thead.innerHTML = '