From b64573decd1724d462ddc536a5a306b90d9d7c4b Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 14 Jun 2025 18:21:12 +0000 Subject: [PATCH] Revert real-time API and use polling Reverted the real-time API implementation in favor of periodic polling for signature updates due to instability. --- src/components/SystemTracker.tsx | 87 ++------------------------------ 1 file changed, 5 insertions(+), 82 deletions(-) diff --git a/src/components/SystemTracker.tsx b/src/components/SystemTracker.tsx index bde374b..48220ab 100644 --- a/src/components/SystemTracker.tsx +++ b/src/components/SystemTracker.tsx @@ -1,5 +1,5 @@ -import { useState, useEffect } from "react"; +import { useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; @@ -7,7 +7,6 @@ import { Button } from "@/components/ui/button"; import { RefreshCw, AlertCircle, Radar } from "lucide-react"; import SignatureListItem from "./SignatureListItem"; import { toast } from "@/hooks/use-toast"; -import pb from "@/lib/pocketbase"; interface SignatureItem { collectionId: string; @@ -33,9 +32,8 @@ interface SystemTrackerProps { const SystemTracker = ({ system }: SystemTrackerProps) => { const queryClient = useQueryClient(); - const [realtimeError, setRealtimeError] = useState(null); - // Query to get signatures for the current system + // Query to get signatures for the current system with polling const { data: signaturesData, refetch: refetchSignatures, @@ -60,72 +58,9 @@ const SystemTracker = ({ system }: SystemTrackerProps) => { return data; }, enabled: !!system, - refetchInterval: 30000, // Fallback polling every 30 seconds if real-time fails + refetchInterval: 5000, // Poll every 5 seconds }); - // Set up real-time subscription for signature updates with error handling - useEffect(() => { - if (!system) return; - - console.log('Setting up real-time subscription for system:', system); - setRealtimeError(null); - - let unsubscribePromise: Promise<() => void> | null = null; - - try { - // Subscribe to changes in the sigview collection - unsubscribePromise = pb.collection('sigview').subscribe('*', function (e) { - console.log('Real-time update received:', e); - - // Check if the update is for our current system - if (e.record?.system === system) { - console.log(`Signature ${e.action} for system ${system}:`, e.record); - - // Invalidate and refetch the signatures query - queryClient.invalidateQueries({ queryKey: ['signatures', system] }); - - // Show toast notification - const actionText = e.action === 'create' ? 'added' : e.action === 'update' ? 'updated' : 'removed'; - toast({ - title: "Signature Updated", - description: `Signature ${e.record?.identifier || 'unknown'} ${actionText} in ${system}`, - }); - } - }); - - // Handle subscription promise - unsubscribePromise.catch((error) => { - console.error('Real-time subscription failed:', error); - setRealtimeError('Real-time updates unavailable. Using periodic refresh instead.'); - - // Don't show error toast immediately to avoid spam - setTimeout(() => { - toast({ - title: "Real-time Connection Failed", - description: "Falling back to periodic updates every 30 seconds.", - variant: "destructive", - }); - }, 2000); - }); - - } catch (error) { - console.error('Failed to set up real-time subscription:', error); - setRealtimeError('Real-time updates unavailable. Using periodic refresh instead.'); - } - - // Cleanup subscription on unmount or system change - return () => { - if (unsubscribePromise) { - console.log('Cleaning up real-time subscription for system:', system); - unsubscribePromise - .then(unsub => unsub()) - .catch(error => { - console.error('Error cleaning up subscription:', error); - }); - } - }; - }, [system, queryClient]); - const handleRefresh = () => { refetchSignatures(); toast({ @@ -164,8 +99,8 @@ const SystemTracker = ({ system }: SystemTrackerProps) => { {system} - - {realtimeError ? 'Polling' : 'Live'} + + Polling