| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- import React, { useContext, useEffect, useState } from 'react';
- import { Link, useNavigate } from 'react-router-dom';
- import { UserContext } from '../context/User';
- import { useSetTheme, useTheme } from '../context/Theme';
- import { API, getLogo, getSystemName, isMobile, showSuccess } from '../helpers';
- import '../index.css';
- import fireworks from 'react-fireworks';
- import {
- IconClose,
- IconHelpCircle,
- IconHome,
- IconHomeStroked, IconIndentLeft,
- IconKey, IconMenu,
- IconNoteMoneyStroked,
- IconPriceTag,
- IconUser
- } from '@douyinfe/semi-icons';
- import { Avatar, Button, Dropdown, Layout, Nav, Switch } from '@douyinfe/semi-ui';
- import { stringToColor } from '../helpers/render';
- import Text from '@douyinfe/semi-ui/lib/es/typography/text';
- import { StyleContext } from '../context/Style/index.js';
- // HeaderBar Buttons
- let headerButtons = [
- {
- text: '关于',
- itemKey: 'about',
- to: '/about',
- icon: <IconHelpCircle />,
- },
- ];
- if (localStorage.getItem('chat_link')) {
- headerButtons.splice(1, 0, {
- name: '聊天',
- to: '/chat',
- icon: 'comments',
- });
- }
- const HeaderBar = () => {
- const [userState, userDispatch] = useContext(UserContext);
- const [styleState, styleDispatch] = useContext(StyleContext);
- let navigate = useNavigate();
- const systemName = getSystemName();
- const logo = getLogo();
- const currentDate = new Date();
- // enable fireworks on new year(1.1 and 2.9-2.24)
- const isNewYear =
- (currentDate.getMonth() === 0 && currentDate.getDate() === 1) ||
- (currentDate.getMonth() === 1 &&
- currentDate.getDate() >= 9 &&
- currentDate.getDate() <= 24);
- let buttons = [
- {
- text: '首页',
- itemKey: 'home',
- to: '/',
- },
- {
- text: '控制台',
- itemKey: 'detail',
- to: '/',
- },
- ];
- async function logout() {
- await API.get('/api/user/logout');
- showSuccess('注销成功!');
- userDispatch({ type: 'logout' });
- localStorage.removeItem('user');
- navigate('/login');
- }
- const handleNewYearClick = () => {
- fireworks.init('root', {});
- fireworks.start();
- setTimeout(() => {
- fireworks.stop();
- setTimeout(() => {
- window.location.reload();
- }, 10000);
- }, 3000);
- };
- const theme = useTheme();
- const setTheme = useSetTheme();
- useEffect(() => {
- if (theme === 'dark') {
- document.body.setAttribute('theme-mode', 'dark');
- }
- if (isNewYear) {
- console.log('Happy New Year!');
- }
- }, []);
- return (
- <>
- <Layout>
- <div style={{ width: '100%' }}>
- <Nav
- mode={'horizontal'}
- renderWrapper={({ itemElement, isSubNav, isInSubNav, props }) => {
- const routerMap = {
- about: '/about',
- login: '/login',
- register: '/register',
- detail: '/detail',
- home: '/',
- };
- return (
- <div onClick={(e) => {
- if (props.itemKey === 'home') {
- styleDispatch({ type: 'SET_INNER_PADDING', payload: true });
- styleDispatch({ type: 'SET_SIDER', payload: true });
- } else {
- styleDispatch({ type: 'SET_INNER_PADDING', payload: false });
- styleDispatch({ type: 'SET_SIDER', payload: false });
- }
- }}>
- <Link
- className="header-bar-text"
- style={{ textDecoration: 'none' }}
- to={routerMap[props.itemKey]}
- >
- {itemElement}
- </Link>
- </div>
- );
- }}
- selectedKeys={[]}
- // items={headerButtons}
- onSelect={(key) => {}}
- header={styleState.isMobile?{
- logo: (
- <>
- {
- styleState.showSider ?
- <Button icon={<IconMenu />} theme="light" aria-label="展开侧边栏" onClick={
- () => styleDispatch({ type: 'SET_SIDER', payload: false })
- } />:
- <Button icon={<IconIndentLeft />} theme="light" aria-label="关闭侧边栏" onClick={
- () => styleDispatch({ type: 'SET_SIDER', payload: true })
- } />
- }
- </>
- ),
- }:{
- logo: (
- <img src={logo} alt='logo' />
- ),
- text: systemName,
- }}
- items={buttons}
- footer={
- <>
- {isNewYear && (
- // happy new year
- <Dropdown
- position='bottomRight'
- render={
- <Dropdown.Menu>
- <Dropdown.Item onClick={handleNewYearClick}>
- Happy New Year!!!
- </Dropdown.Item>
- </Dropdown.Menu>
- }
- >
- <Nav.Item itemKey={'new-year'} text={'🏮'} />
- </Dropdown>
- )}
- <Nav.Item itemKey={'about'} icon={<IconHelpCircle />} />
- <>
- <Switch
- checkedText='🌞'
- size={'large'}
- checked={theme === 'dark'}
- uncheckedText='🌙'
- onChange={(checked) => {
- setTheme(checked);
- }}
- />
- </>
- {userState.user ? (
- <>
- <Dropdown
- position='bottomRight'
- render={
- <Dropdown.Menu>
- <Dropdown.Item onClick={logout}>退出</Dropdown.Item>
- </Dropdown.Menu>
- }
- >
- <Avatar
- size='small'
- color={stringToColor(userState.user.username)}
- style={{ margin: 4 }}
- >
- {userState.user.username[0]}
- </Avatar>
- <span>{userState.user.username}</span>
- </Dropdown>
- </>
- ) : (
- <>
- <Nav.Item
- itemKey={'login'}
- text={'登录'}
- // icon={<IconKey />}
- />
- <Nav.Item
- itemKey={'register'}
- text={'注册'}
- icon={<IconUser />}
- />
- </>
- )}
- </>
- }
- ></Nav>
- </div>
- </Layout>
- </>
- );
- };
- export default HeaderBar;
|