From f7bfea44c7f633f7dd11ccb5b2deaa7cef97c8ad Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 10:51:02 +0800 Subject: [PATCH] feat(portal): WhatsApp sidebar entry with unread badge, start-chat buttons on leads The badge polls the unread count every 30 s and on whatsapp-unread-changed, only for users holding BO_WHATSAPP. The button sits next to the mobile number on Leads and Event leads; it opens the chat when the starter owns it and otherwise says who does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- .../app/components/app/Sidebar.tsx | 55 ++++++++-- .../components/app/Sidebar.whatsapp.test.tsx | 100 ++++++++++++++++++ .../components/app/WhatsAppButton.test.tsx | 65 ++++++++++++ .../app/components/app/WhatsAppButton.tsx | 60 +++++++++++ .../app/components/app/icon-map.ts | 3 +- .../app/event-leads/[eventUuid]/page.tsx | 8 +- .../[eventUuid]/page.whatsapp.test.tsx | 57 ++++++++++ solvway-admin-portal/app/leads/page.tsx | 13 ++- .../app/leads/page.whatsapp.test.tsx | 44 ++++++++ solvway-admin-portal/app/lib/aclSidebar.ts | 2 +- .../app/lib/i18n/defaultMessages.ts | 6 ++ 11 files changed, 402 insertions(+), 11 deletions(-) create mode 100644 solvway-admin-portal/app/components/app/Sidebar.whatsapp.test.tsx create mode 100644 solvway-admin-portal/app/components/app/WhatsAppButton.test.tsx create mode 100644 solvway-admin-portal/app/components/app/WhatsAppButton.tsx create mode 100644 solvway-admin-portal/app/event-leads/[eventUuid]/page.whatsapp.test.tsx create mode 100644 solvway-admin-portal/app/leads/page.whatsapp.test.tsx 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 &&