NoticeModal.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect, useState, useContext, useMemo } from 'react';
  16. import { Button, Modal, Empty, Tabs, TabPane, Timeline } from '@douyinfe/semi-ui';
  17. import { useTranslation } from 'react-i18next';
  18. import { API, showError, getRelativeTime } from '../../helpers';
  19. import { marked } from 'marked';
  20. import { IllustrationNoContent, IllustrationNoContentDark } from '@douyinfe/semi-illustrations';
  21. import { StatusContext } from '../../context/Status';
  22. import { Bell, Megaphone } from 'lucide-react';
  23. const NoticeModal = ({ visible, onClose, isMobile, defaultTab = 'inApp', unreadKeys = [] }) => {
  24. const { t } = useTranslation();
  25. const [noticeContent, setNoticeContent] = useState('');
  26. const [loading, setLoading] = useState(false);
  27. const [activeTab, setActiveTab] = useState(defaultTab);
  28. const [statusState] = useContext(StatusContext);
  29. const announcements = statusState?.status?.announcements || [];
  30. const unreadSet = useMemo(() => new Set(unreadKeys), [unreadKeys]);
  31. const getKeyForItem = (item) => `${item?.publishDate || ''}-${(item?.content || '').slice(0, 30)}`;
  32. const processedAnnouncements = useMemo(() => {
  33. return (announcements || []).slice(0, 20).map(item => {
  34. const pubDate = item?.publishDate ? new Date(item.publishDate) : null;
  35. const absoluteTime = pubDate && !isNaN(pubDate.getTime())
  36. ? `${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')}`
  37. : (item?.publishDate || '');
  38. return ({
  39. key: getKeyForItem(item),
  40. type: item.type || 'default',
  41. time: absoluteTime,
  42. content: item.content,
  43. extra: item.extra,
  44. relative: getRelativeTime(item.publishDate),
  45. isUnread: unreadSet.has(getKeyForItem(item))
  46. });
  47. });
  48. }, [announcements, unreadSet]);
  49. const handleCloseTodayNotice = () => {
  50. const today = new Date().toDateString();
  51. localStorage.setItem('notice_close_date', today);
  52. onClose();
  53. };
  54. const displayNotice = async () => {
  55. setLoading(true);
  56. try {
  57. const res = await API.get('/api/notice');
  58. const { success, message, data } = res.data;
  59. if (success) {
  60. if (data !== '') {
  61. const htmlNotice = marked.parse(data);
  62. setNoticeContent(htmlNotice);
  63. } else {
  64. setNoticeContent('');
  65. }
  66. } else {
  67. showError(message);
  68. }
  69. } catch (error) {
  70. showError(error.message);
  71. } finally {
  72. setLoading(false);
  73. }
  74. };
  75. useEffect(() => {
  76. if (visible) {
  77. displayNotice();
  78. }
  79. }, [visible]);
  80. useEffect(() => {
  81. if (visible) {
  82. setActiveTab(defaultTab);
  83. }
  84. }, [defaultTab, visible]);
  85. const renderMarkdownNotice = () => {
  86. if (loading) {
  87. return <div className="py-12"><Empty description={t('加载中...')} /></div>;
  88. }
  89. if (!noticeContent) {
  90. return (
  91. <div className="py-12">
  92. <Empty
  93. image={<IllustrationNoContent style={{ width: 150, height: 150 }} />}
  94. darkModeImage={<IllustrationNoContentDark style={{ width: 150, height: 150 }} />}
  95. description={t('暂无公告')}
  96. />
  97. </div>
  98. );
  99. }
  100. return (
  101. <div
  102. dangerouslySetInnerHTML={{ __html: noticeContent }}
  103. className="notice-content-scroll max-h-[55vh] overflow-y-auto pr-2"
  104. />
  105. );
  106. };
  107. const renderAnnouncementTimeline = () => {
  108. if (processedAnnouncements.length === 0) {
  109. return (
  110. <div className="py-12">
  111. <Empty
  112. image={<IllustrationNoContent style={{ width: 150, height: 150 }} />}
  113. darkModeImage={<IllustrationNoContentDark style={{ width: 150, height: 150 }} />}
  114. description={t('暂无系统公告')}
  115. />
  116. </div>
  117. );
  118. }
  119. return (
  120. <div className="max-h-[55vh] overflow-y-auto pr-2 card-content-scroll">
  121. <Timeline mode="left">
  122. {processedAnnouncements.map((item, idx) => {
  123. const htmlContent = marked.parse(item.content || '');
  124. const htmlExtra = item.extra ? marked.parse(item.extra) : '';
  125. return (
  126. <Timeline.Item
  127. key={idx}
  128. type={item.type}
  129. time={`${item.relative ? item.relative + ' ' : ''}${item.time}`}
  130. extra={item.extra ? (
  131. <div
  132. className="text-xs text-gray-500"
  133. dangerouslySetInnerHTML={{ __html: htmlExtra }}
  134. />
  135. ) : null}
  136. className={item.isUnread ? '' : ''}
  137. >
  138. <div>
  139. <div
  140. className={item.isUnread ? 'shine-text' : ''}
  141. dangerouslySetInnerHTML={{ __html: htmlContent }}
  142. />
  143. </div>
  144. </Timeline.Item>
  145. );
  146. })}
  147. </Timeline>
  148. </div>
  149. );
  150. };
  151. const renderBody = () => {
  152. if (activeTab === 'inApp') {
  153. return renderMarkdownNotice();
  154. }
  155. return renderAnnouncementTimeline();
  156. };
  157. return (
  158. <Modal
  159. title={
  160. <div className="flex items-center justify-between w-full">
  161. <span>{t('系统公告')}</span>
  162. <Tabs
  163. activeKey={activeTab}
  164. onChange={setActiveTab}
  165. type='button'
  166. >
  167. <TabPane tab={<span className="flex items-center gap-1"><Bell size={14} /> {t('通知')}</span>} itemKey='inApp' />
  168. <TabPane tab={<span className="flex items-center gap-1"><Megaphone size={14} /> {t('系统公告')}</span>} itemKey='system' />
  169. </Tabs>
  170. </div>
  171. }
  172. visible={visible}
  173. onCancel={onClose}
  174. footer={(
  175. <div className="flex justify-end">
  176. <Button type='secondary' onClick={handleCloseTodayNotice}>{t('今日关闭')}</Button>
  177. <Button type="primary" onClick={onClose}>{t('关闭公告')}</Button>
  178. </div>
  179. )}
  180. size={isMobile ? 'full-width' : 'large'}
  181. >
  182. {renderBody()}
  183. </Modal>
  184. );
  185. };
  186. export default NoticeModal;