262 lines
7.5 KiB
React
262 lines
7.5 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import React, { useEffect, useState, useContext, useMemo } from 'react';
|
|
import {
|
|
Badge,
|
|
Button,
|
|
Card,
|
|
Checkbox,
|
|
Modal,
|
|
Segmented,
|
|
Spin,
|
|
Tag,
|
|
theme,
|
|
} from 'antd';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { API, showError, getRelativeTime } from '../../helpers';
|
|
import { marked } from 'marked';
|
|
import { StatusContext } from '../../context/Status';
|
|
import { Bell, Volume1 } from 'lucide-react';
|
|
import './notice-modal.css';
|
|
|
|
const EMPTY_ANNOUNCEMENTS = [];
|
|
|
|
const NoticeModal = ({
|
|
visible,
|
|
onClose,
|
|
isMobile,
|
|
defaultTab = 'inApp',
|
|
unreadKeys = [],
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
const { token } = theme.useToken();
|
|
const [noticeContent, setNoticeContent] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [activeTab, setActiveTab] = useState(defaultTab);
|
|
const [dismissToday, setDismissToday] = useState(true);
|
|
const [statusState] = useContext(StatusContext);
|
|
const announcements =
|
|
statusState?.status?.announcements || EMPTY_ANNOUNCEMENTS;
|
|
const unreadSet = useMemo(() => new Set(unreadKeys), [unreadKeys]);
|
|
|
|
const getKeyForItem = (item) =>
|
|
`${item?.publishDate || ''}-${(item?.content || '').slice(0, 30)}`;
|
|
|
|
const processedAnnouncements = useMemo(() => {
|
|
return (announcements || []).slice(0, 20).map((item) => {
|
|
const pubDate = item?.publishDate ? new Date(item.publishDate) : null;
|
|
const absoluteTime =
|
|
pubDate && !isNaN(pubDate.getTime())
|
|
? `${pubDate.getFullYear()}-${String(pubDate.getMonth() + 1).padStart(2, '0')}-${String(pubDate.getDate()).padStart(2, '0')} ${String(pubDate.getHours()).padStart(2, '0')}:${String(pubDate.getMinutes()).padStart(2, '0')}`
|
|
: item?.publishDate || '';
|
|
return {
|
|
key: getKeyForItem(item),
|
|
type: item.type || 'default',
|
|
time: absoluteTime,
|
|
content: item.content,
|
|
extra: item.extra,
|
|
relative: getRelativeTime(item.publishDate),
|
|
isUnread: unreadSet.has(getKeyForItem(item)),
|
|
};
|
|
});
|
|
}, [announcements, unreadSet]);
|
|
|
|
const handleCloseTodayNotice = () => {
|
|
const today = new Date().toDateString();
|
|
localStorage.setItem('notice_close_date', today);
|
|
onClose();
|
|
};
|
|
|
|
const displayNotice = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const res = await API.get('/api/notice');
|
|
const { success, message, data } = res.data;
|
|
if (success) {
|
|
if (data !== '') {
|
|
const htmlNotice = marked.parse(data);
|
|
setNoticeContent(htmlNotice);
|
|
} else {
|
|
setNoticeContent('');
|
|
}
|
|
} else {
|
|
showError(message);
|
|
}
|
|
} catch (error) {
|
|
showError(error.message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (visible) {
|
|
displayNotice();
|
|
}
|
|
}, [visible]);
|
|
|
|
useEffect(() => {
|
|
if (visible) {
|
|
setActiveTab(defaultTab);
|
|
setDismissToday(true);
|
|
}
|
|
}, [defaultTab, visible]);
|
|
|
|
const unreadCount = processedAnnouncements.filter(
|
|
(item) => item.isUnread,
|
|
).length;
|
|
const typeColors = {
|
|
success: 'success',
|
|
warning: 'warning',
|
|
error: 'error',
|
|
ongoing: 'processing',
|
|
default: 'default',
|
|
};
|
|
|
|
const renderBody = () => {
|
|
if (activeTab === 'inApp') {
|
|
if (loading) {
|
|
return (
|
|
<div className='nm-empty'>
|
|
<Spin description={t('加载中...')}>
|
|
<div className='nm-spin-placeholder' />
|
|
</Spin>
|
|
</div>
|
|
);
|
|
}
|
|
if (!noticeContent) {
|
|
return <div className='nm-empty'>{t('暂无公告')}</div>;
|
|
}
|
|
return (
|
|
<div className='nm-notice-scroll' tabIndex={0} aria-label={t('通知')}>
|
|
<div
|
|
className='nm-markdown'
|
|
dangerouslySetInnerHTML={{ __html: noticeContent }}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
if (processedAnnouncements.length === 0) {
|
|
return <div className='nm-empty'>{t('暂无系统公告')}</div>;
|
|
}
|
|
return (
|
|
<div
|
|
className='nm-announcement-scroll'
|
|
tabIndex={0}
|
|
aria-label={t('系统公告')}
|
|
>
|
|
{processedAnnouncements.map((item, idx) => (
|
|
<Card key={idx} size='small'>
|
|
<div className='nm-announcement-meta'>
|
|
<span>
|
|
<Badge status={typeColors[item.type] || 'default'} />{' '}
|
|
{item.relative} · {item.time}
|
|
</span>
|
|
{item.isUnread && (
|
|
<Tag color={typeColors[item.type] || 'default'}>NEW</Tag>
|
|
)}
|
|
</div>
|
|
<div
|
|
className='nm-markdown'
|
|
dangerouslySetInnerHTML={{
|
|
__html: marked.parse(item.content || ''),
|
|
}}
|
|
/>
|
|
{item.extra && (
|
|
<div
|
|
className='nm-announcement-extra nm-markdown'
|
|
dangerouslySetInnerHTML={{ __html: marked.parse(item.extra) }}
|
|
/>
|
|
)}
|
|
</Card>
|
|
))}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
title={t('系统公告')}
|
|
open={visible}
|
|
onCancel={onClose}
|
|
width={isMobile ? 'calc(100% - 32px)' : 860}
|
|
className='notice-modal'
|
|
rootClassName='notice-modal-root'
|
|
styles={{
|
|
mask: {
|
|
background: `color-mix(in srgb, ${token.colorBgMask} 70%, transparent)`,
|
|
},
|
|
}}
|
|
style={{
|
|
'--nm-surface': token.colorBgContainer,
|
|
'--nm-text': token.colorText,
|
|
'--nm-muted': token.colorTextSecondary,
|
|
'--nm-border': token.colorBorderSecondary,
|
|
'--nm-fill': token.colorFillQuaternary,
|
|
'--nm-tab-bg': token.colorBgLayout,
|
|
'--nm-primary': token.colorPrimary,
|
|
}}
|
|
footer={
|
|
<div className='nm-footer'>
|
|
<Checkbox
|
|
checked={dismissToday}
|
|
onChange={(event) => setDismissToday(event.target.checked)}
|
|
>
|
|
{t('今日不再提示')}
|
|
</Checkbox>
|
|
<Button
|
|
type='primary'
|
|
onClick={dismissToday ? handleCloseTodayNotice : onClose}
|
|
>
|
|
{t('我知道了')}
|
|
</Button>
|
|
</div>
|
|
}
|
|
mask={{ closable: true }}
|
|
>
|
|
<Segmented
|
|
block
|
|
value={activeTab}
|
|
onChange={setActiveTab}
|
|
options={[
|
|
{
|
|
value: 'inApp',
|
|
label: t('通知'),
|
|
icon: <Bell size={14} strokeWidth={1.5} aria-hidden />,
|
|
},
|
|
{
|
|
value: 'system',
|
|
label: (
|
|
<span className='nm-system-label'>
|
|
{t('系统公告')}
|
|
<Badge count={unreadCount} size='small' />
|
|
</span>
|
|
),
|
|
icon: <Volume1 size={14} strokeWidth={1.5} aria-hidden />,
|
|
},
|
|
]}
|
|
/>
|
|
<div className='nm-body'>{renderBody()}</div>
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
export default NoticeModal;
|