149 lines
4.9 KiB
TypeScript
149 lines
4.9 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import Link from 'next/link'
|
|
import { Bell } from 'lucide-react'
|
|
import { Drawer } from '@/components/ui/drawer'
|
|
import { cx } from '@/components/ui/cx'
|
|
|
|
type Notification = {
|
|
id: string
|
|
title: string | null
|
|
link: string | null
|
|
sent_at: string
|
|
read_at: string | null
|
|
}
|
|
|
|
const POLL_MS = 60_000
|
|
|
|
function isToday(iso: string): boolean {
|
|
return new Date(iso).toDateString() === new Date().toDateString()
|
|
}
|
|
|
|
export function NotificationBell() {
|
|
const [notifications, setNotifications] = useState<Notification[]>([])
|
|
const [unread, setUnread] = useState(0)
|
|
const [open, setOpen] = useState(false)
|
|
const [tab, setTab] = useState<'all' | 'unread'>('all')
|
|
|
|
const load = useCallback(() => {
|
|
fetch('/api/notifications')
|
|
.then(r => (r.ok ? r.json() : Promise.reject()))
|
|
.then((data: { notifications: Notification[]; unread: number }) => {
|
|
setNotifications(data.notifications)
|
|
setUnread(data.unread)
|
|
})
|
|
.catch(() => {})
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
load()
|
|
const id = setInterval(load, POLL_MS)
|
|
return () => clearInterval(id)
|
|
}, [load])
|
|
|
|
const markAllRead = () => {
|
|
fetch('/api/notifications', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ all: true }),
|
|
})
|
|
.then(() => {
|
|
setUnread(0)
|
|
setNotifications(prev => prev.map(n => ({ ...n, read_at: n.read_at ?? new Date().toISOString() })))
|
|
})
|
|
.catch(() => {})
|
|
}
|
|
|
|
const shown = tab === 'unread' ? notifications.filter(n => !n.read_at) : notifications
|
|
const groups: Array<{ label: string; items: Notification[] }> = [
|
|
{ label: 'Today', items: shown.filter(n => isToday(n.sent_at)) },
|
|
{ label: 'Earlier', items: shown.filter(n => !isToday(n.sent_at)) },
|
|
].filter(g => g.items.length > 0)
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={() => setOpen(true)}
|
|
aria-label={`Notifications${unread > 0 ? ` (${unread} unread)` : ''}`}
|
|
className="relative inline-grid place-items-center w-8 h-8 rounded-md text-neutral-300 hover:bg-ink-2 cursor-pointer"
|
|
>
|
|
<Bell size={16} strokeWidth={2} aria-hidden />
|
|
{unread > 0 && (
|
|
<span className="absolute -top-0.5 -right-0.5 min-w-[16px] h-4 px-1 rounded-full bg-accent text-white text-[10px] font-semibold grid place-items-center">
|
|
{unread > 99 ? '99+' : unread}
|
|
</span>
|
|
)}
|
|
</button>
|
|
|
|
<Drawer
|
|
open={open}
|
|
onClose={() => setOpen(false)}
|
|
title="Notifications"
|
|
actions={
|
|
unread > 0 ? (
|
|
<button
|
|
type="button"
|
|
onClick={markAllRead}
|
|
className="px-2 text-[12.5px] font-[550] text-accent hover:underline cursor-pointer"
|
|
>
|
|
Mark all read
|
|
</button>
|
|
) : undefined
|
|
}
|
|
>
|
|
<div className="flex gap-0.5 px-3 pb-2 border-b border-rule">
|
|
{(['all', 'unread'] as const).map(t => (
|
|
<button
|
|
key={t}
|
|
type="button"
|
|
onClick={() => setTab(t)}
|
|
className={cx(
|
|
'inline-flex items-center gap-1.5 h-7 px-2.5 rounded-md text-[13px] cursor-pointer',
|
|
tab === t ? 'bg-rule-2 font-semibold' : 'font-medium text-ink-3 hover:bg-surface-2',
|
|
)}
|
|
>
|
|
{t === 'all' ? 'All' : 'Unread'}
|
|
<span className="text-xs font-medium text-faint">{t === 'all' ? notifications.length : unread}</span>
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{groups.length === 0 ? (
|
|
<p className="px-4 py-10 text-sm text-faint text-center">No notifications</p>
|
|
) : (
|
|
groups.map(g => (
|
|
<section key={g.label}>
|
|
<h3 className="px-4 pt-3 pb-1 text-[11.5px] font-semibold tracking-wide uppercase text-faint">{g.label}</h3>
|
|
<ul>
|
|
{g.items.map(n => {
|
|
const body = (
|
|
<>
|
|
<p className={cx('text-[13px]', n.read_at ? 'text-muted' : 'text-ink font-medium')}>
|
|
{n.title ?? 'Notification'}
|
|
</p>
|
|
<p className="text-xs text-faint mt-0.5">{new Date(n.sent_at).toLocaleString()}</p>
|
|
</>
|
|
)
|
|
return (
|
|
<li key={n.id} className={cx('border-b border-rule', !n.read_at && 'bg-accent-tint')}>
|
|
{n.link ? (
|
|
<Link href={n.link} className="block px-4 py-3" onClick={() => setOpen(false)}>
|
|
{body}
|
|
</Link>
|
|
) : (
|
|
<div className="px-4 py-3">{body}</div>
|
|
)}
|
|
</li>
|
|
)
|
|
})}
|
|
</ul>
|
|
</section>
|
|
))
|
|
)}
|
|
</Drawer>
|
|
</>
|
|
)
|
|
}
|