NoticeModal.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Modal, Empty } from '@douyinfe/semi-ui';
  3. import { useTranslation } from 'react-i18next';
  4. import { API, showError } from '../../helpers';
  5. import { marked } from 'marked';
  6. import { IllustrationNoContent, IllustrationNoContentDark } from '@douyinfe/semi-illustrations';
  7. const NoticeModal = ({ visible, onClose, isMobile }) => {
  8. const { t } = useTranslation();
  9. const [noticeContent, setNoticeContent] = useState('');
  10. const [loading, setLoading] = useState(false);
  11. const handleCloseTodayNotice = () => {
  12. const today = new Date().toDateString();
  13. localStorage.setItem('notice_close_date', today);
  14. onClose();
  15. };
  16. const displayNotice = async () => {
  17. setLoading(true);
  18. try {
  19. const res = await API.get('/api/notice');
  20. const { success, message, data } = res.data;
  21. if (success) {
  22. if (data !== '') {
  23. const htmlNotice = marked.parse(data);
  24. setNoticeContent(htmlNotice);
  25. } else {
  26. setNoticeContent('');
  27. }
  28. } else {
  29. showError(message);
  30. }
  31. } catch (error) {
  32. showError(error.message);
  33. } finally {
  34. setLoading(false);
  35. }
  36. };
  37. useEffect(() => {
  38. if (visible) {
  39. displayNotice();
  40. }
  41. }, [visible]);
  42. const renderContent = () => {
  43. if (loading) {
  44. return <div className="py-12"><Empty description={t('加载中...')} /></div>;
  45. }
  46. if (!noticeContent) {
  47. return (
  48. <div className="py-12">
  49. <Empty
  50. image={<IllustrationNoContent style={{ width: 150, height: 150 }} />}
  51. darkModeImage={<IllustrationNoContentDark style={{ width: 150, height: 150 }} />}
  52. description={t('暂无公告')}
  53. />
  54. </div>
  55. );
  56. }
  57. return (
  58. <div
  59. dangerouslySetInnerHTML={{ __html: noticeContent }}
  60. className="notice-content-scroll max-h-[60vh] overflow-y-auto pr-2"
  61. />
  62. );
  63. };
  64. return (
  65. <Modal
  66. title={t('系统公告')}
  67. visible={visible}
  68. onCancel={onClose}
  69. footer={(
  70. <div className="flex justify-end">
  71. <Button type='secondary' className='!rounded-full' onClick={handleCloseTodayNotice}>{t('今日关闭')}</Button>
  72. <Button type="primary" className='!rounded-full' onClick={onClose}>{t('关闭公告')}</Button>
  73. </div>
  74. )}
  75. size={isMobile ? 'full-width' : 'large'}
  76. >
  77. {renderContent()}
  78. </Modal>
  79. );
  80. };
  81. export default NoticeModal;