feat(dashboard): offline web push — service worker, subscribe flow, deep-link

Notifications phase 2: a /push-sw.js service worker shows a system notification
on push and, on click, focuses an open CRM tab (or opens one) and deep-links to
the thread. usePushNotifications registers the SW, requests permission, subscribes
with IIOS's VAPID key, and stores the subscription via the be-crm door. The topbar
bell becomes a real enable/disable control (hidden when push is unsupported/demo).
Dashboard listens for the SW's notif-click message + a ?thread= deep link.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-23 15:38:14 +05:30
parent be50d53c50
commit 041ad0e44a
5 changed files with 283 additions and 5 deletions
+19
View File
@@ -39,6 +39,25 @@ export function Dashboard() {
// eslint-disable-next-line react-hooks/set-state-in-effect
try { const t = localStorage.getItem("lup_dash_theme"); if (t === "light" || t === "dark") setTheme(t); } catch {}
}, []);
// Deep-link from a clicked push notification. Two paths from the service worker:
// - a tab was already open → it postMessages { type: 'notif-click', threadId } to focus here
// - no tab was open → it opens /dashboard?thread=<id>, which we read once on mount
useEffect(() => {
try {
const t = new URLSearchParams(window.location.search).get("thread");
if (t) {
navigateToConversation("messenger", t);
window.history.replaceState({}, "", window.location.pathname);
}
} catch {}
if (!("serviceWorker" in navigator)) return;
const onMessage = (e: MessageEvent) => {
if (e.data?.type === "notif-click" && e.data.threadId) navigateToConversation("messenger", e.data.threadId);
};
navigator.serviceWorker.addEventListener("message", onMessage);
return () => navigator.serviceWorker.removeEventListener("message", onMessage);
}, []);
function toggle() {
setTheme((t) => { const n = t === "dark" ? "light" : "dark"; try { localStorage.setItem("lup_dash_theme", n); } catch {} return n; });
}
@@ -0,0 +1,74 @@
"use client";
// Topbar bell → offline-notification control. Click opens a small popover to enable/disable Web Push
// for this browser. The dot is lit when this browser is subscribed. Hidden entirely when push isn't
// available (demo mode, or a browser without ServiceWorker/PushManager).
import { useState } from "react";
import { Icon } from "./ui";
import { usePushNotifications } from "@/lib/push-notifications";
export function NotificationBell() {
const push = usePushNotifications();
const [open, setOpen] = useState(false);
if (!push.supported) return null;
const denied = push.permission === "denied";
return (
<div style={{ position: "relative" }}>
<button
className="ic-btn"
aria-label="Notifications"
aria-haspopup="menu"
aria-expanded={open}
style={{ position: "relative" }}
onClick={() => setOpen((o) => !o)}
>
<Icon name="bell" size={18} />
<span
style={{
position: "absolute",
top: 9,
right: 10,
width: 7,
height: 7,
borderRadius: 99,
background: push.subscribed ? "var(--orange)" : "var(--border)",
border: "2px solid var(--panel)",
}}
/>
</button>
{open && (
<>
<div className="tm-menu-scrim" onClick={() => setOpen(false)} />
<div className="tm-menu-pop" role="menu" style={{ width: 260, padding: 14 }}>
<div style={{ fontWeight: 700, fontSize: 13, marginBottom: 4 }}>Offline notifications</div>
<p style={{ fontSize: 12, color: "var(--muted)", margin: "0 0 12px", lineHeight: 1.4 }}>
{push.subscribed
? "You'll get push notifications for new direct messages and mentions, even when this tab is closed."
: "Get notified about direct messages and mentions when the CRM isn't open."}
</p>
{denied ? (
<p style={{ fontSize: 12, color: "var(--danger, #c0392b)", margin: 0 }}>
Notifications are blocked in your browser settings. Allow them for this site, then try again.
</p>
) : push.subscribed ? (
<button className="ds-btn v-ghost full" disabled={push.busy} onClick={() => push.disable()}>
{push.busy ? "Turning off…" : "Turn off notifications"}
</button>
) : (
<button className="ds-btn v-primary full" disabled={push.busy} onClick={() => push.enable()}>
{push.busy ? "Enabling…" : "Enable notifications"}
</button>
)}
{push.error && <p style={{ fontSize: 11.5, color: "var(--danger, #c0392b)", margin: "10px 0 0" }}>{push.error}</p>}
</div>
</>
)}
</div>
);
}
+2 -5
View File
@@ -4,8 +4,8 @@ import { useState } from "react";
import { useRouter } from "next/navigation";
import { Sun, Moon, ChevronDown, LogOut } from "lucide-react";
import { useAuth } from "@abe-kap/appshell-sdk/react";
import { Icon } from "./ui";
import { GlobalSearch } from "./global-search";
import { NotificationBell } from "./notification-bell";
import { user } from "./account-data";
function initialsOf(name: string): string {
@@ -40,10 +40,7 @@ export function Topbar({ theme, onToggle, title, subtitle, onNavigate }: { theme
<button className="ic-btn" aria-label="Toggle theme" onClick={onToggle}>
{theme === "dark" ? <Moon size={18} /> : <Sun size={18} />}
</button>
<button className="ic-btn" aria-label="Notifications" style={{ position: "relative" }}>
<Icon name="bell" size={18} />
<span style={{ position: "absolute", top: 9, right: 10, width: 7, height: 7, borderRadius: 99, background: "var(--orange)", border: "2px solid var(--panel)" }} />
</button>
<NotificationBell />
<div className="top-user-wrap" style={{ position: "relative" }}>
<button className="top-user" onClick={() => setMenuOpen((o) => !o)} aria-haspopup="menu" aria-expanded={menuOpen}>
<span className="av" style={{ background: user.avatarGradient, display: "grid", placeItems: "center", color: "#fff", fontWeight: 700, fontSize: 12 }}>{initials}</span>