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

285 lines
8.7 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';
import { useTranslation } from 'react-i18next';
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 {
2024-12-11 16:11:27 +08:00
IconClose,
2024-09-17 19:55:18 +08:00
IconHelpCircle,
IconHome,
2024-12-11 16:11:27 +08:00
IconHomeStroked, IconIndentLeft,
2024-09-18 10:29:25 +00:00
IconComment,
2024-12-11 16:11:27 +08:00
IconKey, IconMenu,
2024-09-17 19:55:18 +08:00
IconNoteMoneyStroked,
IconPriceTag,
IconUser,
IconLanguage
2024-09-17 19:55:18 +08:00
} from '@douyinfe/semi-icons';
2024-12-11 16:11:27 +08:00
import { Avatar, Button, Dropdown, Layout, Nav, Switch } from '@douyinfe/semi-ui';
2024-03-15 16:05:33 +08:00
import { stringToColor } from '../helpers/render';
2024-09-17 19:55:18 +08:00
import Text from '@douyinfe/semi-ui/lib/es/typography/text';
2024-12-11 16:11:27 +08:00
import { StyleContext } from '../context/Style/index.js';
2023-10-31 00:03:22 +08:00
const HeaderBar = () => {
const { t, i18n } = useTranslation();
2024-03-15 16:05:33 +08:00
const [userState, userDispatch] = useContext(UserContext);
2024-12-11 16:11:27 +08:00
const [styleState, styleDispatch] = useContext(StyleContext);
2024-03-15 16:05:33 +08:00
let navigate = useNavigate();
const [currentLang, setCurrentLang] = useState(i18n.language);
2023-10-31 00:03:22 +08:00
2024-03-15 16:05:33 +08:00
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);
2023-10-31 00:03:22 +08:00
2024-12-11 17:19:03 +08:00
let buttons = [
{
text: t('首页'),
2024-12-11 17:19:03 +08:00
itemKey: 'home',
to: '/',
},
{
text: t('控制台'),
2024-12-11 17:19:03 +08:00
itemKey: 'detail',
to: '/',
},
{
text: t('定价'),
itemKey: 'pricing',
to: '/pricing',
},
{
text: t('关于'),
itemKey: 'about',
to: '/about',
},
2024-12-11 17:19:03 +08:00
];
2024-03-15 16:05:33 +08:00
async function logout() {
await API.get('/api/user/logout');
showSuccess(t('注销成功!'));
2024-03-15 16:05:33 +08:00
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');
} else {
document.body.removeAttribute('theme-mode');
}
// 发送当前主题模式给子页面
const iframe = document.querySelector('iframe');
if (iframe) {
iframe.contentWindow.postMessage({ themeMode: theme }, '*');
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!');
}
}, [theme]);
2023-10-31 00:03:22 +08:00
useEffect(() => {
const handleLanguageChanged = (lng) => {
setCurrentLang(lng);
};
i18n.on('languageChanged', handleLanguageChanged);
return () => {
i18n.off('languageChanged', handleLanguageChanged);
};
}, [i18n]);
const handleLanguageChange = (lang) => {
i18n.changeLanguage(lang);
};
2024-03-15 16:05:33 +08:00
return (
<>
<Layout>
<div style={{ width: '100%' }}>
<Nav
className={'topnav'}
2024-03-15 16:05:33 +08:00
mode={'horizontal'}
renderWrapper={({ itemElement, isSubNav, isInSubNav, props }) => {
const routerMap = {
about: '/about',
login: '/login',
2024-03-23 21:24:39 +08:00
register: '/register',
pricing: '/pricing',
2024-12-11 17:19:03 +08:00
detail: '/detail',
2024-09-17 19:55:18 +08:00
home: '/',
2024-09-18 10:29:25 +00:00
chat: '/chat',
2024-03-15 16:05:33 +08:00
};
return (
2024-12-11 17:19:03 +08:00
<div onClick={(e) => {
if (props.itemKey === 'home') {
styleDispatch({ type: 'SET_INNER_PADDING', payload: false });
styleDispatch({ type: 'SET_SIDER', payload: false });
} else {
styleDispatch({ type: 'SET_INNER_PADDING', payload: true });
if (!styleState.isMobile) {
styleDispatch({ type: 'SET_SIDER', payload: true });
}
2024-12-11 17:19:03 +08:00
}
}}>
<Link
className="header-bar-text"
style={{ textDecoration: 'none' }}
to={routerMap[props.itemKey]}
>
{itemElement}
</Link>
</div>
2024-03-15 16:05:33 +08:00
);
}}
selectedKeys={[]}
// items={headerButtons}
2024-03-23 21:24:39 +08:00
onSelect={(key) => {}}
2024-12-11 16:11:27 +08:00
header={styleState.isMobile?{
2024-09-17 19:55:18 +08:00
logo: (
2024-12-11 16:11:27 +08:00
<>
{
!styleState.showSider ?
<Button icon={<IconMenu />} theme="light" aria-label={t('展开侧边栏')} onClick={
() => styleDispatch({ type: 'SET_SIDER', payload: true })
2024-12-11 16:11:27 +08:00
} />:
<Button icon={<IconIndentLeft />} theme="light" aria-label={t('闭侧边栏')} onClick={
() => styleDispatch({ type: 'SET_SIDER', payload: false })
2024-12-11 16:11:27 +08:00
} />
}
</>
2024-09-17 19:55:18 +08:00
),
}:{
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
)}
{/* <Nav.Item itemKey={'about'} icon={<IconHelpCircle />} /> */}
2024-09-17 19:55:18 +08:00
<>
2024-12-11 16:11:27 +08:00
<Switch
checkedText='🌞'
size={styleState.isMobile?'default':'large'}
2024-12-11 16:11:27 +08:00
checked={theme === 'dark'}
uncheckedText='🌙'
onChange={(checked) => {
setTheme(checked);
}}
/>
2024-09-17 19:55:18 +08:00
</>
<Dropdown
position='bottomRight'
render={
<Dropdown.Menu>
<Dropdown.Item
onClick={() => handleLanguageChange('zh')}
type={currentLang === 'zh' ? 'primary' : 'tertiary'}
>
中文
</Dropdown.Item>
<Dropdown.Item
onClick={() => handleLanguageChange('en')}
type={currentLang === 'en' ? 'primary' : 'tertiary'}
>
English
</Dropdown.Item>
</Dropdown.Menu>
}
>
<Nav.Item
itemKey={'language'}
icon={<IconLanguage />}
/>
</Dropdown>
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}>{t('退出')}</Dropdown.Item>
2024-03-15 16:05:33 +08:00
</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>
{styleState.isMobile?null:<Text>{userState.user.username}</Text>}
2024-03-15 16:05:33 +08:00
</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={!styleState.isMobile?t('登录'):null}
2024-03-23 21:24:39 +08:00
icon={<IconUser />}
/>
{
!styleState.isMobile && (
<Nav.Item
itemKey={'register'}
text={t('注册')}
icon={<IconKey />}
/>
)
}
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;