import React, { useState, useEffect, useRef } from "react"; import { LiveWireArticle, fetchLiveWireArticles } from "../connectors/news_client.js"; import { useLanguage } from "../context/LanguageContext.js"; import { Radio, RefreshCw, Zap, Clock, Globe, TrendingUp, ShieldCheck, Pause, Play, ArrowRight, Flame, Layers, ChevronRight, ChevronLeft, } from "lucide-react"; interface ContinuousLiveStreamWidgetProps { onAnalyzeNews: (headline: string, countryIso3: string) => void; onOpenFullWire: () => void; isStreaming: boolean; } export const ContinuousLiveStreamWidget: React.FC = ({ onAnalyzeNews, onOpenFullWire, isStreaming, }) => { const { language } = useLanguage(); const isArabic = language === "ar"; const [articles, setArticles] = useState([]); const [selectedCategory, setSelectedCategory] = useState("all"); const [loading, setLoading] = useState(false); const [isAutoSync, setIsAutoSync] = useState(true); const [countdown, setCountdown] = useState(25); const [lastUpdated, setLastUpdated] = useState(""); const timerRef = useRef(null); const loadStream = async (cat = selectedCategory, silent = false) => { if (!silent) setLoading(true); try { const targetCountry = cat === "local" || cat === "saudi_gulf" ? "SAU" : "GLOBAL"; const data = await fetchLiveWireArticles(cat, targetCountry); if (Array.isArray(data) && data.length > 0) { setArticles(data); setLastUpdated(new Date().toLocaleTimeString(isArabic ? "ar-SA" : "en-US")); } } catch { // Keep existing data on error } finally { if (!silent) setLoading(false); } }; useEffect(() => { loadStream(selectedCategory, false); setCountdown(25); }, [selectedCategory]); // Continuous auto-sync cycle every 25 seconds useEffect(() => { if (timerRef.current) clearInterval(timerRef.current); if (isAutoSync) { timerRef.current = setInterval(() => { setCountdown((prev) => { if (prev <= 1) { loadStream(selectedCategory, true); return 25; } return prev - 1; }); }, 1000); } return () => { if (timerRef.current) clearInterval(timerRef.current); }; }, [isAutoSync, selectedCategory]); const categories = [ { id: "all", label: isArabic ? "🌐 كافة التدفقات" : "🌐 All Wires", icon: Globe }, { id: "sovereign_decisions", label: isArabic ? "🏛️ قرارات سيادية" : "🏛️ Sovereign", icon: ShieldCheck }, { id: "economic", label: isArabic ? "📊 اقتصاد وأسواق" : "📊 Economic", icon: TrendingUp }, { id: "political", label: isArabic ? "⚖️ سياسي ودولي" : "⚖️ Geopolitics", icon: Radio }, { id: "energy_markets", label: isArabic ? "🛢️ أسواق الطاقة" : "🛢️ Energy", icon: Flame }, ]; const getSourceBadgeColor = (source: string) => { if (source.includes("SPA") || source.includes("واس")) { return "bg-emerald-950/80 text-emerald-300 border-emerald-700/60"; } if (source.includes("الشرق") || source.includes("بلومبرغ")) { return "bg-sky-950/80 text-sky-300 border-sky-700/60"; } if (source.includes("أرقام")) { return "bg-amber-950/80 text-amber-300 border-amber-700/60"; } if (source.includes("العربية")) { return "bg-indigo-950/80 text-indigo-300 border-indigo-700/60"; } if (source.includes("Reuters") || source.includes("رويترز")) { return "bg-orange-950/80 text-orange-300 border-orange-700/60"; } if (source.includes("Axios")) { return "bg-purple-950/80 text-purple-300 border-purple-700/60"; } if (source.includes("BBC")) { return "bg-rose-950/80 text-rose-300 border-rose-700/60"; } return "bg-slate-800 text-slate-300 border-slate-700"; }; const displayedArticles = articles.slice(0, 6); return (
{/* Top Header Bar */}
{isArabic ? "تغذية إخبارية مستمرة" : "CONTINUOUS STREAM"}

{isArabic ? "التدفق الإخباري اللحظي للقرارات السيادية والأسواق" : "Real-Time Sovereign & Macro Wire Stream"}

{lastUpdated && ( {lastUpdated} )}
{/* Sync Controls */}
{/* Category Filter Pills */}
{categories.map((cat) => { const active = selectedCategory === cat.id; return ( ); })}
{/* Live Articles Stream Cards */}
{loading && articles.length === 0 ? ( Array.from({ length: 3 }).map((_, idx) => (
)) ) : displayedArticles.length > 0 ? ( displayedArticles.map((article) => (
{article.source} {article.formatted_date}

onAnalyzeNews(article.title, article.country_iso3)} title={article.title} > {article.title}

{article.summary && article.summary !== article.title && (

{article.summary}

)}
{/* Instant Launch Action Button */}
{article.country_iso3 !== "GLOBAL" ? `ISO: ${article.country_iso3}` : "🌍 GLOBAL"}
)) ) : (
{isArabic ? "لا توجد أخبار جديدة في هذا التصنيف حالياً" : "No live articles found in this category"}
)}
); };