// frontend/app/(protected)/dashboard/page.jsx 'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Activity, File, FilePlus, ArrowRight, BellDot, Settings } from 'lucide-react'; import { api } from '@/lib/api'; import { useAuth } from '@/lib/auth'; import { can } from '@/lib/rbac'; export default function DashboardPage() { const { user } = useAuth(); const [stats, setStats] = useState(null); const [myTasks, setMyTasks] = useState([]); const [recentActivity, setRecentActivity] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const fetchData = async () => { try { setLoading(true); // เรียก API ที่จำเป็นสำหรับ Dashboard พร้อมกัน // หมายเหตุ: Endpoint เหล่านี้อาจจะต้องสร้างเพิ่มเติมในฝั่ง Backend const [statsRes, tasksRes, activityRes] = await Promise.all([ api.get('/dashboard/stats').catch(e => ({ data: { totalDocuments: 0, newThisWeek: 0, pendingRfas: 0 }})), // สมมติ endpoint สำหรับ KPI api.get('/dashboard/my-tasks').catch(e => ({ data: [] })), // สมมติ endpoint สำหรับ Action Items api.get('/dashboard/recent-activity').catch(e => ({ data: [] })), // สมมติ endpoint สำหรับ Recent Activity ]); setStats(statsRes.data); setMyTasks(tasksRes.data); setRecentActivity(activityRes.data); } catch (error) { console.error("Failed to fetch dashboard data:", error); } finally { setLoading(false); } }; fetchData(); }, []); if (loading) { // อาจจะใช้ Skeleton UI ที่นี่เพื่อให้ UX ดีขึ้น return
Loading Dashboard...
; } return (
{/* ส่วน Header และ Quick Access Buttons */}

Dashboard

Welcome back, {user?.username || 'User'}!

{/* ปุ่ม Admin Settings จะแสดงเมื่อมีสิทธิ์ 'manage_users' เท่านั้น */} {user && can(user, 'manage_users') && ( )}
{/* ส่วน Key Metrics (KPIs) */}
Total Documents
{stats?.totalDocuments || 0}

+{stats?.newThisWeek || 0} this week

Pending RFAs
{stats?.pendingRfas || 0}

Require your attention

{/* สามารถเพิ่ม Card อื่นๆ ตามต้องการ */}
{/* ส่วน Action Items และ Recent Activity */}
{/* Action Items */} My Action Items Tasks that require your immediate attention. {myTasks && myTasks.length > 0 ? (
    {myTasks.map(task => (
  • {task.title}
  • ))}
) : (

No pending tasks. You're all caught up!

)}
{/* Recent Activity */} Recent Project Activity Latest updates from the team. {recentActivity && recentActivity.length > 0 ? (
    {recentActivity.map(activity => (
  • {new Date(activity.timestamp).toLocaleString()}

  • ))}
) : (

No recent activity.

)}
); }