Files
breadcrumb-the-shire/modules/helpdesk/web/js/helpdesk-risk-radar.js
fs d5d1e22e87 fix(helpdesk): consolidate card content into compact fact pills
Remove duplicate metrics row and reason list. Replace with a single
row of compact pills: '5 open', '2 critical' (orange), '1 SLA' (orange),
'↑3' (orange) or '↓2'. No bullet points — pills as flex-wrap row.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 00:43:35 +02:00

270 lines
9.6 KiB
JavaScript

/**
* Risk Radar — customer portfolio risk view.
*/
const container = document.querySelector('.app-details-container[data-risk-radar-url]');
if (container) {
const dataUrl = container.dataset.riskRadarUrl;
const debitorBaseUrl = container.dataset.debitorBaseUrl || '';
const d = (key, fallback) => container.dataset[key] || fallback;
const elLoading = document.getElementById('radar-loading');
const elError = document.getElementById('radar-error');
const elEmpty = document.getElementById('radar-empty');
const elTruncated = document.getElementById('radar-truncated');
const elContent = document.getElementById('radar-content');
const elCards = document.getElementById('radar-cards');
const elRefresh = container.querySelector('button[name="radar-refresh"]');
const periodSelector = document.getElementById('radar-period-selector');
const searchInput = document.getElementById('radar-search');
const dialog = document.getElementById('radar-detail-dialog');
const dialogTitle = document.getElementById('radar-detail-title');
const dialogBody = document.getElementById('radar-detail-body');
const dialogClose = dialog ? dialog.querySelector('.close') : null;
let currentPeriod = 90;
let lastData = null;
function showState(state) {
if (elLoading) elLoading.hidden = state !== 'loading';
if (elError) elError.hidden = state !== 'error';
if (elEmpty) elEmpty.hidden = state !== 'empty';
if (elContent) elContent.hidden = state !== 'success';
}
function setText(id, value) {
const el = document.getElementById(id);
if (el) el.textContent = value;
}
function h(tag, cls, text) {
const e = document.createElement(tag);
if (cls) e.className = cls;
if (text !== undefined) e.textContent = text;
return e;
}
function levelClass(level) {
return 'helpdesk-risk-level-' + level;
}
function formatAge(hours) {
if (hours < 24) return hours + 'h';
return Math.floor(hours / 24) + 'd';
}
/**
* Dimension label lookup from data attributes.
*/
function dimLabel(id) {
const map = {
'open_pressure': d('labelOpenPressure', 'Open pressure'),
'trend': d('labelTrend', 'Trend'),
'sla_overdue': d('labelSla', 'SLA overdue'),
'resolution': d('labelResolution', 'Resolution time'),
'inactivity': d('labelInactivity', 'Inactivity'),
};
return map[id] || id;
}
/**
* Build a single customer risk card.
*/
function buildCard(customer) {
const card = h('article', 'helpdesk-risk-card ' + levelClass(customer.risk_level));
card.setAttribute('aria-label', customer.customer_name || customer.customer_no);
// Header: name + score badge
const header = h('div', 'helpdesk-risk-card-header');
const nameEl = h('span', 'helpdesk-risk-card-name', customer.customer_name || customer.customer_no);
const scoreBadge = h('span', 'helpdesk-risk-card-score ' + levelClass(customer.risk_level), String(customer.risk_score));
scoreBadge.setAttribute('aria-label', d('labelScore', 'Risk score') + ': ' + customer.risk_score);
header.appendChild(nameEl);
header.appendChild(scoreBadge);
card.appendChild(header);
// Key facts as compact pills
const kpis = customer.kpis || {};
const pills = h('ul', 'helpdesk-risk-card-reasons');
if (kpis.open > 0) {
const li = h('li', '', kpis.open + ' ' + d('labelOpen', 'open'));
pills.appendChild(li);
}
if (kpis.critical > 0) {
const li = h('li', 'helpdesk-risk-card-reason-warn', kpis.critical + ' ' + d('labelCritical', 'critical'));
pills.appendChild(li);
}
if (kpis.sla_overdue > 0) {
const li = h('li', 'helpdesk-risk-card-reason-warn', kpis.sla_overdue + ' ' + d('labelSlaBreaches', 'SLA'));
pills.appendChild(li);
}
if (kpis.net_flow > 0) {
const li = h('li', 'helpdesk-risk-card-reason-warn', '↑' + kpis.net_flow);
pills.appendChild(li);
} else if (kpis.net_flow < 0) {
pills.appendChild(h('li', '', '↓' + Math.abs(kpis.net_flow)));
}
card.appendChild(pills);
// Click/keyboard to open detail
card.tabIndex = 0;
card.addEventListener('click', () => openDetail(customer));
card.addEventListener('keydown', (e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); openDetail(customer); } });
return card;
}
/**
* Open detail dialog for a customer.
*/
function openDetail(customer) {
if (!dialog || !dialogTitle || !dialogBody) return;
dialogTitle.textContent = (customer.customer_name || customer.customer_no) + ' — ' + customer.risk_score + '/100';
dialogBody.replaceChildren();
// All 5 dimensions as table with raw value, score, weight
const dims = customer.dimensions || [];
const dimTable = h('table', 'helpdesk-risk-detail-tickets');
const dimHead = h('thead');
const dimHeadRow = h('tr');
dimHeadRow.appendChild(h('th', '', d('labelScore', 'Dimension')));
dimHeadRow.appendChild(h('th', '', d('labelScore', 'Score')));
dimHeadRow.appendChild(h('th', '', '%'));
dimHead.appendChild(dimHeadRow);
dimTable.appendChild(dimHead);
const dimBody = h('tbody');
for (const dim of dims) {
const tr = h('tr');
tr.appendChild(h('td', '', dimLabel(dim.id)));
tr.appendChild(h('td', '', dim.dimension_score !== null ? String(dim.dimension_score) : '—'));
tr.appendChild(h('td', '', dim.weighted_points > 0 ? dim.weighted_points.toFixed(1) : '—'));
dimBody.appendChild(tr);
}
// Total row
const totalTr = h('tr');
totalTr.appendChild(h('td', '', 'Total'));
const totalTd = h('td', '');
totalTd.style.fontWeight = '700';
totalTd.textContent = String(customer.risk_score);
totalTr.appendChild(totalTd);
totalTr.appendChild(h('td', '', ''));
dimBody.appendChild(totalTr);
dimTable.appendChild(dimBody);
dialogBody.appendChild(dimTable);
// Open tickets list
const tickets = customer.open_tickets || [];
if (tickets.length > 0) {
dialogBody.appendChild(h('h3', 'helpdesk-support-section-title', d('labelTickets', 'Tickets') + ' (' + tickets.length + ')'));
const table = h('table', 'helpdesk-risk-detail-tickets');
const thead = h('thead');
const headRow = h('tr');
headRow.appendChild(h('th', '', d('labelTicket', 'Ticket')));
headRow.appendChild(h('th', '', d('labelSla', 'SLA')));
headRow.appendChild(h('th', '', d('labelStatus', 'Status')));
headRow.appendChild(h('th', '', d('labelAge', 'Age')));
thead.appendChild(headRow);
table.appendChild(thead);
const tbody = h('tbody');
for (const t of tickets) {
const tr = h('tr', t.age_hours > 48 ? 'helpdesk-risk-detail-ticket-critical' : '');
tr.appendChild(h('td', '', t.no));
tr.appendChild(h('td', '', t.escalation_code || '—'));
tr.appendChild(h('td', '', t.state));
tr.appendChild(h('td', '', formatAge(t.age_hours)));
tbody.appendChild(tr);
}
table.appendChild(tbody);
dialogBody.appendChild(table);
}
// Link to debitor
if (debitorBaseUrl && customer.customer_no) {
const link = h('a', 'helpdesk-risk-detail-link', customer.customer_name || customer.customer_no);
link.href = debitorBaseUrl + encodeURIComponent(customer.customer_no);
link.target = '_blank';
link.rel = 'noopener noreferrer';
dialogBody.appendChild(link);
}
dialog.showModal();
}
// Dialog close handlers
if (dialogClose) dialogClose.addEventListener('click', () => dialog.close());
if (dialog) dialog.addEventListener('click', (e) => { if (e.target === dialog) dialog.close(); });
function renderKpis(summary) {
setText('radar-kpi-total', summary.total ?? 0);
setText('radar-kpi-high', summary.high ?? 0);
setText('radar-kpi-medium', summary.medium ?? 0);
setText('radar-kpi-low', summary.low ?? 0);
}
function renderCards(customers) {
if (!elCards) return;
elCards.replaceChildren();
for (const c of customers) elCards.appendChild(buildCard(c));
}
function filterCards(query) {
if (!lastData) return;
const q = query.toLowerCase().trim();
const filtered = q === ''
? lastData.customers
: lastData.customers.filter(c => (c.customer_name || '').toLowerCase().includes(q) || c.customer_no.toLowerCase().includes(q));
renderCards(filtered);
}
async function fetchData(refresh = false) {
showState('loading');
if (elTruncated) elTruncated.hidden = true;
const params = new URLSearchParams({ periodDays: String(currentPeriod) });
if (refresh) params.set('refresh', '1');
try {
const res = await fetch(dataUrl + '?' + params.toString(), { credentials: 'same-origin' });
const json = await res.json();
if (!json.ok) { showState('error'); return; }
if (!json.customers || json.customers.length === 0) { showState('empty'); return; }
lastData = json;
renderKpis(json.summary);
renderCards(json.customers);
showState('success');
if (json.meta && json.meta.truncated && elTruncated) {
elTruncated.hidden = false;
}
} catch {
showState('error');
}
}
if (periodSelector) {
periodSelector.addEventListener('change', (e) => {
const radio = e.target.closest('input[name="radar-period"]');
if (!radio) return;
const period = parseInt(radio.value, 10);
if (!period || period === currentPeriod) return;
currentPeriod = period;
fetchData();
});
}
if (searchInput) {
searchInput.addEventListener('input', () => filterCards(searchInput.value));
}
if (elRefresh) {
elRefresh.addEventListener('click', () => fetchData(true));
}
fetchData();
}