feat: 新增 useNotifications hook 以处理用户通知,并创建 Home 页面展示财务概览及相关功能。
This commit is contained in:
@@ -12,12 +12,23 @@ interface NotificationContextType {
|
||||
|
||||
const NotificationContext = createContext<NotificationContextType | undefined>(undefined);
|
||||
|
||||
// 检查用户是否已登录
|
||||
const isAuthenticated = (): boolean => {
|
||||
const token = localStorage.getItem('token');
|
||||
return !!token;
|
||||
};
|
||||
|
||||
export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [notifications, setNotifications] = useState<Notification[]>([]);
|
||||
const [unreadCount, setUnreadCount] = useState<number>(0);
|
||||
const [loading, setLoading] = useState<boolean>(false);
|
||||
|
||||
const fetchNotifications = useCallback(async () => {
|
||||
// 未登录时跳过请求,避免 401 错误
|
||||
if (!isAuthenticated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch unread count
|
||||
const countRes = await notificationService.getUnreadCount();
|
||||
@@ -32,6 +43,10 @@ export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
setNotifications(listRes.data.notifications);
|
||||
}
|
||||
} catch (error) {
|
||||
// 静默处理认证错误,避免控制台噪音
|
||||
if (error instanceof Error && error.message.includes('Authorization')) {
|
||||
return;
|
||||
}
|
||||
console.error('Failed to fetch notifications:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -324,9 +324,9 @@ function Home() {
|
||||
<h1 className="greeting-text">
|
||||
{greeting},<span className="greeting-highlight">保持节奏</span>
|
||||
</h1>
|
||||
<p className="greeting-insight animate-slide-up delay-100">
|
||||
<div className="greeting-insight animate-slide-up delay-100">
|
||||
<Icon icon="solar:bell-bing-bold-duotone" width="16" className="insight-icon" />
|
||||
{streakInfo?.message || insight}
|
||||
<span>{streakInfo?.message || insight}</span>
|
||||
<div className="insight-actions" style={{ marginLeft: 'auto', display: 'flex', alignItems: 'center' }}>
|
||||
{streakInfo && streakInfo.currentStreak > 0 && (
|
||||
<div
|
||||
@@ -339,7 +339,7 @@ function Home() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="header-actions animate-slide-up delay-200">
|
||||
<button className="health-score-btn" onClick={() => setShowHealthModal(true)}>
|
||||
|
||||
Reference in New Issue
Block a user