forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification-bell.tsx
More file actions
116 lines (109 loc) · 3.99 KB
/
Copy pathnotification-bell.tsx
File metadata and controls
116 lines (109 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use client'
import { useState, useRef, useEffect } from 'react'
import { Bell } from 'lucide-react'
import { formatTimeAgo } from '@/lib/stream-utils'
import { useWallet } from '@/hooks/use-wallet'
import { useNotifications, type AppNotification } from '@/hooks/use-notifications'
function NotificationItem({ notification }: { notification: AppNotification }) {
return (
<div
className={
'border-b border-border px-4 py-3 last:border-0 ' +
(notification.read ? 'opacity-60' : '')
}
>
<div className="flex items-start justify-between gap-2">
<p className="text-sm font-medium">{notification.title}</p>
{!notification.read && (
<span className="mt-1 size-2 shrink-0 rounded-full bg-primary" />
)}
</div>
<p className="mt-0.5 text-xs text-muted-foreground">{notification.body}</p>
<p className="mt-1 text-xs text-muted-foreground">{formatTimeAgo(notification.timestamp)}</p>
</div>
)
}
export function NotificationBell() {
const { address } = useWallet()
const { notifications, unreadCount, markAllRead, clearAll } = useNotifications(address)
const [open, setOpen] = useState(false)
const ref = useRef<HTMLDivElement>(null)
const triggerRef = useRef<HTMLButtonElement>(null)
useEffect(() => {
function handleClickOutside(e: MouseEvent) {
if (ref.current && !ref.current.contains(e.target as Node)) {
setOpen(false)
}
}
function handleKeyDown(e: KeyboardEvent) {
if (e.key === 'Escape' && open) {
setOpen(false)
triggerRef.current?.focus()
}
}
if (open) {
document.addEventListener('mousedown', handleClickOutside)
document.addEventListener('keydown', handleKeyDown)
}
return () => {
document.removeEventListener('mousedown', handleClickOutside)
document.removeEventListener('keydown', handleKeyDown)
}
}, [open])
function handleToggle() {
setOpen((v) => !v)
if (!open && unreadCount > 0) {
markAllRead()
}
}
return (
<div className="relative" ref={ref}>
<button
ref={triggerRef}
onClick={handleToggle}
aria-expanded={open}
aria-haspopup="true"
className="relative rounded-md p-2 text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
aria-label={`Notifications${unreadCount > 0 ? ` (${unreadCount} unread)` : ''}`}
data-testid="notification-bell-trigger"
>
<Bell className="size-5" />
{unreadCount > 0 && (
<span className="absolute -right-0.5 -top-0.5 flex size-4.5 items-center justify-center rounded-full bg-primary text-[10px] font-bold text-primary-foreground">
{unreadCount > 9 ? '9+' : unreadCount}
</span>
)}
</button>
{open && (
<div
role="dialog"
aria-label="Notifications"
className="absolute right-0 top-full z-50 mt-2 w-80 rounded-xl border border-border bg-card shadow-lg"
>
<div className="flex items-center justify-between border-b border-border px-4 py-3">
<h3 className="text-sm font-medium">Notifications</h3>
{notifications.length > 0 && (
<button
onClick={clearAll}
className="text-xs text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:rounded"
>
Clear all
</button>
)}
</div>
<div className="max-h-80 overflow-y-auto">
{notifications.length === 0 ? (
<p className="px-4 py-8 text-center text-sm text-muted-foreground">
No notifications yet
</p>
) : (
notifications.map((n) => (
<NotificationItem key={n.id} notification={n} />
))
)}
</div>
</div>
)}
</div>
)
}