new-api/web/src/pages/Home/index.js

129 lines
5.7 KiB
JavaScript
Raw Normal View History

2023-09-09 03:11:42 +08:00
import React, { useContext, useEffect, useState } from 'react';
import { Card, Col, Row } from '@douyinfe/semi-ui';
2023-09-09 03:11:42 +08:00
import { API, showError, showNotice, timestamp2string } from '../../helpers';
import { StatusContext } from '../../context/Status';
import { marked } from 'marked';
2023-04-22 20:39:27 +08:00
const Home = () => {
2023-08-14 22:16:32 +08:00
const [statusState, statusDispatch] = useContext(StatusContext);
const [homePageContentLoaded, setHomePageContentLoaded] = useState(false);
const [homePageContent, setHomePageContent] = useState('');
2023-04-23 20:00:47 +08:00
2023-08-14 22:16:32 +08:00
const displayNotice = async () => {
const res = await API.get('/api/notice');
2023-09-09 03:11:42 +08:00
const { success, message, data } = res.data;
2023-08-14 22:16:32 +08:00
if (success) {
let oldNotice = localStorage.getItem('notice');
if (data !== oldNotice && data !== '') {
2023-09-09 03:11:42 +08:00
const htmlNotice = marked(data);
showNotice(htmlNotice, true);
2023-08-14 22:16:32 +08:00
localStorage.setItem('notice', data);
}
} else {
showError(message);
}
};
2023-04-22 20:39:27 +08:00
2023-08-14 22:16:32 +08:00
const displayHomePageContent = async () => {
setHomePageContent(localStorage.getItem('home_page_content') || '');
const res = await API.get('/api/home_page_content');
2023-09-09 03:11:42 +08:00
const { success, message, data } = res.data;
2023-08-14 22:16:32 +08:00
if (success) {
let content = data;
if (!data.startsWith('https://')) {
content = marked.parse(data);
}
setHomePageContent(content);
localStorage.setItem('home_page_content', content);
} else {
showError(message);
setHomePageContent('加载首页内容失败...');
}
setHomePageContentLoaded(true);
};
2023-08-14 22:16:32 +08:00
const getStartTimeString = () => {
const timestamp = statusState?.status?.start_time;
return statusState.status ? timestamp2string(timestamp) : '';
2023-08-14 22:16:32 +08:00
};
2023-04-23 20:00:47 +08:00
2023-08-14 22:16:32 +08:00
useEffect(() => {
displayNotice().then();
displayHomePageContent().then();
}, []);
return (
<>
{
2023-09-09 03:11:42 +08:00
homePageContentLoaded && homePageContent === '' ? <>
<Card
bordered={false}
headerLine={false}
title='系统状况'
bodyStyle={{padding: '10px 20px'}}
>
<Row gutter={16}>
<Col span={12}>
<Card
title='系统信息'
headerExtraContent={<span style={{ fontSize:'12px', color: 'var(--semi-color-text-1)'}}>系统信息总览</span>}>
<p>名称{statusState?.system_name}</p>
<p>版本{statusState?.version ? statusState?.version : "unknown"}</p>
<p>
源码
<a
href='https://github.com/songquanpeng/one-api'
target='_blank' rel="noreferrer"
>
https://github.com/songquanpeng/one-api
</a>
</p>
<p>启动时间{getStartTimeString()}</p>
2023-08-14 22:16:32 +08:00
</Card>
</Col>
<Col span={12}>
<Card
title='系统配置'
headerExtraContent={<span style={{ fontSize:'12px', color: 'var(--semi-color-text-1)'}}>系统配置总览</span>}>
<p>
邮箱验证
{statusState?.email_verification === true
? '已启用'
: '未启用'}
</p>
<p>
GitHub 身份验证
{statusState?.github_oauth === true
? '已启用'
: '未启用'}
</p>
<p>
微信身份验证
{statusState?.wechat_login === true
? '已启用'
: '未启用'}
</p>
<p>
Turnstile 用户校验
{statusState?.turnstile_check === true
? '已启用'
: '未启用'}
</p>
2023-08-14 22:16:32 +08:00
</Card>
</Col>
</Row>
</Card>
2023-09-09 03:11:42 +08:00
</> : <>
{
homePageContent.startsWith('https://') ? <iframe
src={homePageContent}
style={{ width: '100%', height: '100vh', border: 'none' }}
/> : <div style={{ fontSize: 'larger' }} dangerouslySetInnerHTML={{ __html: homePageContent }}></div>
}
2023-08-14 22:16:32 +08:00
</>
}
2023-08-14 22:16:32 +08:00
</>
);
2023-04-22 20:39:27 +08:00
};
2023-09-09 03:11:42 +08:00
export default Home;