262 lines
11 KiB
TypeScript
262 lines
11 KiB
TypeScript
'use client';
|
||
|
||
import React, { useState, useEffect } from 'react';
|
||
import { Radio, Plus, RefreshCw, ExternalLink, ShieldCheck, Tag } from 'lucide-react';
|
||
import { InfoTooltip } from '@/components/InfoTooltip';
|
||
|
||
const API_BASE = process.env.NEXT_PUBLIC_API_URL || '';
|
||
|
||
interface Channel {
|
||
id: string;
|
||
telegram_handle: string;
|
||
title: string | null;
|
||
niche: string;
|
||
subscriber_count: number;
|
||
is_active: boolean;
|
||
last_scraped_at: string | null;
|
||
created_at: string;
|
||
}
|
||
|
||
export default function ChannelsPage() {
|
||
const [channels, setChannels] = useState<Channel[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
const [newHandle, setNewHandle] = useState('');
|
||
const [newTitle, setNewTitle] = useState('');
|
||
const [newNiche, setNewNiche] = useState('VPN & Proxy');
|
||
const [adding, setAdding] = useState(false);
|
||
|
||
const fetchChannels = async () => {
|
||
setLoading(true);
|
||
try {
|
||
const res = await fetch(`${API_BASE}/api/v1/channels`);
|
||
if (res.ok) {
|
||
const data = await res.json();
|
||
setChannels(data);
|
||
}
|
||
} catch (err) {
|
||
console.error('Failed to load channels', err);
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
useEffect(() => {
|
||
fetchChannels();
|
||
}, []);
|
||
|
||
const handleAddChannel = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
if (!newHandle) return;
|
||
setAdding(true);
|
||
try {
|
||
const res = await fetch(`${API_BASE}/api/v1/channels`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
telegram_handle: newHandle,
|
||
title: newTitle || newHandle,
|
||
niche: newNiche,
|
||
subscriber_count: 5000,
|
||
is_active: true,
|
||
}),
|
||
});
|
||
if (res.ok) {
|
||
setNewHandle('');
|
||
setNewTitle('');
|
||
fetchChannels();
|
||
} else {
|
||
alert('این کانال قبلاً اضافه شده است.');
|
||
}
|
||
} catch (err) {
|
||
alert('خطا در افزودن کانال');
|
||
} finally {
|
||
setAdding(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="max-w-7xl mx-auto space-y-8" dir="rtl">
|
||
{/* Header */}
|
||
<div className="bg-slate-900 border border-slate-800 p-6 rounded-3xl shadow-xl flex flex-col md:flex-row md:items-center justify-between gap-4">
|
||
<div>
|
||
<div className="flex items-center gap-2">
|
||
<h1 className="text-2xl font-black text-white tracking-tight">کانالهای تلگرامی تحت پایش</h1>
|
||
<InfoTooltip
|
||
title="دیسکاوری و رصد عمومی"
|
||
text="لیست کانالهای تلگرام که Agent A به صورت دورهای محتوا، عکس و ویدیوهای عمومی آنها را بدون نیاز به عضویت اسکرپ میکند."
|
||
/>
|
||
</div>
|
||
<p className="text-xs text-slate-400 mt-1">
|
||
دسترسی از طریق Telegram Web Preview (بدون مسدودی اکانت کاربری)
|
||
</p>
|
||
</div>
|
||
|
||
<button
|
||
onClick={fetchChannels}
|
||
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>
|
||
</div>
|
||
|
||
{/* Add New Channel Card */}
|
||
<div className="bg-slate-900/80 border border-slate-800 p-6 rounded-3xl shadow-lg">
|
||
<div className="flex items-center gap-2 mb-4 text-white text-sm font-bold">
|
||
<Plus className="w-4 h-4 text-indigo-400" />
|
||
<span>افزودن کانال تلگرامی جدید به پایپلاین</span>
|
||
<InfoTooltip text="شناسه کانال عمومی تلگرام (بدون @ یا با @) را وارد کنید." />
|
||
</div>
|
||
|
||
<form onSubmit={handleAddChannel} className="grid grid-cols-1 md:grid-cols-4 gap-4">
|
||
<div>
|
||
<label className="block text-[11px] text-slate-400 mb-1">شناسه کانال (Handle)</label>
|
||
<input
|
||
type="text"
|
||
placeholder="@example_channel"
|
||
value={newHandle}
|
||
onChange={(e) => setNewHandle(e.target.value)}
|
||
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-3.5 py-2 text-xs text-slate-100 focus:outline-none focus:border-indigo-500 font-mono"
|
||
required
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-[11px] text-slate-400 mb-1">عنوان کانال (اختیاری)</label>
|
||
<input
|
||
type="text"
|
||
placeholder="کانال اخبار VPN و سرور"
|
||
value={newTitle}
|
||
onChange={(e) => setNewTitle(e.target.value)}
|
||
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-3.5 py-2 text-xs text-slate-100 focus:outline-none focus:border-indigo-500"
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-[11px] text-slate-400 mb-1">دستهبندی (Niche)</label>
|
||
<select
|
||
value={newNiche}
|
||
onChange={(e) => setNewNiche(e.target.value)}
|
||
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-3.5 py-2 text-xs text-slate-100 focus:outline-none focus:border-indigo-500"
|
||
>
|
||
<option value="VPN & Proxy">VPN & Proxy</option>
|
||
<option value="Bypass Censorship">Bypass Censorship</option>
|
||
<option value="AI Tools">AI Tools</option>
|
||
<option value="Tech Tips">Tech Tips</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div className="flex items-end">
|
||
<button
|
||
type="submit"
|
||
disabled={adding}
|
||
className="w-full h-9 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 text-white rounded-xl text-xs font-semibold shadow-md shadow-indigo-600/20 transition flex items-center justify-center gap-1.5"
|
||
>
|
||
<Plus className="w-4 h-4" />
|
||
{adding ? 'در حال ثبت...' : 'افزودن به رصد'}
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
|
||
{/* Channels Table */}
|
||
<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">
|
||
<Radio 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">
|
||
{channels.length} کانال فعال
|
||
</span>
|
||
</div>
|
||
|
||
{loading ? (
|
||
<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="فعال یا غیرفعال بودن برای اجرای اسکرپر." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1">
|
||
آخرین پایش
|
||
<InfoTooltip text="زمان آخرین اجرای موفقیتآمیز استخراج اطلاعات توسط Agent A." />
|
||
</span>
|
||
</th>
|
||
<th className="py-4 px-6">لینک منبع</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody className="divide-y divide-slate-800/60 text-slate-200">
|
||
{channels.map((ch) => (
|
||
<tr key={ch.id} className="hover:bg-slate-800/40 transition">
|
||
<td className="py-4 px-6 font-mono font-medium text-indigo-400">
|
||
@{ch.telegram_handle}
|
||
</td>
|
||
<td className="py-4 px-6 font-medium text-slate-200">
|
||
{ch.title || '-'}
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<span className="inline-flex items-center gap-1 px-2.5 py-1 bg-slate-800 text-slate-300 rounded-md text-[11px]">
|
||
<Tag className="w-3 h-3 text-slate-400" />
|
||
{ch.niche}
|
||
</span>
|
||
</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 bg-emerald-500/10 text-emerald-400 border border-emerald-500/20">
|
||
<ShieldCheck className="w-3 h-3" />
|
||
فعال
|
||
</span>
|
||
</td>
|
||
<td className="py-4 px-6 font-mono text-slate-400">
|
||
{ch.last_scraped_at ? (
|
||
new Date(ch.last_scraped_at).toLocaleString('fa-IR')
|
||
) : (
|
||
<span className="text-slate-600">هنوز پایش نشده</span>
|
||
)}
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<a
|
||
href={`https://t.me/${ch.telegram_handle}`}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="text-slate-400 hover:text-indigo-400 transition inline-flex items-center gap-1"
|
||
>
|
||
کانال <ExternalLink className="w-3 h-3" />
|
||
</a>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|