Files
canina/frontend/admin-panel/src/pages/CompanyOrchestrationHQ.tsx
T

353 lines
16 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import {
Building2,
CheckCircle2,
Clock,
AlertTriangle,
ArrowUpRight,
RefreshCw,
ShieldCheck,
Briefcase,
FileText,
Stethoscope,
Search,
Headphones,
Cpu,
Layers,
Terminal,
Activity,
XCircle,
} from 'lucide-react';
import { Link } from 'react-router-dom';
import api from '../services/api';
interface DepartmentCard {
id: string;
name: string;
role: string;
agentName: string;
avatarColor: string;
icon: any;
status: 'ACTIVE' | 'PROCESSING' | 'STANDBY';
currentFocus: string;
pendingApprovals: number;
completedTasks: number;
recentReasoningSnippet: string;
}
const BASE_DEPARTMENTS: Omit<DepartmentCard, 'status' | 'pendingApprovals' | 'completedTasks' | 'recentReasoningSnippet' | 'currentFocus'>[] = [
{
id: 'CONTENT',
name: 'واحد محتوا و وبلاگ تخصصی',
role: 'تحقیق عمیق، مقالات بالینی، تولید تصویر و سئوی محتوایی',
agentName: 'Canina-Content-Agent',
avatarColor: 'from-blue-600 to-indigo-700',
icon: FileText,
},
{
id: 'CLINICAL_RD',
name: 'واحد تحقیق و توسعه بالینی (R&D)',
role: 'فکت‌چکینگ پزشکی، اعتبارسنجی دوزها و مراجع داروسازی آلمان',
agentName: 'Canina-Medical-Agent',
avatarColor: 'from-emerald-600 to-teal-700',
icon: Stethoscope,
},
{
id: 'SEO',
name: 'واحد سئو و هوش بازار',
role: 'پایش سرچ کنسول، شناسایی کلمات کلیدی، بهینه‌سازی اسکیما و فید ترب',
agentName: 'Canina-SEO-Agent',
avatarColor: 'from-amber-500 to-orange-600',
icon: Search,
},
{
id: 'B2B',
name: 'واحد توسعه سازمانی و B2B',
role: 'پایپ‌لاین کلینیک‌ها، بیمارستان‌های دامپزشکی و بسته‌های نمایندگی',
agentName: 'Canina-B2B-Agent',
avatarColor: 'from-purple-600 to-violet-800',
icon: Building2,
},
{
id: 'DEVOPS',
name: 'واحد مهندسی، زیرساخت و امنیت',
role: 'پایش سلامت کانتینرها، مصرف دیسک، SSL و پایپ‌لاین آزمون‌محور TDD',
agentName: 'Canina-DevOps-Agent',
avatarColor: 'from-rose-600 to-red-700',
icon: Cpu,
},
{
id: 'SUPPORT',
name: 'واحد پشتیبانی و مشاوره هوشمند',
role: 'پاسخ‌گویی به مشتریان، تریاژ پرسش‌های پت و ثبت تیکت‌های پیگیری',
agentName: 'Canina-Support-Agent',
avatarColor: 'from-cyan-600 to-blue-700',
icon: Headphones,
}
];
export default function CompanyOrchestrationHQ() {
const [departments, setDepartments] = useState<DepartmentCard[]>([]);
const [totalPendingApprovals, setTotalPendingApprovals] = useState<number>(0);
const [totalTasksCount, setTotalTasksCount] = useState<number>(0);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [hasError, setHasError] = useState<boolean>(false);
const fetchLiveStatus = async () => {
setIsLoading(true);
setHasError(false);
try {
const [_statsRes, tasksRes, approvalsRes] = await Promise.all([
api.get('/admin/department-tasks/stats'),
api.get('/admin/department-tasks?limit=100'),
api.get('/admin/approvals?limit=100')
]);
const tasks = tasksRes.data?.data || [];
const approvals = approvalsRes.data?.data || [];
// Calculate operational pending approvals (excluding non-authorization items)
const pendingApprovalsList = approvals.filter((a: any) =>
a.status === 'PENDING' && a.category !== 'B2B_SUPPLIER_STRATEGY'
);
setTotalPendingApprovals(pendingApprovalsList.length);
setTotalTasksCount(tasks.length);
const mapped: DepartmentCard[] = BASE_DEPARTMENTS.map(base => {
const deptTasks = tasks.filter((t: any) =>
t.department === base.id || (base.id === 'DEVOPS' && t.department === 'DEV')
);
const inProgress = deptTasks.find((t: any) => t.status === 'IN_PROGRESS');
const latestTask = deptTasks[0];
const completedCount = deptTasks.filter((t: any) => t.status === 'DONE').length;
let status: 'ACTIVE' | 'PROCESSING' | 'STANDBY' = 'STANDBY';
if (inProgress) status = 'ACTIVE';
else if (deptTasks.some((t: any) => t.status === 'TEST' || t.status === 'WAITING_DEPENDENCIES')) {
status = 'PROCESSING';
}
let currentFocus = 'آماده‌باش / بدون وظیفه فعال در حال حاضر';
if (inProgress) currentFocus = inProgress.title;
else if (latestTask) currentFocus = latestTask.title;
let snippet = 'بدون ثبت لاگ اجرایی در وظایف اخیر.';
if (latestTask?.executionLogs?.length) {
const lastLog = latestTask.executionLogs[latestTask.executionLogs.length - 1];
snippet = lastLog?.summary || lastLog?.note || (lastLog?.deliverable?.type ? `تحویل خروجی: ${lastLog.deliverable.type}` : snippet);
}
// Real department-specific pending approvals (strictly from live data, no synthetic telemetry)
const deptPendingCount = pendingApprovalsList.filter((a: any) =>
a.department === base.id || (base.id === 'DEVOPS' && a.department === 'DEV')
).length;
return {
...base,
status,
currentFocus,
pendingApprovals: deptPendingCount,
completedTasks: completedCount,
recentReasoningSnippet: snippet
};
});
setDepartments(mapped);
} catch (err) {
console.error('Error fetching live orchestration telemetry:', err);
setHasError(true);
} finally {
setIsLoading(false);
}
};
useEffect(() => {
fetchLiveStatus();
}, []);
return (
<div className="space-y-6 font-vazir" dir="rtl">
{/* Honest Executive Header */}
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 bg-white p-6 rounded-3xl border border-gray-200 shadow-sm">
<div>
<div className="flex items-center gap-3">
<div className="p-3 bg-purple-100 text-purple-700 rounded-2xl border border-purple-200">
<Building2 className="w-8 h-8" />
</div>
<div>
<h1 className="text-xl md:text-2xl font-black text-gray-900 flex items-center gap-3">
وضعیت و ارکستراسیون دپارتمان‌ها (Department Governance Status)
{hasError ? (
<span className="text-[10px] px-2.5 py-1 rounded-full font-black bg-rose-100 text-rose-800 border border-rose-300">
خطا در همگام‌سازی وضعیت
</span>
) : isLoading ? (
<span className="text-[10px] px-2.5 py-1 rounded-full font-black bg-blue-100 text-blue-800 border border-blue-300">
در حال دریافت داده‌ها...
</span>
) : (
<span className="text-[10px] px-2.5 py-1 rounded-full font-black bg-emerald-100 text-emerald-800 border border-emerald-300">
داده‌های زنده سیستم
</span>
)}
</h1>
<p className="text-xs text-gray-500 mt-1 font-medium">
پایش وظایف، وضعیت و خروجی عملیاتی دپارتمان‌ها و ایجنت‌ها بر اساس داده‌های واقعی سرویس
</p>
</div>
</div>
</div>
<div className="flex flex-wrap items-center gap-2.5">
<Link
to="/manager-actions"
className="flex items-center gap-1.5 px-3.5 py-2 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 border border-indigo-200 rounded-xl text-xs font-bold transition-all"
>
<Briefcase className="w-4 h-4 text-indigo-600" />
<span>اقدامات مدیریتی و همکاری‌ها</span>
</Link>
<Link
to="/approvals"
className="flex items-center gap-1.5 px-3.5 py-2 bg-amber-50 hover:bg-amber-100 text-amber-800 border border-amber-300 rounded-xl text-xs font-bold transition-all"
>
<ShieldCheck className="w-4 h-4 text-amber-600" />
<span>مجوزهای عملیاتی ({totalPendingApprovals} مورد)</span>
</Link>
<button
type="button"
onClick={fetchLiveStatus}
disabled={isLoading}
className="inline-flex items-center gap-1.5 px-3 py-2 rounded-xl bg-gray-100 hover:bg-gray-200 text-gray-700 text-xs font-bold transition-all border border-gray-200 cursor-pointer disabled:opacity-50"
>
<RefreshCw className={`w-3.5 h-3.5 ${isLoading ? 'animate-spin' : ''}`} />
<span>تازه‌سازی</span>
</button>
</div>
</div>
{/* Honest Error State */}
{hasError ? (
<div className="bg-white p-8 rounded-3xl border border-rose-200 shadow-sm text-center space-y-3">
<XCircle className="w-12 h-12 text-rose-500 mx-auto" />
<h3 className="text-base font-black text-gray-900">داده‌های وضعیت در دسترس نیست</h3>
<p className="text-xs text-gray-500 max-w-md mx-auto">
امکان ارتباط با سرویس ارکستراسیون یا دریافت آمار وظایف دپارتمان‌ها وجود ندارد. به منظور حفظ صداقت عملیاتی، هیچ وضعیت شبیه‌سازی‌شده‌ای رندر نمی‌شود.
</p>
<button
type="button"
onClick={fetchLiveStatus}
className="px-4 py-2 bg-purple-600 hover:bg-purple-700 text-white rounded-xl text-xs font-bold transition-all"
>
تلاش مجدد جهت اتصال
</button>
</div>
) : isLoading ? (
<div className="py-24 flex flex-col justify-center items-center gap-3">
<RefreshCw className="w-8 h-8 text-purple-600 animate-spin" />
<span className="text-xs font-bold text-gray-500">در حال واکشی وضعیت واقعی دپارتمان‌ها...</span>
</div>
) : (
<>
{/* Honest Metrics Summary */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
<div className="bg-white p-4 rounded-2xl border border-gray-200 shadow-xs">
<span className="text-[11px] font-bold text-gray-500">کل دپارتمان‌های سازمانی</span>
<p className="text-2xl font-black text-gray-900 mt-1 font-mono">{departments.length}</p>
</div>
<div className="bg-white p-4 rounded-2xl border border-gray-200 shadow-xs">
<span className="text-[11px] font-bold text-gray-500">کل وظایف ثبت‌شده</span>
<p className="text-2xl font-black text-gray-900 mt-1 font-mono">{totalTasksCount}</p>
</div>
<div className="bg-white p-4 rounded-2xl border border-amber-200 shadow-xs">
<span className="text-[11px] font-bold text-amber-700">مجوزهای عملیاتی معلق</span>
<p className="text-2xl font-black text-amber-700 mt-1 font-mono">{totalPendingApprovals}</p>
</div>
<div className="bg-white p-4 rounded-2xl border border-emerald-200 shadow-xs">
<span className="text-[11px] font-bold text-emerald-700">دپارتمان‌های در حال کار</span>
<p className="text-2xl font-black text-emerald-700 mt-1 font-mono">
{departments.filter(d => d.status === 'ACTIVE').length}
</p>
</div>
</div>
{/* Department Cards Grid */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-5">
{departments.map((dept) => (
<div
key={dept.id}
className="bg-white border border-gray-200 rounded-3xl p-5 hover:border-purple-300 hover:shadow-md transition-all flex flex-col justify-between shadow-xs"
>
<div>
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-3">
<div className={`p-3 rounded-2xl bg-gradient-to-br ${dept.avatarColor} text-white shadow-xs`}>
<dept.icon className="w-5 h-5" />
</div>
<div>
<h3 className="text-sm font-black text-gray-900">{dept.name}</h3>
<p className="text-[11px] text-purple-700 font-bold">{dept.agentName}</p>
</div>
</div>
<span className={`text-[10px] font-black px-2 py-0.5 rounded-full border ${
dept.status === 'ACTIVE'
? 'bg-emerald-50 text-emerald-800 border-emerald-200'
: dept.status === 'PROCESSING'
? 'bg-amber-50 text-amber-800 border-amber-200'
: 'bg-gray-100 text-gray-600 border-gray-200'
}`}>
{dept.status === 'ACTIVE' ? 'در حال کار' : dept.status === 'PROCESSING' ? 'در حال تست' : 'آماده‌باش'}
</span>
</div>
<p className="text-xs text-gray-600 mt-3 font-medium leading-relaxed">
{dept.role}
</p>
<div className="mt-4 p-3 rounded-2xl bg-gray-50 border border-gray-100 space-y-1">
<div className="text-[10px] font-bold text-gray-500 flex items-center gap-1.5">
<Activity className="w-3.5 h-3.5 text-purple-600" />
<span>تمرکز فعلی دپارتمان:</span>
</div>
<p className="text-xs text-gray-800 font-medium line-clamp-1">
{dept.currentFocus}
</p>
</div>
<div className="mt-2 p-3 rounded-2xl bg-gray-50 border border-gray-100 space-y-1">
<div className="text-[10px] font-bold text-gray-500 flex items-center gap-1.5">
<Terminal className="w-3.5 h-3.5 text-purple-600" />
<span>آخرین لاگ و استدلال واقعی:</span>
</div>
<p className="text-[11px] text-gray-700 font-mono leading-relaxed line-clamp-2">
{dept.recentReasoningSnippet}
</p>
</div>
</div>
<div className="mt-5 pt-4 border-t border-gray-100 flex items-center justify-between text-xs">
<div className="space-x-2 space-x-reverse text-gray-600">
<span>وظایف انجام‌شده: <strong className="text-gray-900 font-bold">{dept.completedTasks}</strong></span>
{dept.pendingApprovals > 0 && (
<span className="text-amber-700 font-bold">({dept.pendingApprovals} مجوز معلق)</span>
)}
</div>
<Link
to="/tasks"
className="flex items-center gap-1 text-purple-700 hover:text-purple-800 font-bold text-xs"
>
<span>مشاهده وظایف</span>
<ArrowUpRight className="w-3.5 h-3.5" />
</Link>
</div>
</div>
))}
</div>
</>
)}
</div>
);
}