NoticeModal.jsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 {
  17. Button,
  18. Modal,
  19. Empty,
  20. Tabs,
  21. TabPane,
  22. Timeline,
  23. } from '@douyinfe/semi-ui';
  24. import { useTranslation } from 'react-i18next';
  25. import { API, showError, getRelativeTime } from '../../helpers';
  26. import { marked } from 'marked';
  27. import {
  28. IllustrationNoContent,
  29. IllustrationNoContentDark,
  30. } from '@douyinfe/semi-illustrations';
  31. import { StatusContext } from '../../context/Status';
  32. import { Bell, Megaphone } from 'lucide-react';
  33. const NoticeModal = ({
  34. visible,
  35. onClose,
  36. isMobile,
  37. defaultTab = 'inApp',
  38. unreadKeys = [],
  39. }) => {
  40. const { t } = useTranslation();
  41. const [noticeContent, setNoticeContent] = useState('');
  42. const [loading, setLoading] = useState(false);
  43. const [activeTab, setActiveTab] = useState(defaultTab);
  44. const [statusState] = useContext(StatusContext);
  45. const announcements = statusState?.status?.announcements || [];
  46. const unreadSet = useMemo(() => new Set(unreadKeys), [unreadKeys]);
  47. const getKeyForItem = (item) =>
  48. `${item?.publishDate || ''}-${(item?.content || '').slice(0, 30)}`;
  49. const processedAnnouncements = useMemo(() => {
  50. return (announcements || []).slice(0, 20).map((item) => {
  51. const pubDate = item?.publishDate ? new Date(item.publishDate) : null;
  52. const absoluteTime =
  53. pubDate && !isNaN(pubDate.getTime())
  54. ? `${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')}`
  55. : item?.publishDate || '';
  56. return {
  57. key: getKeyForItem(item),
  58. type: item.type || 'default',
  59. time: absoluteTime,
  60. content: item.content,
  61. extra: item.extra,
  62. relative: getRelativeTime(item.publishDate),
  63. isUnread: unreadSet.has(getKeyForItem(item)),
  64. };
  65. });
  66. }, [announcements, unreadSet]);
  67. const handleCloseTodayNotice = () => {
  68. const today = new Date().toDateString();
  69. localStorage.setItem('notice_close_date', today);
  70. onClose();
  71. };
  72. const displayNotice = async () => {
  73. setLoading(true);
  74. try {
  75. const res = await API.get('/api/notice');
  76. const { success, message, data } = res.data;
  77. if (success) {
  78. if (data !== '') {
  79. const htmlNotice = marked.parse(data);
  80. setNoticeContent(htmlNotice);
  81. } else {
  82. setNoticeContent('');
  83. }
  84. } else {
  85. showError(message);
  86. }
  87. } catch (error) {
  88. showError(error.message);
  89. } finally {
  90. setLoading(false);
  91. }
  92. };
  93. useEffect(() => {
  94. if (visible) {
  95. displayNotice();
  96. }
  97. }, [visible]);
  98. useEffect(() => {
  99. if (visible) {
  100. setActiveTab(defaultTab);
  101. }
  102. }, [defaultTab, visible]);
  103. const renderMarkdownNotice = () => {
  104. if (loading) {
  105. return (
  106. <div className='py-12'>
  107. <Empty description={t('加载中...')} />
  108. </div>
  109. );
  110. }
  111. if (!noticeContent) {
  112. return (
  113. <div className='py-12'>
  114. <Empty
  115. image={
  116. <IllustrationNoContent style={{ width: 150, height: 150 }} />
  117. }
  118. darkModeImage={
  119. <IllustrationNoContentDark style={{ width: 150, height: 150 }} />
  120. }
  121. description={t('暂无公告')}
  122. />
  123. </div>
  124. );
  125. }
  126. return (
  127. <div
  128. dangerouslySetInnerHTML={{ __html: noticeContent }}
  129. className='notice-content-scroll max-h-[55vh] overflow-y-auto pr-2'
  130. />
  131. );
  132. };
  133. const renderAnnouncementTimeline = () => {
  134. if (processedAnnouncements.length === 0) {
  135. return (
  136. <div className='py-12'>
  137. <Empty
  138. image={
  139. <IllustrationNoContent style={{ width: 150, height: 150 }} />
  140. }
  141. darkModeImage={
  142. <IllustrationNoContentDark style={{ width: 150, height: 150 }} />
  143. }
  144. description={t('暂无系统公告')}
  145. />
  146. </div>
  147. );
  148. }
  149. return (
  150. <div className='max-h-[55vh] overflow-y-auto pr-2 card-content-scroll'>
  151. <Timeline mode='left'>
  152. {processedAnnouncements.map((item, idx) => {
  153. const htmlContent = marked.parse(item.content || '');
  154. const htmlExtra = item.extra ? marked.parse(item.extra) : '';
  155. return (
  156. <Timeline.Item
  157. key={idx}
  158. type={item.type}
  159. time={`${item.relative ? item.relative + ' ' : ''}${item.time}`}
  160. extra={
  161. item.extra ? (
  162. <div
  163. className='text-xs text-gray-500'
  164. dangerouslySetInnerHTML={{ __html: htmlExtra }}
  165. />
  166. ) : null
  167. }
  168. className={item.isUnread ? '' : ''}
  169. >
  170. <div>
  171. <div
  172. className={item.isUnread ? 'shine-text' : ''}
  173. dangerouslySetInnerHTML={{ __html: htmlContent }}
  174. />
  175. </div>
  176. </Timeline.Item>
  177. );
  178. })}
  179. </Timeline>
  180. </div>
  181. );
  182. };
  183. const renderBody = () => {
  184. if (activeTab === 'inApp') {
  185. return renderMarkdownNotice();
  186. }
  187. return renderAnnouncementTimeline();
  188. };
  189. return (
  190. <Modal
  191. title={
  192. <div className='flex items-center justify-between w-full'>
  193. <span>{t('系统公告')}</span>
  194. <Tabs activeKey={activeTab} onChange={setActiveTab} type='button'>
  195. <TabPane
  196. tab={
  197. <span className='flex items-center gap-1'>
  198. <Bell size={14} /> {t('通知')}
  199. </span>
  200. }
  201. itemKey='inApp'
  202. />
  203. <TabPane
  204. tab={
  205. <span className='flex items-center gap-1'>
  206. <Megaphone size={14} /> {t('系统公告')}
  207. </span>
  208. }
  209. itemKey='system'
  210. />
  211. </Tabs>
  212. </div>
  213. }
  214. visible={visible}
  215. onCancel={onClose}
  216. footer={
  217. <div className='flex justify-end'>
  218. <Button type='secondary' onClick={handleCloseTodayNotice}>
  219. {t('今日关闭')}
  220. </Button>
  221. <Button type='primary' onClick={onClose}>
  222. {t('关闭公告')}
  223. </Button>
  224. </div>
  225. }
  226. size={isMobile ? 'full-width' : 'large'}
  227. >
  228. {renderBody()}
  229. </Modal>
  230. );
  231. };
  232. export default NoticeModal;