Files
new-api/web/src/components/HeaderBar.js

218 lines
5.9 KiB
JavaScript
Raw Normal View History

2024-03-15 16:05:33 +08:00
import React, { useContext, useEffect, useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { UserContext } from '../context/User';
2024-04-16 17:11:39 +08:00
import { useSetTheme, useTheme } from '../context/Theme';
2023-10-31 00:03:22 +08:00
2024-09-17 19:55:18 +08:00
import { API, getLogo, getSystemName, isMobile, showSuccess } from '../helpers';
2023-10-31 00:03:22 +08:00
import '../index.css';
2023-12-27 18:04:02 +08:00
import fireworks from 'react-fireworks';
2024-09-17 19:55:18 +08:00
import {
IconHelpCircle,
IconHome,
IconHomeStroked,
IconKey,
IconNoteMoneyStroked,
IconPriceTag,
IconUser
} from '@douyinfe/semi-icons';
2024-03-15 16:05:33 +08:00
import { Avatar, Dropdown, Layout, Nav, Switch } from '@douyinfe/semi-ui';
import { stringToColor } from '../helpers/render';
2024-09-17 19:55:18 +08:00
import Text from '@douyinfe/semi-ui/lib/es/typography/text';
2023-10-31 00:03:22 +08:00
// HeaderBar Buttons
let headerButtons = [
2024-03-15 16:05:33 +08:00
{
text: '关于',
itemKey: 'about',
to: '/about',
2024-03-23 21:24:39 +08:00
icon: <IconHelpCircle />,
},
2023-10-31 00:03:22 +08:00
];
2024-09-17 19:55:18 +08:00
let buttons = [
{
text: '首页',
itemKey: 'home',
to: '/',
// icon: <IconHomeStroked />,
2024-09-17 19:55:18 +08:00
},
// {
2024-09-26 00:59:09 +08:00
// text: 'Playground',
// itemKey: 'playground',
// to: '/playground',
// // icon: <IconNoteMoneyStroked />,
2024-09-17 19:55:18 +08:00
// },
];
2023-10-31 00:03:22 +08:00
if (localStorage.getItem('chat_link')) {
2024-03-15 16:05:33 +08:00
headerButtons.splice(1, 0, {
name: '聊天',
to: '/chat',
2024-03-23 21:24:39 +08:00
icon: 'comments',
2024-03-15 16:05:33 +08:00
});
2023-10-31 00:03:22 +08:00
}
const HeaderBar = () => {
2024-03-15 16:05:33 +08:00
const [userState, userDispatch] = useContext(UserContext);
let navigate = useNavigate();
2023-10-31 00:03:22 +08:00
2024-03-15 16:05:33 +08:00
const [showSidebar, setShowSidebar] = useState(false);
const systemName = getSystemName();
const logo = getLogo();
const currentDate = new Date();
// enable fireworks on new year(1.1 and 2.9-2.24)
2024-03-23 21:24:39 +08:00
const isNewYear =
(currentDate.getMonth() === 0 && currentDate.getDate() === 1) ||
(currentDate.getMonth() === 1 &&
currentDate.getDate() >= 9 &&
currentDate.getDate() <= 24);
2023-10-31 00:03:22 +08:00
2024-03-15 16:05:33 +08:00
async function logout() {
setShowSidebar(false);
await API.get('/api/user/logout');
showSuccess('注销成功!');
userDispatch({ type: 'logout' });
localStorage.removeItem('user');
navigate('/login');
}
2023-10-31 00:03:22 +08:00
2024-03-15 16:05:33 +08:00
const handleNewYearClick = () => {
fireworks.init('root', {});
fireworks.start();
setTimeout(() => {
fireworks.stop();
setTimeout(() => {
window.location.reload();
}, 10000);
}, 3000);
};
2023-12-27 18:04:02 +08:00
2024-04-16 17:11:39 +08:00
const theme = useTheme();
const setTheme = useSetTheme();
2024-03-15 16:05:33 +08:00
useEffect(() => {
2024-04-16 17:11:39 +08:00
if (theme === 'dark') {
document.body.setAttribute('theme-mode', 'dark');
2024-03-15 16:05:33 +08:00
}
2024-04-16 17:11:39 +08:00
2024-03-15 16:05:33 +08:00
if (isNewYear) {
console.log('Happy New Year!');
}
}, []);
2023-10-31 00:03:22 +08:00
2024-03-15 16:05:33 +08:00
return (
<>
<Layout>
<div style={{ width: '100%' }}>
<Nav
mode={'horizontal'}
// bodyStyle={{ height: 100 }}
renderWrapper={({ itemElement, isSubNav, isInSubNav, props }) => {
const routerMap = {
about: '/about',
login: '/login',
2024-03-23 21:24:39 +08:00
register: '/register',
2024-09-17 19:55:18 +08:00
home: '/',
2024-03-15 16:05:33 +08:00
};
return (
<Link
style={{ textDecoration: 'none' }}
to={routerMap[props.itemKey]}
>
{itemElement}
</Link>
);
}}
selectedKeys={[]}
// items={headerButtons}
2024-03-23 21:24:39 +08:00
onSelect={(key) => {}}
2024-09-17 19:55:18 +08:00
header={isMobile()?{
logo: (
<img src={logo} alt='logo' style={{ marginRight: '0.75em' }} />
),
}:{
logo: (
<img src={logo} alt='logo' />
),
text: systemName,
}}
items={buttons}
2024-03-15 16:05:33 +08:00
footer={
<>
2024-03-23 21:24:39 +08:00
{isNewYear && (
2024-03-15 16:05:33 +08:00
// happy new year
<Dropdown
2024-03-23 21:24:39 +08:00
position='bottomRight'
2024-03-15 16:05:33 +08:00
render={
<Dropdown.Menu>
2024-03-23 21:24:39 +08:00
<Dropdown.Item onClick={handleNewYearClick}>
Happy New Year!!!
</Dropdown.Item>
2024-03-15 16:05:33 +08:00
</Dropdown.Menu>
}
>
<Nav.Item itemKey={'new-year'} text={'🏮'} />
</Dropdown>
2024-03-23 21:24:39 +08:00
)}
2024-03-15 16:05:33 +08:00
<Nav.Item itemKey={'about'} icon={<IconHelpCircle />} />
2024-09-17 19:55:18 +08:00
<>
{!isMobile() && (
<Switch
checkedText='🌞'
size={'large'}
checked={theme === 'dark'}
uncheckedText='🌙'
onChange={(checked) => {
setTheme(checked);
}}
/>
)}
</>
2024-03-23 21:24:39 +08:00
{userState.user ? (
2024-03-15 16:05:33 +08:00
<>
<Dropdown
2024-03-23 21:24:39 +08:00
position='bottomRight'
2024-03-15 16:05:33 +08:00
render={
<Dropdown.Menu>
<Dropdown.Item onClick={logout}>退出</Dropdown.Item>
</Dropdown.Menu>
}
2023-10-31 00:03:22 +08:00
>
2024-03-23 21:24:39 +08:00
<Avatar
size='small'
color={stringToColor(userState.user.username)}
style={{ margin: 4 }}
>
2024-03-15 16:05:33 +08:00
{userState.user.username[0]}
</Avatar>
<span>{userState.user.username}</span>
</Dropdown>
</>
2024-03-23 21:24:39 +08:00
) : (
2024-03-15 16:05:33 +08:00
<>
2024-03-23 21:24:39 +08:00
<Nav.Item
itemKey={'login'}
text={'登录'}
2024-09-17 19:55:18 +08:00
// icon={<IconKey />}
2024-03-23 21:24:39 +08:00
/>
<Nav.Item
itemKey={'register'}
text={'注册'}
icon={<IconUser />}
/>
2024-03-15 16:05:33 +08:00
</>
2024-03-23 21:24:39 +08:00
)}
2024-03-15 16:05:33 +08:00
</>
}
2024-03-23 21:24:39 +08:00
></Nav>
2024-03-15 16:05:33 +08:00
</div>
</Layout>
</>
);
2023-10-31 00:03:22 +08:00
};
export default HeaderBar;