SiderBar.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. IconSemiLogo,
  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: 'token',
  39. to: '/token',
  40. icon: <IconKey/>
  41. },
  42. {
  43. text: '兑换码',
  44. itemKey: 'redemption',
  45. to: '/redemption',
  46. icon: <IconGift/>,
  47. className: isAdmin()?'semi-navigation-item-normal':'tableHiddle',
  48. },
  49. {
  50. text: '钱包',
  51. itemKey: 'topup',
  52. to: '/topup',
  53. icon: <IconCreditCard/>
  54. },
  55. {
  56. text: '用户管理',
  57. itemKey: 'user',
  58. to: '/user',
  59. icon: <IconUser/>,
  60. className: isAdmin()?'semi-navigation-item-normal':'tableHiddle',
  61. },
  62. {
  63. text: '日志',
  64. itemKey: 'log',
  65. to: '/log',
  66. icon: <IconHistogram/>
  67. },
  68. {
  69. text: '绘图',
  70. itemKey: 'midjourney',
  71. to: '/midjourney',
  72. icon: <IconImage/>
  73. },
  74. {
  75. text: '设置',
  76. itemKey: 'setting',
  77. to: '/setting',
  78. icon: <IconSetting/>
  79. },
  80. // {
  81. // text: '关于',
  82. // itemKey: 'about',
  83. // to: '/about',
  84. // icon: <IconAt/>
  85. // }
  86. ];
  87. if (localStorage.getItem('chat_link')) {
  88. headerButtons.splice(1, 0, {
  89. name: '聊天',
  90. to: '/chat',
  91. icon: 'comments'
  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. const toggleSidebar = () => {
  110. setShowSidebar(!showSidebar);
  111. };
  112. const renderButtons = (isMobile) => {
  113. return headerButtons.map((button) => {
  114. if (button.admin && !isAdmin()) return <></>;
  115. if (isMobile) {
  116. return (
  117. <Menu.Item
  118. onClick={() => {
  119. navigate(button.to);
  120. setShowSidebar(false);
  121. }}
  122. >
  123. {button.name}
  124. </Menu.Item>
  125. );
  126. }
  127. return (
  128. <Menu.Item key={button.name} as={Link} to={button.to}>
  129. <Icon name={button.icon}/>
  130. {button.name}
  131. </Menu.Item>
  132. );
  133. });
  134. };
  135. return (
  136. <>
  137. <Layout>
  138. <div style={{height: '100%'}}>
  139. <Nav
  140. // mode={'horizontal'}
  141. // bodyStyle={{ height: 100 }}
  142. selectedKeys={selectedKeys}
  143. renderWrapper={({itemElement, isSubNav, isInSubNav, props}) => {
  144. const routerMap = {
  145. home: "/",
  146. channel: "/channel",
  147. token: "/token",
  148. redemption: "/redemption",
  149. topup: "/topup",
  150. user: "/user",
  151. log: "/log",
  152. midjourney: "/midjourney",
  153. setting: "/setting",
  154. about: "/about",
  155. };
  156. return (
  157. <Link
  158. style={{textDecoration: "none"}}
  159. to={routerMap[props.itemKey]}
  160. >
  161. {itemElement}
  162. </Link>
  163. );
  164. }}
  165. items={headerButtons}
  166. onSelect={key => {
  167. console.log(key);
  168. setSelectedKeys([key.itemKey]);
  169. }}
  170. header={{
  171. logo: <img src={logo} alt='logo' style={{marginRight: '0.75em'}}/>,
  172. text: systemName,
  173. }}
  174. // footer={{
  175. // text: '© 2021 NekoAPI',
  176. // }}
  177. >
  178. <Nav.Footer collapseButton={true}>
  179. </Nav.Footer>
  180. </Nav>
  181. </div>
  182. </Layout>
  183. </>
  184. );
  185. };
  186. export default HeaderBar;