"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { useTheme } from "next-themes"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { useUser } from "@/providers/user-provider"; import { useNotifications } from "@/providers/notification-provider"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Badge } from "@/components/ui/badge"; import { Search, Bell, Sun, Moon, Menu, LogOut, User, Settings, CheckCheck, Trash2, } from "lucide-react"; import { CrmIcon } from "./crm-icon"; interface TopbarProps { onMenuClick: () => void; } export function Topbar({ onMenuClick }: TopbarProps) { const router = useRouter(); const { theme, setTheme } = useTheme(); const { user, logout } = useUser(); const { notifications, unreadCount, markAsRead, markAllAsRead, dismiss } = useNotifications(); const [mounted, setMounted] = useState(false); const [searchOpen, setSearchOpen] = useState(false); useEffect(() => { setMounted(true); }, []); if (!user) return null; function formatTimeAgo(ts: string): string { const diff = Date.now() - new Date(ts).getTime(); const mins = Math.floor(diff / 60000); if (mins < 1) return "Just now"; if (mins < 60) return `${mins}m ago`; const hrs = Math.floor(mins / 60); if (hrs < 24) return `${hrs}h ago`; const days = Math.floor(hrs / 24); if (days < 7) return `${days}d ago`; return new Date(ts).toLocaleDateString(); } const initials = user.name .split(" ") .map((n: string) => n[0]) .join("") .toUpperCase(); return (
{/* Logo */}
CRM
{/* Mobile menu button */} {/* Search */}
{/* Mobile search toggle */} {/* Theme toggle */} {/* Notifications */} Notifications {notifications.length > 0 && ( )} {notifications.length === 0 ? (
No notifications
) : (
{notifications.slice(0, 20).map((n) => (
{ if (!n.read) markAsRead(n.id); if (n.link) router.push(n.link); }} className={`flex cursor-pointer items-start gap-3 rounded-lg p-2 transition-colors hover:bg-muted/50 ${!n.read ? "bg-muted/30" : ""}`} >

{n.title}

{n.description}

{formatTimeAgo(n.timestamp)}

))}
)} {/* User profile */}

{user.name}

{user.email}

{user.role}
router.push("/profile")}> Profile router.push("/settings")}> Settings Log out
); }