import { redirect } from "next/navigation"; import { cookies } from "next/headers"; import { apiFetch, ApiError } from "@/lib/api"; import { BlogPost, User, UserRole, CurrentUser } from "@/lib/types"; import { PostsTable } from "./components/posts-table"; import { UsersTable } from "./components/users-table"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { LayoutDashboard, Users, FileText, ShieldAlert } from "lucide-react"; // ── Decode JWT payload without verification (public payload info only) ───────── function decodeJwtPayload(token: string): CurrentUser | null { try { const base64Payload = token.split(".")[1]; const decoded = Buffer.from(base64Payload, "base64url").toString("utf-8"); return JSON.parse(decoded) as CurrentUser; } catch { return null; } } const roleColor: Record = { [UserRole.ADMIN]: "bg-red-100 text-red-800 border-red-200", [UserRole.MANAGER]: "bg-amber-100 text-amber-800 border-amber-200", [UserRole.MEMBER]: "bg-blue-100 text-blue-700 border-blue-200", }; export default async function DashboardPage() { // ── Auth check ───────────────────────────────────────────────────────────── const cookieStore = await cookies(); const accessToken = cookieStore.get("accessToken")?.value; if (!accessToken) { redirect("/auth"); } const currentUser = decodeJwtPayload(accessToken); if (!currentUser) { redirect("/auth"); } // ── Fetch posts ──────────────────────────────────────────────────────────── let posts: BlogPost[] = []; try { const data = await apiFetch<{ posts: BlogPost[]; total: number }>( "/blog-posts?pageSize=50" ); posts = data.posts; } catch (err) { if (err instanceof ApiError && err.status === 401) { redirect("/auth"); } // Non-fatal — show empty list } // ── Fetch users (ADMIN only) ─────────────────────────────────────────────── let users: User[] = []; if (currentUser.role === UserRole.ADMIN) { try { const data = await apiFetch<{ users: User[]; total: number }>( "/users?pageSize=50" ); users = data.users; } catch { // Non-fatal — show empty list } } const displayName = currentUser.name || currentUser.email; return (
{/* ── Header ──────────────────────────────────────────────────────────── */}

Dashboard

Manage your blog content and users

{displayName} {currentUser.role}
{/* ── Stats strip ─────────────────────────────────────────────────────── */}

{posts.length}

Total posts

{posts.filter((p) => p.status === "published").length}

Published

{posts.filter((p) => p.status === "draft").length}

Drafts

{/* ── Posts section ───────────────────────────────────────────────────── */}

Posts

{posts.length}
{/* ── Users section (ADMIN only) ───────────────────────────────────────── */} {currentUser.role === UserRole.ADMIN && ( <>

Users

{users.length}
{users.length === 0 ? ( No users loaded Unable to fetch user list. Check your backend connection. ) : ( )}
)} {/* ── Member notice ───────────────────────────────────────────────────── */} {currentUser.role === UserRole.MEMBER && ( Read-only access You have the MEMBER role. You can view posts but cannot create, edit, or delete them. Contact an admin to upgrade your role. )}
); }