// DIDI History Page Script (Full V3 API)
console.log('[DIDI History] Script loaded');
let historyData = [];
function getRiskColorByScore(score) {
if (score >= 80) return '#B91C1C';
if (score >= 60) return '#EF4444';
if (score >= 40) return '#F97316';
if (score >= 20) return '#EAB308';
return '#22C55E';
}
document.addEventListener('DOMContentLoaded', async () => {
chrome.runtime.sendMessage({ action: 'getAuthStatus' }, (authResponse) => {
const authInfo = document.getElementById('authInfo');
if (authResponse?.authenticated) {
authInfo.innerHTML = `Connected`;
} else {
authInfo.innerHTML = `Not connected`;
}
});
chrome.storage.local.get(['didiHistory'], (data) => {
historyData = data.didiHistory || [];
console.log('[DIDI History] Loaded', historyData.length, 'items');
renderHistory();
});
setupEventListeners();
});
function renderHistory() {
const grid = document.getElementById('historyGrid');
const subtitle = document.getElementById('subtitle');
if (!historyData || historyData.length === 0) {
subtitle.textContent = 'No analyses yet';
grid.innerHTML = `
${riskScore}
${riskCategory}
${analysisType}
Techniques${techniques.length}
Claims${claimsData.total_claims || 0}
AI Prob.${aiProb}%
${createdAt ? `
${new Date(createdAt).toLocaleString()}
` : ''}
`;
}).join('');
}
function setupEventListeners() {
const modal = document.getElementById('analysisModal');
const modalClose = document.getElementById('modalClose');
document.getElementById('historyGrid').addEventListener('click', (e) => {
if (e.target.classList.contains('view-details-btn')) {
e.preventDefault(); e.stopPropagation();
openModal(parseInt(e.target.getAttribute('data-index'), 10));
return;
}
if (e.target.classList.contains('delete-btn')) {
e.preventDefault(); e.stopPropagation();
deleteItem(parseInt(e.target.getAttribute('data-index'), 10));
return;
}
const card = e.target.closest('.history-item');
if (card && !e.target.closest('button')) {
openModal(parseInt(card.getAttribute('data-index'), 10));
}
});
modalClose.addEventListener('click', () => closeModal());
modal.addEventListener('click', (e) => { if (e.target === modal) closeModal(); });
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeModal(); });
}
function deleteItem(index) {
if (!confirm('Delete this analysis result?')) return;
historyData.splice(index, 1);
chrome.storage.local.set({ didiHistory: historyData }, () => renderHistory());
}
function openModal(index) {
const item = historyData[index];
if (!item) return;
const modal = document.getElementById('analysisModal');
document.getElementById('modalBody').innerHTML = generateAnalysisHTML(item);
modal.classList.add('active');
}
function closeModal() {
document.getElementById('analysisModal').classList.remove('active');
}
function generateAnalysisHTML(item) {
// Shared renderer mirrors web app PipelineAnalysis layout.
return window.DidiRender.renderAnalysisHTML(item.analysis || {});
}