114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import { Bell } from 'lucide-react'
|
|
import Link from 'next/link'
|
|
import sdk from '@farcaster/frame-sdk'
|
|
|
|
interface Notification {
|
|
id: string
|
|
type: string
|
|
fromUsername?: string
|
|
pollQuestion?: string
|
|
timestamp: number
|
|
read: boolean
|
|
}
|
|
|
|
export default function NotificationBell() {
|
|
const [showDropdown, setShowDropdown] = useState(false)
|
|
const [unreadCount, setUnreadCount] = useState(0)
|
|
const [notifications, setNotifications] = useState<Notification[]>([])
|
|
const [fid, setFid] = useState<string>('')
|
|
|
|
useEffect(() => {
|
|
const loadData = async () => {
|
|
const ctx = await sdk.context
|
|
if (!ctx?.user?.fid) return
|
|
|
|
const userFid = ctx.user.fid.toString()
|
|
setFid(userFid)
|
|
|
|
const [countRes, notifsRes] = await Promise.all([
|
|
fetch(`/api/notifications/unread?fid=${userFid}`),
|
|
fetch(`/api/notifications?fid=${userFid}`)
|
|
])
|
|
|
|
const countData = await countRes.json()
|
|
const notifsData = await notifsRes.json()
|
|
|
|
setUnreadCount(countData.count || 0)
|
|
setNotifications(notifsData.notifications?.slice(0, 5) || [])
|
|
}
|
|
|
|
loadData()
|
|
const interval = setInterval(loadData, 30000)
|
|
return () => clearInterval(interval)
|
|
}, [])
|
|
|
|
const getMessage = (notif: Notification) => {
|
|
switch (notif.type) {
|
|
case 'vote': return `${notif.fromUsername} voted on your poll`
|
|
case 'comment': return `${notif.fromUsername} commented`
|
|
case 'like': return `${notif.fromUsername} liked your comment`
|
|
case 'follow': return `${notif.fromUsername} followed you`
|
|
case 'win': return 'You won!'
|
|
case 'mention': return `${notif.fromUsername} mentioned you`
|
|
default: return 'New notification'
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="relative">
|
|
<button
|
|
onClick={() => setShowDropdown(!showDropdown)}
|
|
className="relative p-2 hover:bg-white/10 rounded-full transition"
|
|
>
|
|
<Bell size={20} />
|
|
{unreadCount > 0 && (
|
|
<span className="absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center font-bold">
|
|
{unreadCount > 9 ? '9+' : unreadCount}
|
|
</span>
|
|
)}
|
|
</button>
|
|
|
|
{showDropdown && (
|
|
<>
|
|
<div className="fixed inset-0 z-40" onClick={() => setShowDropdown(false)} />
|
|
<div className="absolute right-0 mt-2 w-80 bg-celo-dark border border-white/20 rounded-xl shadow-xl z-50 max-h-96 overflow-y-auto">
|
|
<div className="p-4 border-b border-white/10 flex items-center justify-between">
|
|
<h3 className="font-bold">Notifications</h3>
|
|
<Link href="/notifications" className="text-sm text-celo-green hover:underline">
|
|
View All
|
|
</Link>
|
|
</div>
|
|
|
|
{notifications.length > 0 ? (
|
|
<div>
|
|
{notifications.map(notif => (
|
|
<div
|
|
key={notif.id}
|
|
className={`p-4 border-b border-white/10 hover:bg-white/5 ${!notif.read ? 'bg-white/10' : ''}`}
|
|
>
|
|
<p className="text-sm">{getMessage(notif)}</p>
|
|
{notif.pollQuestion && (
|
|
<p className="text-xs text-gray-400 mt-1">{notif.pollQuestion}</p>
|
|
)}
|
|
<p className="text-xs text-gray-500 mt-1">
|
|
{new Date(notif.timestamp).toLocaleString()}
|
|
</p>
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="p-8 text-center text-gray-400">
|
|
<Bell size={32} className="mx-auto mb-2 opacity-50" />
|
|
<p>No notifications</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|