Files
webhook/client/src/components/requestTable.js
T
2026-07-07 19:35:28 +02:00

118 lines
4.9 KiB
JavaScript

// Webhook kérések táblázata - valós időben frissülő lista
import { formatRelativeTime, formatTimestamp, methodColor, truncatePayload } from '../utils/formatters.js';
import { icon } from '../utils/icon.js';
// Maximum ennyi sort tartunk a táblázatban memóriakímélés miatt
const MAX_ROWS = 100;
// Táblázat komponens létrehozása és visszaadása
// onRowClick: callback amikor egy sort kattintanak
export function createRequestTable(onRowClick) {
let selectedId = null;
const rows = []; // Sorok nyilvántartása a cleanup-hoz
const wrapper = document.createElement('div');
wrapper.className = 'overflow-x-auto';
wrapper.innerHTML = `
<table class="w-full text-sm">
<thead>
<tr class="text-left text-slate-500 border-b border-slate-800">
<th class="pb-3 pl-3 pr-4 font-medium whitespace-nowrap text-xs uppercase tracking-wider">Időpont</th>
<th class="pb-3 pr-4 font-medium text-xs uppercase tracking-wider">Metódus</th>
<th class="pb-3 pr-4 font-medium text-xs uppercase tracking-wider">IP cím</th>
<th class="pb-3 pr-4 font-medium hidden md:table-cell text-xs uppercase tracking-wider">Payload</th>
<th class="pb-3 pr-4 font-medium text-center hidden sm:table-cell text-xs uppercase tracking-wider">GET</th>
<th class="pb-3 font-medium text-center hidden sm:table-cell text-xs uppercase tracking-wider">POST</th>
</tr>
</thead>
<tbody id="request-tbody" class="divide-y divide-slate-800/60">
</tbody>
</table>
<div id="empty-state" class="py-16 text-center text-slate-600">
<div class="text-4xl mb-3 flex justify-center">${icon('network', 'w-18 h-18 text-slate-600')}</div>
<p class="text-lg font-medium text-slate-400">Várakozás a webhook hívásokra…</p>
<p class="text-sm mt-1">Küldd el a webhook URL-t, és a kérések itt jelennek meg</p>
</div>
`;
const tbody = wrapper.querySelector('#request-tbody');
const emptyState = wrapper.querySelector('#empty-state');
// Relatív időpontok frissítése (minden 30 másodpercben)
const timeInterval = setInterval(() => {
wrapper.querySelectorAll('[data-timestamp]').forEach(el => {
el.textContent = formatRelativeTime(el.dataset.timestamp);
});
}, 30_000);
// Új sor hozzáadása a táblázat elejére
function prepend(data) {
emptyState.classList.add('hidden');
const tr = document.createElement('tr');
tr.dataset.id = data.id;
// Bal oldali átlátszó accent sáv alapból, hogy kijelöléskor ne ugorjon a layout
tr.className = 'cursor-pointer border-l-2 border-transparent hover:bg-slate-800/40 transition-colors duration-150 group';
tr.innerHTML = `
<td class="py-3 pl-3 pr-4 whitespace-nowrap">
<span
class="text-slate-400 group-hover:text-slate-100 transition-colors"
data-timestamp="${data.timestamp}"
title="${formatTimestamp(data.timestamp)}"
>${formatRelativeTime(data.timestamp)}</span>
</td>
<td class="py-3 pr-4">
<span class="inline-block px-2 py-0.5 rounded-md border text-xs font-bold ${methodColor(data.method)}">
${data.method}
</span>
</td>
<td class="py-3 pr-4 text-slate-400 font-mono text-xs whitespace-nowrap">${data.remoteIp}</td>
<td class="py-3 pr-4 text-slate-500 hidden md:table-cell max-w-xs truncate">
${truncatePayload(data.rawPayload)}
</td>
<td class="py-3 pr-4 text-center hidden sm:table-cell">
${data.queryCount > 0 ? `<span class="text-cyan-300 font-semibold">${data.queryCount}</span>` : '<span class="text-slate-700">—</span>'}
</td>
<td class="py-3 text-center hidden sm:table-cell">
${data.bodyCount > 0 ? `<span class="text-emerald-300 font-semibold">${data.bodyCount}</span>` : '<span class="text-slate-700">—</span>'}
</td>
`;
// Kijelölést jelző osztályok (gradiens háttér + cyan accent sáv)
const selectedClasses = ['bg-linear-to-r', 'from-cyan-500/15', 'to-transparent', 'border-cyan-400'];
// Sor kattintás esemény
tr.addEventListener('click', () => {
// Előző kijelölés törlése
tbody.querySelectorAll('tr').forEach(r => {
r.classList.remove(...selectedClasses);
r.classList.add('border-transparent');
});
// Új kijelölés
tr.classList.remove('border-transparent');
tr.classList.add(...selectedClasses);
selectedId = data.id;
onRowClick(data);
});
// Sor hozzáadása az elejére + belépési animáció
tbody.insertBefore(tr, tbody.firstChild);
rows.unshift(tr);
// Maximum sorok betartása - legrégebbi törlése
if (rows.length > MAX_ROWS) {
const oldest = rows.pop();
oldest.remove();
}
}
// Cleanup - időzítők leállítása
function destroy() {
clearInterval(timeInterval);
}
return { prepend, destroy, element: wrapper };
}