Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions components/layout/notification-bell.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use client'

import { useState, useRef, useEffect } from 'react'
import { Bell } from 'lucide-react'
import { Bell, X } 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 }) {
function NotificationItem({
notification,
onDismiss,
}: {
notification: AppNotification
onDismiss: (id: string) => void
}) {
return (
<div
className={
Expand All @@ -16,9 +22,18 @@ function NotificationItem({ notification }: { notification: AppNotification }) {
>
<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 className="flex shrink-0 items-center gap-1.5 mt-0.5">
{!notification.read && (
<span className="size-2 rounded-full bg-primary" />
)}
<button
onClick={() => onDismiss(notification.id)}
aria-label="Dismiss notification"
className="text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:rounded"
>
<X className="size-3.5" />
</button>
</div>
</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>
Expand All @@ -28,7 +43,7 @@ function NotificationItem({ notification }: { notification: AppNotification }) {

export function NotificationBell() {
const { address } = useWallet()
const { notifications, unreadCount, markAllRead, clearAll } = useNotifications(address)
const { notifications, unreadCount, markAllRead, clearAll, dismissNotification } = useNotifications(address)
const [open, setOpen] = useState(false)
const ref = useRef<HTMLDivElement>(null)
const triggerRef = useRef<HTMLButtonElement>(null)
Expand Down Expand Up @@ -105,7 +120,7 @@ export function NotificationBell() {
</p>
) : (
notifications.map((n) => (
<NotificationItem key={n.id} notification={n} />
<NotificationItem key={n.id} notification={n} onDismiss={dismissNotification} />
))
)}
</div>
Expand Down
10 changes: 9 additions & 1 deletion hooks/use-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ export function useNotifications(walletAddress: string | null) {
saveNotifications([]);
}, []);

const dismissNotification = useCallback((id: string) => {
setNotifications((prev) => {
const updated = prev.filter((n) => n.id !== id);
saveNotifications(updated);
return updated;
});
}, []);

useEffect(() => {
const rpcUrl = config.rpcUrl;
const contractId = config.streamContractId;
Expand Down Expand Up @@ -388,5 +396,5 @@ export function useNotifications(walletAddress: string | null) {
network,
]);

return { notifications, unreadCount, markAllRead, clearAll };
return { notifications, unreadCount, markAllRead, clearAll, dismissNotification };
}
Loading