feat(design): add AppShell, MobileShell and the notifications drawer
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
'use client'
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
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
|
||||
@@ -14,11 +16,15 @@ type Notification = {
|
||||
|
||||
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 panelRef = useRef<HTMLDivElement>(null)
|
||||
const [tab, setTab] = useState<'all' | 'unread'>('all')
|
||||
|
||||
const load = useCallback(() => {
|
||||
fetch('/api/notifications')
|
||||
@@ -36,15 +42,6 @@ export function NotificationBell() {
|
||||
return () => clearInterval(id)
|
||||
}, [load])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onClick = (e: MouseEvent) => {
|
||||
if (panelRef.current && !panelRef.current.contains(e.target as Node)) setOpen(false)
|
||||
}
|
||||
document.addEventListener('mousedown', onClick)
|
||||
return () => document.removeEventListener('mousedown', onClick)
|
||||
}, [open])
|
||||
|
||||
const markAllRead = () => {
|
||||
fetch('/api/notifications', {
|
||||
method: 'POST',
|
||||
@@ -58,62 +55,94 @@ export function NotificationBell() {
|
||||
.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 (
|
||||
<div ref={panelRef} className="fixed top-3 right-3 z-50">
|
||||
<>
|
||||
<button
|
||||
onClick={() => setOpen(o => !o)}
|
||||
type="button"
|
||||
onClick={() => setOpen(true)}
|
||||
aria-label={`Notifications${unread > 0 ? ` (${unread} unread)` : ''}`}
|
||||
className="relative bg-surface border border-divider w-10 h-10 flex items-center justify-center text-ink cursor-pointer transition-colors hover:bg-ink/7 active:bg-ink/15"
|
||||
className="relative inline-grid place-items-center w-8 h-8 rounded-md text-neutral-300 hover:bg-ink-2 cursor-pointer"
|
||||
>
|
||||
<Bell size={18} strokeWidth={2} aria-hidden />
|
||||
<Bell size={16} strokeWidth={2} aria-hidden />
|
||||
{unread > 0 && (
|
||||
<span className="absolute -top-1 -right-1 bg-accent text-ground text-[10px] font-extrabold min-w-[18px] h-[18px] px-1 flex items-center justify-center">
|
||||
<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>
|
||||
|
||||
{open && (
|
||||
<div className="absolute right-0 mt-2 w-80 max-h-96 overflow-y-auto bg-surface shadow-lg border border-divider">
|
||||
<div className="flex items-center justify-between px-4 py-2 border-b border-divider">
|
||||
<span className="text-sm font-extrabold text-ink">Notifications</span>
|
||||
{unread > 0 && (
|
||||
<button onClick={markAllRead} className="text-xs text-accent-700 hover:underline">
|
||||
Mark all read
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{notifications.length === 0 ? (
|
||||
<p className="px-4 py-6 text-sm text-gray-400 text-center">No notifications</p>
|
||||
) : (
|
||||
<ul>
|
||||
{notifications.map(n => {
|
||||
const inner = (
|
||||
<>
|
||||
<p className={`text-sm ${n.read_at ? 'text-gray-500' : 'text-gray-900 font-medium'}`}>
|
||||
{n.title ?? 'Notification'}
|
||||
</p>
|
||||
<p className="text-xs text-gray-400 mt-0.5">
|
||||
{new Date(n.sent_at).toLocaleString()}
|
||||
</p>
|
||||
</>
|
||||
)
|
||||
return (
|
||||
<li key={n.id} className="border-b border-gray-50 last:border-0">
|
||||
{n.link ? (
|
||||
<Link href={n.link} className="block px-4 py-3 hover:bg-gray-50" onClick={() => setOpen(false)}>
|
||||
{inner}
|
||||
</Link>
|
||||
) : (
|
||||
<div className="px-4 py-3">{inner}</div>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
<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>
|
||||
)}
|
||||
</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>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user