233 lines
10 KiB
TypeScript
233 lines
10 KiB
TypeScript
'use client';
|
||
|
||
import React, { useState, useEffect } from 'react';
|
||
import {
|
||
Clock, Send, RefreshCw, CheckCircle2,
|
||
AlertCircle, ExternalLink, Calendar
|
||
} from 'lucide-react';
|
||
import { InfoTooltip } from '@/components/InfoTooltip';
|
||
|
||
const API_BASE = process.env.NEXT_PUBLIC_API_URL || '';
|
||
|
||
interface QueueItem {
|
||
id: string;
|
||
draft_id: string;
|
||
scheduled_time: string;
|
||
status: 'queued' | 'publishing' | 'success' | 'failed';
|
||
telegram_msg_id: number | null;
|
||
retry_count: number;
|
||
error_log: string | null;
|
||
created_at: string;
|
||
draft?: {
|
||
id: string;
|
||
current_text: string;
|
||
brand_target: string;
|
||
raw_post?: {
|
||
post_url: string;
|
||
media_type: string;
|
||
};
|
||
};
|
||
}
|
||
|
||
export default function QueuePage() {
|
||
const [queue, setQueue] = useState<QueueItem[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
const [dispatching, setDispatching] = useState(false);
|
||
const [notice, setNotice] = useState<string | null>(null);
|
||
|
||
const fetchQueue = async () => {
|
||
setLoading(true);
|
||
try {
|
||
const res = await fetch(`${API_BASE}/api/v1/queue`);
|
||
if (res.ok) {
|
||
const data = await res.json();
|
||
setQueue(data);
|
||
}
|
||
} catch (err) {
|
||
console.error('Failed to load publish queue', err);
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
useEffect(() => {
|
||
fetchQueue();
|
||
}, []);
|
||
|
||
const handleDispatchNow = async () => {
|
||
setDispatching(true);
|
||
try {
|
||
const res = await fetch(`${API_BASE}/api/v1/pipeline/dispatch-now`, {
|
||
method: 'POST',
|
||
});
|
||
if (res.ok) {
|
||
const data = await res.json();
|
||
setNotice(`عملیات انتشار انجام شد: ${data.published_count} پست به تلگرام ارسال گردید.`);
|
||
fetchQueue();
|
||
}
|
||
} catch (err) {
|
||
setNotice('خطا در ارتباط با وبسرویس انتشار');
|
||
} finally {
|
||
setDispatching(false);
|
||
setTimeout(() => setNotice(null), 5000);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="max-w-7xl mx-auto space-y-8" dir="rtl">
|
||
{/* Header */}
|
||
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 bg-slate-900 border border-slate-800 p-6 rounded-3xl shadow-xl">
|
||
<div>
|
||
<div className="flex items-center gap-2">
|
||
<h1 className="text-2xl font-black text-white tracking-tight">صف انتشار زمانبندیشده</h1>
|
||
<InfoTooltip
|
||
title="مدیریت صف انتشار"
|
||
text="پستهای تاییدشده توسط سردبیر که به صف ارسال رسمی ربات به کانال @NetVibe_VPN منتقل شدهاند."
|
||
/>
|
||
</div>
|
||
<p className="text-xs text-slate-400 mt-1">
|
||
ورکر خودکار Agent C هر ۶۰ ثانیه این صف را بررسی کرده و پستهای سررسید شده را منتشر میکند.
|
||
</p>
|
||
</div>
|
||
|
||
<div className="flex items-center gap-3">
|
||
<button
|
||
onClick={fetchQueue}
|
||
className="flex items-center gap-2 px-4 py-2.5 bg-slate-800 hover:bg-slate-700 text-slate-200 rounded-xl text-xs font-medium transition"
|
||
>
|
||
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
|
||
بروزرسانی جدول
|
||
</button>
|
||
|
||
<button
|
||
onClick={handleDispatchNow}
|
||
disabled={dispatching}
|
||
className="flex items-center gap-2 px-5 py-2.5 bg-emerald-600 hover:bg-emerald-500 disabled:opacity-50 text-white rounded-xl text-xs font-semibold shadow-lg shadow-emerald-600/30 transition"
|
||
>
|
||
<Send className={`w-4 h-4 ${dispatching ? 'animate-pulse' : ''}`} />
|
||
{dispatching ? 'در حال ارسال...' : 'ارسال فوری پستهای موعدرسیده'}
|
||
</button>
|
||
<InfoTooltip
|
||
title="انتشار آنی"
|
||
text="فراخوانی فوری ماژول Agent C برای دیسپچ مستقیم پستهای آماده به کانال تلگرام بدون انتظار برای بازه زمانبندی."
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
{notice && (
|
||
<div className="p-4 bg-emerald-500/10 border border-emerald-500/30 rounded-2xl text-xs text-emerald-300 flex items-center justify-between animate-fadeIn">
|
||
<span>{notice}</span>
|
||
<button onClick={() => setNotice(null)} className="text-slate-400 hover:text-white">✕</button>
|
||
</div>
|
||
)}
|
||
|
||
{/* Table Section */}
|
||
<div className="bg-slate-900/90 border border-slate-800 rounded-3xl overflow-hidden shadow-xl">
|
||
<div className="p-6 border-b border-slate-800 flex items-center justify-between">
|
||
<div className="flex items-center gap-2">
|
||
<Clock className="w-5 h-5 text-indigo-400" />
|
||
<h2 className="text-base font-bold text-white">لیست پستهای صف</h2>
|
||
<InfoTooltip text="جدول کامل لاگ وضعیت دیسپچ و شناسههای پیام منتشرشده در تلگرام." />
|
||
</div>
|
||
<span className="text-xs text-slate-400 font-mono">
|
||
مجموع: {queue.length} پیام
|
||
</span>
|
||
</div>
|
||
|
||
{loading ? (
|
||
<div className="text-center py-20 text-slate-500 text-xs">در حال بارگذاری صف...</div>
|
||
) : queue.length === 0 ? (
|
||
<div className="text-center py-20 text-slate-500 text-xs">
|
||
در حال حاضر هیچ پستی در صف انتشار قرار ندارد. از صفحه «صندوق بررسی»، پیشنویسها را تایید کنید.
|
||
</div>
|
||
) : (
|
||
<div className="overflow-x-auto">
|
||
<table className="w-full text-right text-xs">
|
||
<thead className="bg-slate-950/60 text-slate-400 border-b border-slate-800 font-medium">
|
||
<tr>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
کانال مقصد
|
||
<InfoTooltip text="کانال تلگرامی که پست به آن ارسال خواهد شد." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
متن خلاصه
|
||
<InfoTooltip text="بخش ابتدایی متن تاییدشده جهت انتشار." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
زمان انتشار
|
||
<InfoTooltip text="زمان زمانبندیشده برای ارسال پست به تلگرام." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
وضعیت
|
||
<InfoTooltip text="وضعیت دیسپچر: queued (در صف)، publishing (در حال ارسال)، success (موفق)، failed (خطا)." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
شناسه پیام
|
||
<InfoTooltip text="شناسه پیام یکتا در کانال تلگرام که پس از ارسال موفقیتآمیز بازگردانده میشود." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
تلاش مجدد
|
||
<InfoTooltip text="تعداد دفعات تلاش برای ارسال در صورت بروز خطا در شبکه تلگرام." />
|
||
</span>
|
||
</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody className="divide-y divide-slate-800/60 text-slate-200">
|
||
{queue.map((item) => (
|
||
<tr key={item.id} className="hover:bg-slate-800/40 transition">
|
||
<td className="py-4 px-6 font-mono font-medium text-indigo-400">
|
||
{item.draft?.brand_target || '@NetVibe_VPN'}
|
||
</td>
|
||
<td className="py-4 px-6 max-w-xs truncate text-slate-300">
|
||
{item.draft?.current_text ? item.draft.current_text.slice(0, 75) + '...' : '-'}
|
||
</td>
|
||
<td className="py-4 px-6 font-mono text-slate-400">
|
||
<div className="flex items-center gap-1.5">
|
||
<Calendar className="w-3.5 h-3.5 text-slate-500" />
|
||
<span>{new Date(item.scheduled_time).toLocaleString('fa-IR')}</span>
|
||
</div>
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<span className={`inline-flex items-center gap-1 px-2.5 py-1 rounded-md text-[11px] font-medium ${
|
||
item.status === 'success' ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' :
|
||
item.status === 'queued' ? 'bg-amber-500/10 text-amber-400 border border-amber-500/20' :
|
||
item.status === 'publishing' ? 'bg-indigo-500/10 text-indigo-400 border border-indigo-500/20' :
|
||
'bg-rose-500/10 text-rose-400 border border-rose-500/20'
|
||
}`}>
|
||
{item.status === 'success' && <CheckCircle2 className="w-3 h-3" />}
|
||
{item.status === 'queued' && <Clock className="w-3 h-3" />}
|
||
{item.status === 'failed' && <AlertCircle className="w-3 h-3" />}
|
||
{item.status}
|
||
</span>
|
||
</td>
|
||
<td className="py-4 px-6 font-mono text-slate-400">
|
||
{item.telegram_msg_id ? (
|
||
<span className="text-emerald-400 font-semibold">#{item.telegram_msg_id}</span>
|
||
) : (
|
||
<span className="text-slate-600">-</span>
|
||
)}
|
||
</td>
|
||
<td className="py-4 px-6 font-mono text-slate-400">
|
||
{item.retry_count}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|