fix(telefoni): preserve SSR call rows when client refresh fails

This commit is contained in:
Christian 2026-05-18 07:00:52 +02:00
parent ef8e68fc16
commit 94f6735ed5

View File

@ -850,6 +850,27 @@ function hasExistingCallRows(tbody) {
return Boolean(tbody && tbody.querySelector('tr[data-call-id]'));
}
function cacheInitialSsrRows(tbody) {
if (!tbody) return;
if (tbody.dataset.initialRowsCached === '1') return;
tbody.dataset.initialRowsHtml = tbody.innerHTML;
tbody.dataset.initialRowsCached = '1';
}
function restoreInitialSsrRows(tbody) {
if (!tbody) return false;
const initialHtml = String(tbody.dataset.initialRowsHtml || '').trim();
if (!initialHtml) return false;
if (!initialHtml.includes('data-call-id')) return false;
tbody.innerHTML = initialHtml;
tbody.classList.remove('d-none');
tbody.style.display = '';
tbody.style.visibility = '';
tbody.style.opacity = '';
return true;
}
function hasActiveCallFilters() {
const userId = document.getElementById('filterUser')?.value;
const from = document.getElementById('filterFrom')?.value;
@ -862,6 +883,7 @@ async function loadCalls(options = {}) {
const preserveOnEmpty = Boolean(options.preserveOnEmpty);
const skipLoadingState = Boolean(options.skipLoadingState);
const tbody = document.getElementById('telefoniRows');
cacheInitialSsrRows(tbody);
const hadRowsBeforeLoad = hasExistingCallRows(tbody);
const noActiveFilters = !hasActiveCallFilters();
@ -888,6 +910,10 @@ async function loadCalls(options = {}) {
console.warn('Telefoni: bevarer SSR-rækker efter tom/fejlende auto-refresh');
return;
}
if (preserveOnEmpty && noActiveFilters && restoreInitialSsrRows(tbody)) {
console.warn('Telefoni: gendannede SSR-rækker efter API-fejl under auto-refresh');
return;
}
tbody.innerHTML = `<tr><td colspan="7" class="text-danger small">Fejl: ${escapeHtml(t)}</td></tr>`;
return;
}
@ -899,6 +925,10 @@ async function loadCalls(options = {}) {
console.warn('Telefoni: API returnerede 0 rækker, bevarer eksisterende SSR-visning');
return;
}
if (preserveOnEmpty && noActiveFilters && restoreInitialSsrRows(tbody)) {
console.warn('Telefoni: gendannede SSR-rækker efter tom API-respons');
return;
}
tbody.innerHTML = '<tr><td colspan="7" class="text-muted small">Ingen opkald fundet</td></tr>';
return;
}
@ -972,6 +1002,10 @@ async function loadCalls(options = {}) {
console.warn('Telefoni: bevarer SSR-rækker efter exception i auto-refresh');
return;
}
if (preserveOnEmpty && noActiveFilters && restoreInitialSsrRows(tbody)) {
console.warn('Telefoni: gendannede SSR-rækker efter exception i auto-refresh');
return;
}
tbody.innerHTML = '<tr><td colspan="7" class="text-danger small">Kunne ikke hente opkald</td></tr>';
}
}
@ -1030,6 +1064,7 @@ async function unlinkCase(callId) {
}
document.addEventListener('DOMContentLoaded', async () => {
cacheInitialSsrRows(document.getElementById('telefoniRows'));
initLinkContactModalEvents();
initLinkSagModalEvents();
const userFilter = document.getElementById('filterUser');