NoticeModal.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/index.js';
  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. key: getKeyForItem(item),
  35. type: item.type || 'default',
  36. time: getRelativeTime(item.publishDate),
  37. content: item.content,
  38. extra: item.extra,
  39. isUnread: unreadSet.has(getKeyForItem(item))
  40. }));
  41. }, [announcements, unreadSet]);
  42. const handleCloseTodayNotice = () => {
  43. const today = new Date().toDateString();
  44. localStorage.setItem('notice_close_date', today);
  45. onClose();
  46. };
  47. const displayNotice = async () => {
  48. setLoading(true);
  49. try {
  50. const res = await API.get('/api/notice');
  51. const { success, message, data } = res.data;
  52. if (success) {
  53. if (data !== '') {
  54. const htmlNotice = marked.parse(data);
  55. setNoticeContent(htmlNotice);
  56. } else {
  57. setNoticeContent('');
  58. }
  59. } else {
  60. showError(message);
  61. }
  62. } catch (error) {
  63. showError(error.message);
  64. } finally {
  65. setLoading(false);
  66. }
  67. };
  68. useEffect(() => {
  69. if (visible) {
  70. displayNotice();
  71. }
  72. }, [visible]);
  73. useEffect(() => {
  74. if (visible) {
  75. setActiveTab(defaultTab);
  76. }
  77. }, [defaultTab, visible]);
  78. const renderMarkdownNotice = () => {
  79. if (loading) {
  80. return <div className="py-12"><Empty description={t('加载中...')} /></div>;
  81. }
  82. if (!noticeContent) {
  83. return (
  84. <div className="py-12">
  85. <Empty
  86. image={<IllustrationNoContent style={{ width: 150, height: 150 }} />}
  87. darkModeImage={<IllustrationNoContentDark style={{ width: 150, height: 150 }} />}
  88. description={t('暂无公告')}
  89. />
  90. </div>
  91. );
  92. }
  93. return (
  94. <div
  95. dangerouslySetInnerHTML={{ __html: noticeContent }}
  96. className="notice-content-scroll max-h-[55vh] overflow-y-auto pr-2"
  97. />
  98. );
  99. };
  100. const renderAnnouncementTimeline = () => {
  101. if (processedAnnouncements.length === 0) {
  102. return (
  103. <div className="py-12">
  104. <Empty
  105. image={<IllustrationNoContent style={{ width: 150, height: 150 }} />}
  106. darkModeImage={<IllustrationNoContentDark style={{ width: 150, height: 150 }} />}
  107. description={t('暂无系统公告')}
  108. />
  109. </div>
  110. );
  111. }
  112. return (
  113. <div className="max-h-[55vh] overflow-y-auto pr-2 card-content-scroll">
  114. <Timeline mode="alternate">
  115. {processedAnnouncements.map((item, idx) => {
  116. const htmlContent = marked.parse(item.content || '');
  117. const htmlExtra = item.extra ? marked.parse(item.extra) : '';
  118. return (
  119. <Timeline.Item
  120. key={idx}
  121. type={item.type}
  122. time={item.time}
  123. className={item.isUnread ? '' : ''}
  124. >
  125. <div>
  126. <div
  127. className={item.isUnread ? 'shine-text' : ''}
  128. dangerouslySetInnerHTML={{ __html: htmlContent }}
  129. />
  130. {item.extra && (
  131. <div
  132. className="text-xs text-gray-500"
  133. dangerouslySetInnerHTML={{ __html: htmlExtra }}
  134. />
  135. )}
  136. </div>
  137. </Timeline.Item>
  138. );
  139. })}
  140. </Timeline>
  141. </div>
  142. );
  143. };
  144. const renderBody = () => {
  145. if (activeTab === 'inApp') {
  146. return renderMarkdownNotice();
  147. }
  148. return renderAnnouncementTimeline();
  149. };
  150. return (
  151. <Modal
  152. title={
  153. <div className="flex items-center justify-between w-full">
  154. <span>{t('系统公告')}</span>
  155. <Tabs
  156. activeKey={activeTab}
  157. onChange={setActiveTab}
  158. type='card'
  159. size='small'
  160. >
  161. <TabPane tab={<span className="flex items-center gap-1"><Bell size={14} /> {t('通知')}</span>} itemKey='inApp' />
  162. <TabPane tab={<span className="flex items-center gap-1"><Megaphone size={14} /> {t('系统公告')}</span>} itemKey='system' />
  163. </Tabs>
  164. </div>
  165. }
  166. visible={visible}
  167. onCancel={onClose}
  168. footer={(
  169. <div className="flex justify-end">
  170. <Button type='secondary' onClick={handleCloseTodayNotice}>{t('今日关闭')}</Button>
  171. <Button type="primary" onClick={onClose}>{t('关闭公告')}</Button>
  172. </div>
  173. )}
  174. size={isMobile ? 'full-width' : 'large'}
  175. >
  176. {renderBody()}
  177. </Modal>
  178. );
  179. };
  180. export default NoticeModal;