"use client"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Bell, CheckCheck } from "lucide-react"; import { formatDistanceToNow } from "date-fns"; import { faIR } from "date-fns/locale"; import { Skeleton } from "@/components/ui/skeleton"; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { http } from "@/lib/api"; import type { NotificationItem } from "@/lib/types"; import { useRouter } from "next/navigation"; import { SafeMarkdown } from "@/components/safe-markdown"; export function NotificationBell() { const router = useRouter(); const qc = useQueryClient(); const { data, isLoading } = useQuery({ queryKey: ["notifications"], queryFn: () => http.get<{ notifications: NotificationItem[]; unread: number }>("/api/notifications?limit=12"), }); const unread = data?.unread ?? 0; const markAll = async () => { await http.post("/api/notifications/read-all"); qc.invalidateQueries({ queryKey: ["notifications"] }); qc.invalidateQueries({ queryKey: ["me"] }); }; const categoryMeta: Record = { progress: { label: "پیشرفت", dot: "bg-primary" }, gamification: { label: "بازی‌سازی", dot: "bg-gold" }, mentor: { label: "منتور", dot: "bg-accent" }, event: { label: "رویداد", dot: "bg-secondary" }, urgent: { label: "فوری", dot: "bg-destructive" }, curriculum: { label: "درس", dot: "bg-primary" }, community: { label: "جامعه", dot: "bg-accent" }, system: { label: "سیستم", dot: "bg-muted-foreground" }, }; return (

اعلان‌ها

{unread > 0 ? `${unread} خوانده‌نشده` : "همه خوانده شده"}

{isLoading && (
{Array.from({ length: 3 }).map((_, i) => (
))}
)} {!isLoading && (!data?.notifications || data.notifications.length === 0) && (
اعلانی ندارید.
با قبولی در یک آزمون یا ثبت‌نام در رویداد، اعلان می‌گیرید.
)} {data?.notifications.map((n) => { const cat = categoryMeta[n.category] ?? { label: "اعلان", dot: "bg-primary" }; return ( ); })}
); }