refactor: enhance log initialization and update logic in Proxmox Firewall component to prevent duplicate entries and improve performance

This commit is contained in:
2025-12-02 08:19:13 -05:00
parent 31b65ef44f
commit 1a8d867b73

View File

@@ -323,18 +323,46 @@
@if(count($firewallLogs) > 0) @if(count($firewallLogs) > 0)
<div <div
wire:key="firewall-logs-table"
x-data="{ x-data="{
logs: @js($firewallLogs), logs: [],
searchTerm: '', searchTerm: '',
sortField: 'timestamp_iso', sortField: 'timestamp_iso',
sortDirection: 'desc', sortDirection: 'desc',
initialized: false,
seenRaws: new Set(),
initialize(initialLogs) { initialize(initialLogs) {
// This is called on x-init if (this.initialized) return;
if (initialLogs && initialLogs.length > 0) {
this.logs = initialLogs.map((log, index) => ({
index,
...log,
}));
// Track which logs we've seen
this.logs.forEach(log => {
if (log.raw) this.seenRaws.add(log.raw);
});
this.initialized = true;
}
}, },
updateLogs(newLogs) { updateLogs(newLogs) {
if (!newLogs || newLogs.length === 0) return; if (!newLogs || newLogs.length === 0) return;
this.logs = newLogs;
// Filter out logs we've already seen
const logsToAdd = newLogs.filter(log => {
if (!log.raw) return false;
if (this.seenRaws.has(log.raw)) return false;
this.seenRaws.add(log.raw);
return true;
});
if (logsToAdd.length > 0) {
// Add new logs to the beginning (most recent first)
this.logs = [...logsToAdd, ...this.logs].map((log, index) => ({
index,
...log,
}));
}
}, },
sort(field) { sort(field) {
if (this.sortField === field) { if (this.sortField === field) {
@@ -380,7 +408,15 @@
}); });
}, },
}" }"
x-init="initialize(@js($firewallLogs))"
@logs-updated.window="updateLogs($event.detail.logs || $event.detail[0]?.logs || $event.detail[0] || [])" @logs-updated.window="updateLogs($event.detail.logs || $event.detail[0]?.logs || $event.detail[0] || [])"
x-effect="
// Watch for changes in the backend data and merge new logs
const backendLogs = @js($firewallLogs);
if (initialized && backendLogs && backendLogs.length > 0) {
updateLogs(backendLogs);
}
"
class="space-y-4" class="space-y-4"
> >
<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-3"> <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-3">