SiderBar.js 5.8 KB

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