import React, { useState, useEffect } from 'react'; import { Bell, BellRing, BellOff, RefreshCw, CheckCircle2, AlertTriangle, Radio, ExternalLink, Scale, Clock, Sparkles, ChevronRight, Volume2, Play, Pause, Sliders, Check, FileText, Activity, History } from 'lucide-react'; import { pacerWorker, WorkerState, DocketStatusChangeEvent } from '../services/pacerNotificationService'; import { CaseHistoryRecord } from '../types/document'; interface Props { onSelectDocket?: (docketNumber: string) => void; onDocketsUpdated?: (dockets: CaseHistoryRecord[]) => void; } export const PacerBackgroundWorkerBanner: React.FC = ({ onSelectDocket, onDocketsUpdated }) => { const [workerState, setWorkerState] = useState(pacerWorker.getState()); const [isToastVisible, setIsToastVisible] = useState(false); const [latestToastEvent, setLatestToastEvent] = useState(null); const [isHistoryOpen, setIsHistoryOpen] = useState(false); const onDocketsUpdatedRef = React.useRef(onDocketsUpdated); onDocketsUpdatedRef.current = onDocketsUpdated; useEffect(() => { let lastCheckCount = workerState.checkCount; let lastAlertId: string | null = workerState.recentAlerts.length > 0 ? workerState.recentAlerts[0].id : null; const unsubscribe = pacerWorker.subscribe((state) => { setWorkerState(state); // Only notify parent when a check was performed or dockets actually changed if (state.checkCount > lastCheckCount) { lastCheckCount = state.checkCount; if (onDocketsUpdatedRef.current) { onDocketsUpdatedRef.current(pacerWorker.getDocketsList()); } } // Only trigger toast if a brand new alert has arrived if (state.recentAlerts.length > 0) { const top = state.recentAlerts[0]; if (top.id !== lastAlertId) { lastAlertId = top.id; setLatestToastEvent(top); setIsToastVisible(true); } } }); return () => unsubscribe(); }, []); const handleRequestPermission = async () => { const perm = await pacerWorker.requestNotificationPermission(); if (perm === 'granted') { pacerWorker.triggerBrowserNotification( '🏛️ Browser Alerts Enabled', 'You will now receive native desktop alerts whenever a PACER or State Court docket status change is detected.', 'TEST' ); } }; const handleTestNotification = () => { pacerWorker.triggerBrowserNotification( '🏛️ PACER Alert Test: Case No. C-03-CV-26-004128', 'Test notification: Circuit Court for Baltimore County Docket Active. Browser alerts functioning properly!', 'TEST-4128' ); }; const handleForceCheck = async () => { const changes = await pacerWorker.checkDockets(true); if (changes.length > 0) { setLatestToastEvent(changes[0]); setIsToastVisible(true); } }; return (
{/* Worker Control Bar */}
{/* Status info */}
{workerState.isActive ? : }
{workerState.isActive && ( )}
PACER Docket Background Worker {workerState.isActive ? 'ACTIVE POLLING' : 'PAUSED'} • {workerState.monitoredDocketsCount} Linked Dockets • Interval: {workerState.intervalSeconds}s

Continuous background monitor polling federal RECAP & Maryland MDEC dockets for minute orders, status changes, and calendar orders.

{/* Action buttons & Notification toggle */}
{/* Notification Permission Pill */} {workerState.notificationPermission === 'granted' ? (
Browser Alerts: Active
) : workerState.notificationPermission === 'denied' ? (
Alerts Blocked
) : ( )} {/* Test alert */} {/* Force check / Simulate live docket change */} {/* Interval selection */} {/* Pause / Resume */} {/* History Toggle */}
{/* Worker Telemetry Sub-bar */}
Last Audit: {workerState.lastCheckTimestamp || 'Just now'} Total Audits Completed: {workerState.checkCount}
Source Endpoints: Free Law Project CourtListener RECAP • Maryland Judiciary MDEC
{/* Floating In-App Status Notification Toast Banner */} {isToastVisible && latestToastEvent && (
{latestToastEvent.docketNumber} PACER Status Change Detected {latestToastEvent.timestamp}

New Status: “{latestToastEvent.newStatus}”

{latestToastEvent.newEntry && (
{latestToastEvent.newEntry.documentNumber}: {latestToastEvent.newEntry.description}
)} {latestToastEvent.actionRequired && (
Recommended Action: {latestToastEvent.actionRequired}
)}
{onSelectDocket && ( )}
)} {/* Expandable Alert History Log */} {isHistoryOpen && (

PACER Background Worker Detection History

{workerState.recentAlerts.length > 0 && ( )}
{workerState.recentAlerts.length === 0 ? (
No status changes detected yet in this session. The background worker will record updates and dispatch browser notifications as new minute orders or docket filings are published.
) : (
{workerState.recentAlerts.map((alert) => (
{alert.docketNumber} {alert.court}
{alert.timestamp}
Status: {alert.previousStatus} → {alert.newStatus}
{alert.newEntry && (
{alert.newEntry.documentNumber}: {alert.newEntry.description}
)}
Source: {alert.source} {alert.url && ( Open Docket in RECAP )}
))}
)}
)}
); };