Files
PolyHermes/frontend/src/components/Layout.tsx
T
WrBug 07d1b50b9b feat: 实现公告板功能
- 后端:从 GitHub Issues API 获取公告数据
  - 支持筛选指定 assignees 的评论
  - 支持排除指定 issue 的评论
  - 从评论第一行提取标题(移除 Markdown 格式)
  - 支持获取 reactions 数据(+1、heart、laugh 等)
  - 实现 10 分钟缓存机制
  - 强制刷新时,如果被限流则保留缓存数据
  - 添加 GitHub API 健康检查

- 前端:公告列表和详情页面
  - 响应式设计(移动端和桌面端)
  - 移动端使用侧边抽屉显示列表
  - 支持 Markdown 渲染
  - 支持内容折叠(超过 30 行)
  - 显示 reactions(使用 emoji)
  - 添加刷新按钮(支持强制刷新)
  - 公告菜单项放在第一位
  - 默认页面改为公告页

- 其他:
  - 更新 favicon 为 SVG 格式
  - 添加 GitHub API 限流方案文档
  - 添加 GitHub Token 配置文档
2025-12-07 16:15:59 +08:00

364 lines
9.7 KiB
TypeScript

import { useState, useEffect } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'
import { Layout as AntLayout, Menu, Drawer, Button, Modal } from 'antd'
import { useTranslation } from 'react-i18next'
import { useMediaQuery } from 'react-responsive'
import {
WalletOutlined,
UserOutlined,
UnorderedListOutlined,
BarChartOutlined,
MenuOutlined,
FileTextOutlined,
LinkOutlined,
AppstoreOutlined,
TeamOutlined,
LogoutOutlined,
SettingOutlined,
GithubOutlined,
TwitterOutlined,
CheckCircleOutlined,
SendOutlined,
NotificationOutlined
} from '@ant-design/icons'
import type { MenuProps } from 'antd'
import type { ReactNode } from 'react'
import { removeToken } from '../utils'
import { wsManager } from '../services/websocket'
import Logo from './Logo'
const { Header, Content, Sider } = AntLayout
interface LayoutProps {
children: ReactNode
}
const Layout: React.FC<LayoutProps> = ({ children }) => {
const { t } = useTranslation()
const navigate = useNavigate()
const location = useLocation()
const isMobile = useMediaQuery({ maxWidth: 768 })
const [mobileMenuOpen, setMobileMenuOpen] = useState(false)
// 获取当前选中的菜单项
const getSelectedKeys = (): string[] => {
return [location.pathname]
}
// 获取当前应该打开的父菜单
const getInitialOpenKeys = (): string[] => {
const path = location.pathname
const keys: string[] = []
if (path.startsWith('/leaders') || path.startsWith('/templates') || path.startsWith('/copy-trading')) {
keys.push('/copy-trading-management')
}
if (path.startsWith('/system-settings')) {
keys.push('/system-settings')
}
return keys
}
const [openKeys, setOpenKeys] = useState<string[]>(getInitialOpenKeys())
// 当路径变化时,自动打开对应的父菜单
useEffect(() => {
const path = location.pathname
const keys: string[] = []
if (path.startsWith('/leaders') || path.startsWith('/templates') || path.startsWith('/copy-trading')) {
keys.push('/copy-trading-management')
}
if (path.startsWith('/system-settings')) {
keys.push('/system-settings')
}
setOpenKeys(keys)
}, [location.pathname])
const menuItems: MenuProps['items'] = [
{
key: '/announcements',
icon: <NotificationOutlined />,
label: t('menu.announcements') || '公告'
},
{
key: '/accounts',
icon: <WalletOutlined />,
label: t('menu.accounts')
},
{
key: '/copy-trading-management',
icon: <AppstoreOutlined />,
label: t('menu.copyTrading'),
children: [
{
key: '/copy-trading',
icon: <LinkOutlined />,
label: t('menu.copyTradingConfig')
},
{
key: '/leaders',
icon: <UserOutlined />,
label: t('menu.leaders')
},
{
key: '/templates',
icon: <FileTextOutlined />,
label: t('menu.templates')
}
]
},
{
key: '/positions',
icon: <UnorderedListOutlined />,
label: t('menu.positions')
},
{
key: '/statistics',
icon: <BarChartOutlined />,
label: t('menu.statistics')
},
{
key: '/users',
icon: <TeamOutlined />,
label: t('menu.users')
},
{
key: '/system-settings',
icon: <SettingOutlined />,
label: t('menu.systemSettings') || '系统管理',
children: [
{
key: '/system-settings',
icon: <SettingOutlined />,
label: t('menu.systemOverview') || '通用设置'
},
{
key: '/system-settings/api-health',
icon: <CheckCircleOutlined />,
label: t('menu.apiHealth') || 'API健康'
}
]
},
{
key: 'logout',
icon: <LogoutOutlined />,
label: t('menu.logout')
}
]
const handleLogout = () => {
removeToken()
// 断开 WebSocket 连接
wsManager.disconnect()
navigate('/login', { replace: true })
}
const handleLogoutConfirm = () => {
Modal.confirm({
title: t('menu.logoutConfirm'),
content: t('menu.logoutConfirmDesc'),
okText: t('common.confirm'),
cancelText: t('common.cancel'),
onOk: () => {
handleLogout()
if (isMobile) {
setMobileMenuOpen(false)
}
}
})
}
const handleMenuClick = ({ key }: { key: string }) => {
// 如果是父菜单,不导航(但 /system-settings 作为子菜单项时可以导航)
if (key === '/copy-trading-management') {
return
}
// 处理退出登录
if (key === 'logout') {
handleLogoutConfirm()
return
}
navigate(key)
if (isMobile) {
setMobileMenuOpen(false)
}
}
const handleOpenChange = (keys: string[]) => {
setOpenKeys(keys)
}
if (isMobile) {
// 移动端布局
return (
<AntLayout style={{ minHeight: '100vh' }}>
<Header style={{
background: '#001529',
padding: '0 16px',
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between'
}}>
<Logo
size="normal"
darkMode={true}
/>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<a
href="https://github.com/WrBug/PolyHermes"
target="_blank"
rel="noopener noreferrer"
style={{ color: '#fff', fontSize: '16px', display: 'flex', alignItems: 'center' }}
title="GitHub"
>
<GithubOutlined />
</a>
<a
href="https://x.com/quant_tr"
target="_blank"
rel="noopener noreferrer"
style={{ color: '#fff', fontSize: '16px', display: 'flex', alignItems: 'center' }}
title="Twitter"
>
<TwitterOutlined />
</a>
<a
href="https://t.me/+5BwdYvvvuf9iZGZl"
target="_blank"
rel="noopener noreferrer"
style={{ color: '#fff', fontSize: '16px', display: 'flex', alignItems: 'center' }}
title="Telegram 交流群"
>
<SendOutlined />
</a>
<Button
type="text"
icon={<MenuOutlined />}
style={{ color: '#fff', marginLeft: '4px' }}
onClick={() => setMobileMenuOpen(true)}
/>
</div>
</Header>
<Content style={{
padding: '12px 8px',
background: '#f0f2f5',
minHeight: 'calc(100vh - 64px)'
}}>
{children}
</Content>
<Drawer
title={t('menu.navigation')}
placement="left"
onClose={() => setMobileMenuOpen(false)}
open={mobileMenuOpen}
bodyStyle={{ padding: 0 }}
>
<Menu
mode="inline"
selectedKeys={getSelectedKeys()}
openKeys={openKeys}
onOpenChange={handleOpenChange}
items={menuItems}
onClick={handleMenuClick}
style={{ border: 'none' }}
/>
</Drawer>
</AntLayout>
)
}
// 桌面端布局
return (
<AntLayout style={{ height: '100vh', overflow: 'hidden' }}>
<Sider
width={200}
style={{
background: '#001529',
height: '100vh',
position: 'fixed',
left: 0,
top: 0,
overflow: 'hidden'
}}
>
<div style={{
padding: '16px',
color: '#fff',
flexShrink: 0,
borderBottom: '1px solid rgba(255, 255, 255, 0.1)'
}}>
<div style={{
fontSize: '18px',
fontWeight: 'bold',
marginBottom: '12px',
textAlign: 'center'
}}>
PolyHermes
</div>
<div style={{
display: 'flex',
gap: '12px',
alignItems: 'center',
justifyContent: 'center'
}}>
<a
href="https://github.com/WrBug/PolyHermes"
target="_blank"
rel="noopener noreferrer"
style={{ color: '#fff', fontSize: '18px', display: 'flex', alignItems: 'center' }}
title="GitHub"
>
<GithubOutlined />
</a>
<a
href="https://x.com/quant_tr"
target="_blank"
rel="noopener noreferrer"
style={{ color: '#fff', fontSize: '18px', display: 'flex', alignItems: 'center' }}
title="Twitter"
>
<TwitterOutlined />
</a>
<a
href="https://t.me/+5BwdYvvvuf9iZGZl"
target="_blank"
rel="noopener noreferrer"
style={{ color: '#fff', fontSize: '18px', display: 'flex', alignItems: 'center' }}
title="Telegram 交流群"
>
<SendOutlined />
</a>
</div>
</div>
<Menu
mode="inline"
selectedKeys={getSelectedKeys()}
openKeys={openKeys}
onOpenChange={handleOpenChange}
items={menuItems}
onClick={handleMenuClick}
style={{
height: 'calc(100vh - 100px)',
borderRight: 0,
overflowY: 'auto'
}}
/>
</Sider>
<AntLayout style={{ marginLeft: 200, height: '100vh' }}>
<Content style={{
padding: '24px',
background: '#f0f2f5',
height: '100vh',
overflowY: 'auto'
}}>
{children}
</Content>
</AntLayout>
</AntLayout>
)
}
export default Layout