SiderBar.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React, {useContext, useState} from 'react';
  2. import {Link, useNavigate} from 'react-router-dom';
  3. import {UserContext} from '../context/User';
  4. import {Button, Container, Icon, Menu, Segment} from 'semantic-ui-react';
  5. import {API, getLogo, getSystemName, isAdmin, isMobile, showSuccess} from '../helpers';
  6. import '../index.css';
  7. import {
  8. IconAt,
  9. IconHistogram,
  10. IconGift,
  11. IconKey,
  12. IconUser,
  13. IconLayers,
  14. IconSetting,
  15. IconCreditCard,
  16. IconComment,
  17. IconHome,
  18. IconImage
  19. } from '@douyinfe/semi-icons';
  20. import {Nav, Avatar, Dropdown, Layout} from '@douyinfe/semi-ui';
  21. // HeaderBar Buttons
  22. let headerButtons = [
  23. {
  24. text: '首页',
  25. itemKey: 'home',
  26. to: '/',
  27. icon: <IconHome/>
  28. },
  29. {
  30. text: '渠道',
  31. itemKey: 'channel',
  32. to: '/channel',
  33. icon: <IconLayers/>,
  34. className: isAdmin()?'semi-navigation-item-normal':'tableHiddle',
  35. },
  36. {
  37. text: '聊天',
  38. itemKey: 'chat',
  39. to: '/chat',
  40. icon: <IconComment />,
  41. className: localStorage.getItem('chat_link')?'semi-navigation-item-normal':'tableHiddle',
  42. },
  43. {
  44. text: '令牌',
  45. itemKey: 'token',
  46. to: '/token',
  47. icon: <IconKey/>
  48. },
  49. {
  50. text: '兑换码',
  51. itemKey: 'redemption',
  52. to: '/redemption',
  53. icon: <IconGift/>,
  54. className: isAdmin()?'semi-navigation-item-normal':'tableHiddle',
  55. },
  56. {
  57. text: '钱包',
  58. itemKey: 'topup',
  59. to: '/topup',
  60. icon: <IconCreditCard/>
  61. },
  62. {
  63. text: '用户管理',
  64. itemKey: 'user',
  65. to: '/user',
  66. icon: <IconUser/>,
  67. className: isAdmin()?'semi-navigation-item-normal':'tableHiddle',
  68. },
  69. {
  70. text: '日志',
  71. itemKey: 'log',
  72. to: '/log',
  73. icon: <IconHistogram/>
  74. },
  75. {
  76. text: '绘图',
  77. itemKey: 'midjourney',
  78. to: '/midjourney',
  79. icon: <IconImage/>
  80. },
  81. {
  82. text: '设置',
  83. itemKey: 'setting',
  84. to: '/setting',
  85. icon: <IconSetting/>
  86. },
  87. // {
  88. // text: '关于',
  89. // itemKey: 'about',
  90. // to: '/about',
  91. // icon: <IconAt/>
  92. // }
  93. ];
  94. const HeaderBar = () => {
  95. const [userState, userDispatch] = useContext(UserContext);
  96. let navigate = useNavigate();
  97. const [selectedKeys, setSelectedKeys] = useState(['home']);
  98. const [showSidebar, setShowSidebar] = useState(false);
  99. const systemName = getSystemName();
  100. const logo = getLogo();
  101. async function logout() {
  102. setShowSidebar(false);
  103. await API.get('/api/user/logout');
  104. showSuccess('注销成功!');
  105. userDispatch({type: 'logout'});
  106. localStorage.removeItem('user');
  107. navigate('/login');
  108. }
  109. return (
  110. <>
  111. <Layout>
  112. <div style={{height: '100%'}}>
  113. <Nav
  114. // mode={'horizontal'}
  115. // bodyStyle={{ height: 100 }}
  116. selectedKeys={selectedKeys}
  117. renderWrapper={({itemElement, isSubNav, isInSubNav, props}) => {
  118. const routerMap = {
  119. home: "/",
  120. channel: "/channel",
  121. token: "/token",
  122. redemption: "/redemption",
  123. topup: "/topup",
  124. user: "/user",
  125. log: "/log",
  126. midjourney: "/midjourney",
  127. setting: "/setting",
  128. about: "/about",
  129. chat: "/chat",
  130. };
  131. return (
  132. <Link
  133. style={{textDecoration: "none"}}
  134. to={routerMap[props.itemKey]}
  135. >
  136. {itemElement}
  137. </Link>
  138. );
  139. }}
  140. items={headerButtons}
  141. onSelect={key => {
  142. console.log(key);
  143. setSelectedKeys([key.itemKey]);
  144. }}
  145. header={{
  146. logo: <img src={logo} alt='logo' style={{marginRight: '0.75em'}}/>,
  147. text: systemName,
  148. }}
  149. // footer={{
  150. // text: '© 2021 NekoAPI',
  151. // }}
  152. >
  153. <Nav.Footer collapseButton={true}>
  154. </Nav.Footer>
  155. </Nav>
  156. </div>
  157. </Layout>
  158. </>
  159. );
  160. };
  161. export default HeaderBar;