"use client"; import { useEffect, useState } from "react"; import { NoPermissionState } from "@/components/auth/no-permission-state"; import { useSuperAdminSession } from "@/components/auth/session-context"; import { PageHeader } from "@/components/dashboard/page-header"; import { Badge } from "@/components/ui/badge"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { EmptyState } from "@/components/ui/empty-state"; import { useToast } from "@/components/ui/toast"; import { listModerationComments } from "@/lib/api/comments"; import { getItems } from "@/lib/api/core"; import { listPlatformNotifications } from "@/lib/api/notifications"; import { formatDateTime } from "@/lib/format"; import { getCommentAuthor, getUserLabel } from "@/lib/post-utils"; import { SUPERADMIN_PERMISSIONS, hasPermission } from "@/lib/permissions"; import type { ApiComment, NotificationItem, NotificationsResponse } from "@/types/api"; const EMPTY_NOTIFICATIONS: NotificationsResponse = { items: [], data: [], unreadCount: 0 }; export default function MessagesPage() { const { permissions } = useSuperAdminSession(); const [comments, setComments] = useState([]); const [alerts, setAlerts] = useState([]); const [loading, setLoading] = useState(true); const { toast } = useToast(); const canReadNotifications = hasPermission( permissions, SUPERADMIN_PERMISSIONS.NOTIFICATIONS_READ, ); const canModerateContent = hasPermission( permissions, SUPERADMIN_PERMISSIONS.CONTENT_MODERATE, ); useEffect(() => { let active = true; const loadData = async () => { if (!canReadNotifications && !canModerateContent) { setLoading(false); return; } setLoading(true); try { const [commentsResponse, notificationsResponse] = await Promise.all([ canModerateContent ? listModerationComments({ page: 1, limit: 8, sortOrder: "desc" }) : Promise.resolve({ items: [], data: [] }), canReadNotifications ? listPlatformNotifications({ page: 1, limit: 8, sortOrder: "desc" }) : Promise.resolve(EMPTY_NOTIFICATIONS), ]); if (!active) return; setComments(getItems(commentsResponse) as ApiComment[]); setAlerts( (getItems(notificationsResponse) as NotificationItem[]).filter((item) => ["message", "mention", "comment"].includes(item.type), ), ); } catch (error) { if (!active) return; toast({ title: "Failed to load engagement follow-up", description: String(error), variant: "danger" }); } finally { if (active) setLoading(false); } }; void loadData(); return () => { active = false; }; }, [canModerateContent, canReadNotifications, toast]); if (!canReadNotifications && !canModerateContent) { return (
); } return (
{canReadNotifications ? ( Interaction alerts {!alerts.length && !loading ? ( ) : (
{alerts.map((item) => (

{item.title ?? item.type}

{item.type}

{item.previewText ?? "-"}

{formatDateTime(item.createdAt)}

))}
)}
) : null} {canModerateContent ? ( Recent public comments {!comments.length && !loading ? ( ) : (
{comments.map((comment) => (

{getUserLabel(getCommentAuthor(comment))}

{formatDateTime(comment.createdAt)}

{comment.mentionUsernames?.length ? "mention" : "comment"}

{comment.content}

))}
)}
) : null}
); }