Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

feat(ui): enhance patrol stream reliability and approval banner UX

rcourtmanba7fd5c797e7
diff --git a/frontend-modern/src/components/patrol/ApprovalBanner.tsx b/frontend-modern/src/components/patrol/ApprovalBanner.tsx
index 7bca248a98..fe2833f58a 100644
--- a/frontend-modern/src/components/patrol/ApprovalBanner.tsx
+++ b/frontend-modern/src/components/patrol/ApprovalBanner.tsx
@@ -8,7 +8,7 @@
* - Multiple: count + "Review" button to scroll to first finding
*/
-import { Component, Show, createMemo, createSignal, onCleanup } from 'solid-js';
+import { Component, Show, createMemo, createSignal, createEffect, onCleanup } from 'solid-js';
import { aiIntelligenceStore } from '@/stores/aiIntelligence';
import { notificationStore } from '@/stores/notifications';
import type { ApprovalRequest } from '@/api/ai';
@@ -24,14 +24,18 @@ export const ApprovalBanner: Component<ApprovalBannerProps> = (props) => {
const [actionLoading, setActionLoading] = createSignal<string | null>(null);
const [tick, setTick] = createSignal(Date.now());
- // Tick every second to keep countdown live
- const tickInterval = setInterval(() => setTick(Date.now()), 1000);
- onCleanup(() => clearInterval(tickInterval));
-
const pending = createMemo(() =>
aiIntelligenceStore.pendingApprovals.filter((a: ApprovalRequest) => a.status === 'pending')
);
+ // Only tick when there are pending approvals to avoid unnecessary work
+ createEffect(() => {
+ if (pending().length > 0) {
+ const tickInterval = setInterval(() => setTick(Date.now()), 1000);
+ onCleanup(() => clearInterval(tickInterval));
+ }
+ });
+
const firstApproval = createMemo(() => pending()[0] ?? null);
const timeRemaining = (expiresAt: string) => {
diff --git a/frontend-modern/src/hooks/usePatrolStream.ts b/frontend-modern/src/hooks/usePatrolStream.ts
index 840753e0ad..f3351b5ce2 100644
--- a/frontend-modern/src/hooks/usePatrolStream.ts
+++ b/frontend-modern/src/hooks/usePatrolStream.ts
@@ -13,6 +13,8 @@ import { logger } from '@/utils/logger';
export interface UsePatrolStreamOptions {
/** Reactive boolean – stream is only open while this returns true. */
running: Accessor<boolean>;
+ /** Called when the SSE connection opens successfully. */
+ onStart?: () => void;
/** Called when a 'complete' event is received. */
onComplete?: () => void;
/** Called when an 'error' event is received or the SSE connection fails. */
@@ -106,6 +108,7 @@ export function usePatrolStream(opts: UsePatrolStreamOptions): PatrolStreamState
logger.info('[PatrolStream] SSE connected');
setIsStreaming(true);
reconnectAttempts = 0; // Reset on successful connection
+ opts.onStart?.();
};
es.onmessage = (msg) => {
@@ -166,9 +169,10 @@ export function usePatrolStream(opts: UsePatrolStreamOptions): PatrolStreamState
// Only reconnect if the close wasn't intentional and patrol is still running
if (!intentionalClose && opts.running()) {
scheduleReconnect();
- } else {
+ } else if (!intentionalClose) {
opts.onError?.();
}
+ // If intentionalClose, do nothing — we already handled it
};
} catch {
// EventSource constructor can throw in some environments
diff --git a/frontend-modern/src/pages/AIIntelligence.tsx b/frontend-modern/src/pages/AIIntelligence.tsx
index 171ae9e382..c5c576aedb 100644
--- a/frontend-modern/src/pages/AIIntelligence.tsx
+++ b/frontend-modern/src/pages/AIIntelligence.tsx
@@ -101,9 +101,19 @@ export function AIIntelligence() {
// Optimistic running state — set immediately on "Run Patrol" click to avoid race with backend
const [manualRunRequested, setManualRunRequested] = createSignal(false);
+ // Safety timer ref — hoisted so onStart can clear it when SSE connects
+ let safetyTimerRef: ReturnType<typeof setTimeout> | undefined;
+
// Live patrol streaming
const patrolStream = usePatrolStream({
running: () => (patrolStatus()?.running ?? false) || manualRunRequested(),
+ onStart: () => {
+ // SSE connected — clear the safety timeout
+ if (safetyTimerRef !== undefined) {
+ clearTimeout(safetyTimerRef);
+ safetyTimerRef = undefined;
+ }
+ },
onComplete: () => {
setManualRunRequested(false);
loadAllData();
@@ -264,8 +274,9 @@ export function AIIntelligence() {
setIsTriggeringPatrol(true);
setManualRunRequested(true);
- // Safety timeout: if SSE never connects within 15s, clear optimistic state
- const safetyTimer = setTimeout(() => {
+ // Safety timeout: if SSE never connects within 15s, clear optimistic state.
+ // Cleared early via onStart callback when the SSE connection opens.
+ safetyTimerRef = setTimeout(() => {
if (manualRunRequested() && !patrolStream.isStreaming()) {
setManualRunRequested(false);
notificationStore.error('Patrol run did not start — connection timed out');
@@ -280,8 +291,12 @@ export function AIIntelligence() {
console.error('Failed to trigger patrol run:', err);
setManualRunRequested(false);
notificationStore.error('Failed to start patrol run');
+ // Clear safety timer on API error
+ if (safetyTimerRef !== undefined) {
+ clearTimeout(safetyTimerRef);
+ safetyTimerRef = undefined;
+ }
} finally {
- clearTimeout(safetyTimer);
setIsTriggeringPatrol(false);
}
}