diff --git a/solvway-admin-portal/app/components/app/Sidebar.tsx b/solvway-admin-portal/app/components/app/Sidebar.tsx index 4657ed4..a89a064 100644 --- a/solvway-admin-portal/app/components/app/Sidebar.tsx +++ b/solvway-admin-portal/app/components/app/Sidebar.tsx @@ -12,6 +12,8 @@ import { cn } from "@/app/lib/utils"; import { Button } from "@/app/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "@/app/components/ui/popover"; import PoweredBy from "./PoweredBy"; +import { usePolling } from "@/app/lib/hooks/usePolling"; +import { unreadCount } from "@/app/lib/services/whatsapp"; // The wordmark is the brand name: data, not translatable copy. const BRAND = "Co-Lite"; @@ -26,7 +28,26 @@ const BRAND = "Co-Lite"; type NavProps = { trail: SidebarLink[]; onNavigate?: () => void }; -function NavNode({ link, depth, trail, onNavigate }: { link: SidebarLink; depth: number } & NavProps) { +const WHATSAPP = "BO_WHATSAPP"; + +/** Unread WhatsApp chats for the signed-in user; 0 without the module. A failed read keeps the last count. */ +function useWhatsAppUnread(enabled: boolean) { + const [count, setCount] = useState(0); + usePolling( + () => { + unreadCount().then((r) => setCount(r?.count ?? 0), () => {}); + }, + 30_000, + "whatsapp-unread-changed", + enabled, + ); + return enabled ? count : 0; +} + +const badgeText = (n: number) => (n > 99 ? "99+" : String(n)); +const badgeClass = "rounded-full bg-primary px-1.5 text-xs leading-5 font-semibold text-primary-foreground tabular-nums"; + +function NavNode({ link, depth, trail, onNavigate, badge = 0 }: { link: SidebarLink; depth: number; badge?: number } & NavProps) { const t = useT(); const Icon = iconFor(link.iconName); const hasChildren = link.children.length > 0; @@ -38,6 +59,7 @@ function NavNode({ link, depth, trail, onNavigate }: { link: SidebarLink; depth: 0 ? `${t(link.name)} ${t("WaUnread", { n: badge })}` : undefined} onClick={onNavigate} className={cn( "flex items-center gap-3 rounded-lg px-3 transition-colors", @@ -53,6 +75,11 @@ function NavNode({ link, depth, trail, onNavigate }: { link: SidebarLink; depth: reference it returns is stable across renders for a given iconName. */} {Icon &&