| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import React, { useEffect, useState } from 'react'; |
| import { Badge, Button, Tooltip } from '@douyinfe/semi-ui'; |
| import { IconMail } from '@douyinfe/semi-icons'; |
| import { useNavigate } from 'react-router-dom'; |
| import { API } from '../helpers'; |
| import { useTranslation } from 'react-i18next'; |
|
|
| const InboxIcon = () => { |
| const { t } = useTranslation(); |
| const navigate = useNavigate(); |
| const [unreadCount, setUnreadCount] = useState(0); |
| const [loading, setLoading] = useState(false); |
|
|
| const loadUnreadCount = async () => { |
| if (loading) return; |
| |
| setLoading(true); |
| try { |
| const res = await API.get('/api/user/messages/unread_count'); |
| const { success, data } = res.data; |
| if (success) { |
| setUnreadCount(data.unread_count || 0); |
| } |
| } catch (error) { |
| |
| console.error('Failed to load unread count:', error); |
| } finally { |
| setLoading(false); |
| } |
| }; |
|
|
| const handleClick = () => { |
| navigate('/app/inbox'); |
| }; |
|
|
| useEffect(() => { |
| loadUnreadCount(); |
| |
| |
| const interval = setInterval(loadUnreadCount, 30000); |
| |
| return () => clearInterval(interval); |
| }, []); |
|
|
| return ( |
| <Tooltip content={t('收件箱')} position="bottom"> |
| <div style={{ position: 'relative', display: 'inline-block' }}> |
| <Button |
| icon={<IconMail />} |
| theme="borderless" |
| type="tertiary" |
| onClick={handleClick} |
| style={{ |
| color: 'var(--semi-color-text-2)', |
| fontSize: '16px', |
| padding: '8px', |
| minWidth: 'auto', |
| height: 'auto' |
| }} |
| /> |
| {unreadCount > 0 && ( |
| <Badge |
| count={unreadCount > 99 ? '99+' : unreadCount} |
| style={{ |
| position: 'absolute', |
| top: '-2px', |
| right: '-2px', |
| fontSize: '10px', |
| minWidth: '16px', |
| height: '16px', |
| lineHeight: '16px', |
| padding: '0 4px', |
| borderRadius: '8px', |
| backgroundColor: 'var(--semi-color-danger)', |
| color: 'white', |
| border: '1px solid var(--semi-color-bg-1)', |
| boxShadow: '0 1px 2px rgba(0, 0, 0, 0.1)' |
| }} |
| /> |
| )} |
| </div> |
| </Tooltip> |
| ); |
| }; |
|
|
| export default InboxIcon; |