File size: 12,304 Bytes
f453a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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<ContinuousLiveStreamWidgetProps> = ({
  onAnalyzeNews,
  onOpenFullWire,
  isStreaming,
}) => {
  const { language } = useLanguage();
  const isArabic = language === "ar";

  const [articles, setArticles] = useState<LiveWireArticle[]>([]);
  const [selectedCategory, setSelectedCategory] = useState<string>("all");
  const [loading, setLoading] = useState(false);
  const [isAutoSync, setIsAutoSync] = useState(true);
  const [countdown, setCountdown] = useState(25);
  const [lastUpdated, setLastUpdated] = useState<string>("");

  const timerRef = useRef<NodeJS.Timeout | null>(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 (
    <div className="bg-slate-900/90 border border-slate-800 rounded-xl p-4 sm:p-5 shadow-lg relative overflow-hidden mb-6">
      {/* Top Header Bar */}
      <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-3 border-b border-slate-800">
        <div className="flex items-center gap-2.5 flex-wrap">
          <div className="flex items-center gap-1.5 bg-emerald-950/70 border border-emerald-800 text-emerald-400 font-mono text-[11px] font-bold px-2 py-0.5 rounded">
            <span className="relative flex h-2 w-2">
              <span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75"></span>
              <span className="relative inline-flex rounded-full h-2 w-2 bg-emerald-500"></span>
            </span>
            <span>{isArabic ? "تغذية إخبارية مستمرة" : "CONTINUOUS STREAM"}</span>
          </div>

          <h2 className="text-sm font-bold text-white font-display flex items-center gap-1.5">
            <Radio className="w-4 h-4 text-cyan-400" />
            <span>{isArabic ? "التدفق الإخباري اللحظي للقرارات السيادية والأسواق" : "Real-Time Sovereign & Macro Wire Stream"}</span>
          </h2>

          {lastUpdated && (
            <span className="text-[11px] font-mono text-slate-400 flex items-center gap-1">
              <Clock className="w-3 h-3 text-slate-500" />
              <span>{lastUpdated}</span>
            </span>
          )}
        </div>

        {/* Sync Controls */}
        <div className="flex items-center gap-2 shrink-0">
          <div className="flex items-center gap-1.5 text-[11px] font-mono bg-slate-950/80 px-2.5 py-1 rounded border border-slate-800 text-slate-300">
            <button
              onClick={() => setIsAutoSync(!isAutoSync)}
              className="flex items-center gap-1 text-slate-400 hover:text-cyan-300 transition-colors cursor-pointer"
              title={isAutoSync ? (isArabic ? "إيقاف التحديث المستمر" : "Pause Auto-Sync") : (isArabic ? "تشغيل التحديث المستمر" : "Resume Auto-Sync")}
            >
              {isAutoSync ? (
                <>
                  <Pause className="w-3 h-3 text-cyan-400" />
                  <span className="text-cyan-400">{countdown}s</span>
                </>
              ) : (
                <>
                  <Play className="w-3 h-3 text-amber-400" />
                  <span className="text-amber-400">{isArabic ? "متوقف" : "Paused"}</span>
                </>
              )}
            </button>
          </div>

          <button
            onClick={() => loadStream(selectedCategory, false)}
            disabled={loading}
            className="p-1.5 bg-slate-800 hover:bg-slate-700 text-slate-300 hover:text-cyan-300 rounded border border-slate-700 transition-all cursor-pointer disabled:opacity-40"
            title={isArabic ? "تحديث يدوي فوري" : "Force Refresh"}
          >
            <RefreshCw className={`w-3.5 h-3.5 ${loading ? "animate-spin text-cyan-400" : ""}`} />
          </button>

          <button
            onClick={onOpenFullWire}
            className="flex items-center gap-1 text-[11px] font-mono text-cyan-400 hover:text-cyan-300 bg-cyan-950/50 hover:bg-cyan-950 border border-cyan-800/80 px-2.5 py-1 rounded transition-all cursor-pointer"
          >
            <span>{isArabic ? "رصد كامل" : "Full Feed"}</span>
            <ChevronRight className="w-3 h-3 rtl:rotate-180" />
          </button>
        </div>
      </div>

      {/* Category Filter Pills */}
      <div className="flex items-center gap-1.5 overflow-x-auto py-2.5 scrollbar-none">
        {categories.map((cat) => {
          const active = selectedCategory === cat.id;
          return (
            <button
              key={cat.id}
              onClick={() => setSelectedCategory(cat.id)}
              className={`px-2.5 py-1 rounded text-[11px] font-mono shrink-0 transition-all cursor-pointer ${
                active
                  ? "bg-cyan-500/20 text-cyan-300 border border-cyan-500/50 font-semibold"
                  : "bg-slate-950/60 text-slate-400 hover:text-slate-200 border border-slate-800/80 hover:bg-slate-800/60"
              }`}
            >
              {cat.label}
            </button>
          );
        })}
      </div>

      {/* Live Articles Stream Cards */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2.5 mt-1">
        {loading && articles.length === 0 ? (
          Array.from({ length: 3 }).map((_, idx) => (
            <div
              key={idx}
              className="bg-slate-950/60 border border-slate-800/60 rounded-lg p-3 animate-pulse space-y-2"
            >
              <div className="h-4 bg-slate-800 rounded w-1/3"></div>
              <div className="h-5 bg-slate-800 rounded w-full"></div>
              <div className="h-3 bg-slate-800 rounded w-2/3"></div>
            </div>
          ))
        ) : displayedArticles.length > 0 ? (
          displayedArticles.map((article) => (
            <div
              key={article.id}
              className="bg-slate-950/70 hover:bg-slate-950 border border-slate-800/80 hover:border-cyan-800/60 rounded-lg p-3 transition-all flex flex-col justify-between group shadow-sm"
            >
              <div>
                <div className="flex items-center justify-between gap-2 mb-1.5">
                  <span
                    className={`text-[10px] font-mono px-2 py-0.5 rounded border font-semibold ${getSourceBadgeColor(
                      article.source
                    )}`}
                  >
                    {article.source}
                  </span>
                  <span className="text-[10px] font-mono text-slate-400 flex items-center gap-1">
                    <Clock className="w-2.5 h-2.5 text-slate-500" />
                    {article.formatted_date}
                  </span>
                </div>

                <h3
                  className="text-xs font-semibold text-slate-200 group-hover:text-cyan-300 transition-colors line-clamp-2 leading-snug cursor-pointer mb-1.5"
                  onClick={() => onAnalyzeNews(article.title, article.country_iso3)}
                  title={article.title}
                >
                  {article.title}
                </h3>

                {article.summary && article.summary !== article.title && (
                  <p className="text-[11px] text-slate-400 line-clamp-2 leading-relaxed mb-3">
                    {article.summary}
                  </p>
                )}
              </div>

              {/* Instant Launch Action Button */}
              <div className="pt-2 border-t border-slate-800/60 flex items-center justify-between mt-auto">
                <span className="text-[10px] font-mono text-slate-400">
                  {article.country_iso3 !== "GLOBAL" ? `ISO: ${article.country_iso3}` : "🌍 GLOBAL"}
                </span>

                <button
                  onClick={() => onAnalyzeNews(article.title, article.country_iso3)}
                  disabled={isStreaming}
                  className="flex items-center gap-1 px-2.5 py-1 rounded bg-cyan-500/10 hover:bg-cyan-500/20 text-cyan-300 hover:text-cyan-200 border border-cyan-500/30 hover:border-cyan-400 font-mono text-[11px] font-semibold transition-all cursor-pointer disabled:opacity-40"
                  title={isArabic ? "تشغيل النموذج واستشراف الأثر فوراً" : "Run instant forecast on this event"}
                >
                  <Zap className="w-3 h-3 text-cyan-400 fill-cyan-400/30" />
                  <span>{isArabic ? "تحليل فوري" : "Forecast"}</span>
                </button>
              </div>
            </div>
          ))
        ) : (
          <div className="col-span-full py-8 text-center text-xs font-mono text-slate-400 bg-slate-950/40 rounded-lg border border-slate-800">
            {isArabic ? "لا توجد أخبار جديدة في هذا التصنيف حالياً" : "No live articles found in this category"}
          </div>
        )}
      </div>
    </div>
  );
};