2025-12-08 16:31:30 +03:00

138 lines
4.5 KiB
TypeScript

'use client'
import { useState } from 'react'
import useSWR from 'swr'
import { formatDistanceToNow } from 'date-fns'
import LoadingSpinner from '@/components/ui/LoadingSpinner'
import EmptyState from '@/components/ui/EmptyState'
import { toast } from '@/components/ui/Toast'
import type { Notification } from '@/types'
const fetcher = (url: string) => fetch(url).then(r => r.json())
export default function NotificationsPage() {
const [filter, setFilter] = useState<'all' | 'unread' | 'mentions'>('all')
const { data, isLoading, mutate } = useSWR<{ success: boolean; data: Notification[] }>(
'/api/notifications',
fetcher
)
const notifications = data?.data || []
const filteredNotifications = notifications.filter(n => {
if (filter === 'unread') return !n.isRead
if (filter === 'mentions') return n.type === 'mention'
return true
})
const handleMarkAsRead = async (id: string) => {
try {
await fetch(`/api/notifications/${id}`, { method: 'PUT' })
mutate()
} catch (error) {
toast.error('Failed to mark as read')
}
}
const handleMarkAllAsRead = async () => {
try {
const res = await fetch('/api/notifications', { method: 'POST' })
const result = await res.json()
if (result.success) {
toast.success('All notifications marked as read')
mutate()
}
} catch (error) {
toast.error('Failed to mark all as read')
}
}
if (isLoading) {
return (
<div className="flex justify-center items-center min-h-screen">
<LoadingSpinner size="lg" />
</div>
)
}
return (
<div className="max-w-2xl mx-auto p-4 space-y-6">
<div className="flex items-center justify-between">
<h1 className="text-3xl font-bold">๐Ÿ”” Notifications</h1>
{notifications.some(n => !n.isRead) && (
<button
onClick={handleMarkAllAsRead}
className="text-sm text-celo-green hover:underline"
>
Mark all as read
</button>
)}
</div>
<div className="flex gap-2">
{['all', 'unread', 'mentions'].map(f => (
<button
key={f}
onClick={() => setFilter(f as any)}
className={`px-4 py-2 rounded-xl font-semibold capitalize ${
filter === f
? 'bg-celo-green text-white'
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
}`}
>
{f}
</button>
))}
</div>
<div className="bg-white rounded-2xl border border-gray-200 overflow-hidden">
{filteredNotifications.length > 0 ? (
<div>
{filteredNotifications.map(notif => (
<a
key={notif.id}
href={notif.link || '#'}
onClick={() => !notif.isRead && handleMarkAsRead(notif.id)}
className={`block p-4 border-b hover:bg-gray-50 ${!notif.isRead ? 'bg-blue-50' : ''}`}
>
<div className="flex items-start gap-4">
<span className="text-3xl">
{notif.type === 'vote' ? '๐Ÿ—ณ๏ธ' :
notif.type === 'comment' ? '๐Ÿ’ฌ' :
notif.type === 'like' ? 'โค๏ธ' :
notif.type === 'follow' ? '๐Ÿ‘ฅ' :
notif.type === 'win' ? '๐Ÿ†' :
notif.type === 'mention' ? '๐Ÿ“ข' :
notif.type === 'poll_closed' ? '๐Ÿ”’' :
notif.type === 'achievement' ? '๐ŸŽ–๏ธ' : '๐Ÿ””'}
</span>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<h3 className="font-bold">{notif.title}</h3>
{!notif.isRead && (
<span className="w-2 h-2 bg-blue-500 rounded-full" />
)}
</div>
<p className="text-gray-700">{notif.message}</p>
<p className="text-sm text-gray-500 mt-2">
{formatDistanceToNow(new Date(notif.createdAt), { addSuffix: true })}
</p>
</div>
</div>
</a>
))}
</div>
) : (
<EmptyState
emoji="๐Ÿ”•"
title={filter === 'unread' ? 'All caught up!' : 'No notifications'}
description={filter === 'unread' ? 'You have no unread notifications' : 'No notifications to show'}
/>
)}
</div>
</div>
)
}