import { useEffect, useState } from 'react'; import { Card } from '@/components/ui/card'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, ReferenceLine } from 'recharts'; import { supabase } from '@/integrations/supabase/client'; type DataPoint = { time: string; coherence: number; lighthouse: number; }; export const CoherenceTrendChart = () => { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetchData(); // Realtime subscription const channel = supabase .channel('coherence_updates') .on( 'postgres_changes', { event: 'INSERT', schema: 'public', table: 'lighthouse_events', }, (payload) => { const newEvent = payload.new as any; const newPoint: DataPoint = { time: new Date(newEvent.timestamp).toLocaleTimeString(), coherence: Number(newEvent.coherence), lighthouse: Number(newEvent.lighthouse_signal), }; setData((prev) => [...prev.slice(-50), newPoint]); } ) .subscribe(); return () => { supabase.removeChannel(channel); }; }, []); const fetchData = async () => { try { const { data: events, error } = await supabase .from('lighthouse_events') .select('timestamp, coherence, lighthouse_signal') .order('timestamp', { ascending: false }) .limit(50); if (error) throw error; const chartData = (events || []) .reverse() .map((event) => ({ time: new Date(event.timestamp).toLocaleTimeString(), coherence: Number(event.coherence), lighthouse: Number(event.lighthouse_signal), })); setData(chartData); } catch (error) { console.error('Error fetching coherence data:', error); } finally { setLoading(false); } }; if (loading) { return (

Coherence & Lighthouse Trends

Loading data...

); } return (

Coherence & Lighthouse Trends

Real-time tracking of field coherence Γ and Lighthouse signal L(t)

{data.length === 0 && (

No data yet. Start the field on /aureon to begin tracking.

)}
); };