'use client';
import { useEffect, useState } from 'react';
export function NotificationBadge() {
const [count, setCount] = useState(0);
useEffect(() => {
let active = true;
fetch('/api/notifications?page=1')
.then((res) => (res.ok ? res.json() : null))
.then((data) => {
if (active && data && typeof data.total === 'number') {
setCount(data.total);
}
})
.catch(() => undefined);
return () => {
active = false;
};
}, []);
if (count === 0) {
return (
通知
);
}
return (
通知
{count > 99 ? '99+' : count}
);
}