sidebar.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import React, { useEffect, useRef, useMemo, useState, Fragment } from "react";
  2. import styles from "./home.module.scss";
  3. import DragIcon from "../icons/drag.svg";
  4. import faviconSrc from "../icons/favicon.png";
  5. import deepSeekSrc from "../icons/deepSeek.png";
  6. import { MenuOutlined, PlusOutlined } from '@ant-design/icons';
  7. import { useAppConfig, useChatStore, useGlobalStore } from "../store";
  8. import {
  9. DEFAULT_SIDEBAR_WIDTH,
  10. MAX_SIDEBAR_WIDTH,
  11. MIN_SIDEBAR_WIDTH,
  12. NARROW_SIDEBAR_WIDTH,
  13. } from "../constant";
  14. import { useLocation, useNavigate } from "react-router-dom";
  15. import { isIOS, useMobileScreen } from "../utils";
  16. import { Button } from "antd";
  17. export function useHotKey() {
  18. const chatStore = useChatStore();
  19. useEffect(() => {
  20. const onKeyDown = (e: KeyboardEvent) => {
  21. if (e.altKey || e.ctrlKey) {
  22. if (e.key === "ArrowUp") {
  23. chatStore.nextSession(-1);
  24. } else if (e.key === "ArrowDown") {
  25. chatStore.nextSession(1);
  26. }
  27. }
  28. };
  29. window.addEventListener("keydown", onKeyDown);
  30. return () => window.removeEventListener("keydown", onKeyDown);
  31. });
  32. }
  33. export function useDragSideBar() {
  34. const limit = (x: number) => Math.min(MAX_SIDEBAR_WIDTH, x);
  35. const config = useAppConfig();
  36. const startX = useRef(0);
  37. const startDragWidth = useRef(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
  38. const lastUpdateTime = useRef(Date.now());
  39. const toggleSideBar = () => {
  40. config.update((config) => {
  41. if (config.sidebarWidth < MIN_SIDEBAR_WIDTH) {
  42. config.sidebarWidth = DEFAULT_SIDEBAR_WIDTH;
  43. } else {
  44. config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
  45. }
  46. });
  47. };
  48. const onDragStart = (e: MouseEvent) => {
  49. // Remembers the initial width each time the mouse is pressed
  50. startX.current = e.clientX;
  51. startDragWidth.current = config.sidebarWidth;
  52. const dragStartTime = Date.now();
  53. const handleDragMove = (e: MouseEvent) => {
  54. if (Date.now() < lastUpdateTime.current + 20) {
  55. return;
  56. }
  57. lastUpdateTime.current = Date.now();
  58. const d = e.clientX - startX.current;
  59. const nextWidth = limit(startDragWidth.current + d);
  60. config.update((config) => {
  61. if (nextWidth < MIN_SIDEBAR_WIDTH) {
  62. config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
  63. } else {
  64. config.sidebarWidth = nextWidth;
  65. }
  66. });
  67. };
  68. const handleDragEnd = () => {
  69. // In useRef the data is non-responsive, so `config.sidebarWidth` can't get the dynamic sidebarWidth
  70. window.removeEventListener("pointermove", handleDragMove);
  71. window.removeEventListener("pointerup", handleDragEnd);
  72. // if user click the drag icon, should toggle the sidebar
  73. const shouldFireClick = Date.now() - dragStartTime < 300;
  74. if (shouldFireClick) {
  75. toggleSideBar();
  76. }
  77. };
  78. window.addEventListener("pointermove", handleDragMove);
  79. window.addEventListener("pointerup", handleDragEnd);
  80. };
  81. const isMobileScreen = useMobileScreen();
  82. const shouldNarrow =
  83. !isMobileScreen && config.sidebarWidth < MIN_SIDEBAR_WIDTH;
  84. useEffect(() => {
  85. const barWidth = shouldNarrow
  86. ? NARROW_SIDEBAR_WIDTH
  87. : limit(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
  88. const sideBarWidth = isMobileScreen ? "60vw" : `${barWidth}px`;
  89. document.documentElement.style.setProperty("--sidebar-width", sideBarWidth);
  90. }, [config.sidebarWidth, isMobileScreen, shouldNarrow]);
  91. return {
  92. onDragStart,
  93. shouldNarrow,
  94. };
  95. }
  96. export function SideBarContainer(props: {
  97. children: React.ReactNode;
  98. onDragStart: (e: MouseEvent) => void;
  99. shouldNarrow: boolean;
  100. className?: string;
  101. }) {
  102. const isMobileScreen = useMobileScreen();
  103. const isIOSMobile = useMemo(
  104. () => isIOS() && isMobileScreen,
  105. [isMobileScreen],
  106. );
  107. const { children, className, onDragStart, shouldNarrow } = props;
  108. return (
  109. <div
  110. className={`${styles.sidebar} ${className} ${shouldNarrow && styles["narrow-sidebar"]
  111. }`}
  112. style={{
  113. transition: isMobileScreen && isIOSMobile ? "none" : undefined,
  114. background: '#FFFFFF',
  115. overflowY: "auto",
  116. }}
  117. >
  118. {children}
  119. <div
  120. className={styles["sidebar-drag"]}
  121. onPointerDown={(e) => onDragStart(e as any)}
  122. >
  123. <DragIcon />
  124. </div>
  125. </div>
  126. );
  127. }
  128. export function SideBarHeader(props: {
  129. title?: string | React.ReactNode;
  130. subTitle?: string | React.ReactNode;
  131. logo?: React.ReactNode;
  132. children?: React.ReactNode;
  133. }) {
  134. const { title, subTitle, logo, children } = props;
  135. return (
  136. <Fragment>
  137. <div className={styles["sidebar-header"]} data-tauri-drag-region>
  138. <div className={styles["sidebar-title-container"]}>
  139. <div className={styles["sidebar-title"]} data-tauri-drag-region>
  140. {title}
  141. </div>
  142. <div className={styles["sidebar-sub-title"]}>{subTitle}</div>
  143. </div>
  144. <div className={styles["sidebar-logo"] + " no-dark"}>{logo}</div>
  145. </div>
  146. {children}
  147. </Fragment>
  148. );
  149. }
  150. export function SideBarBody(props: {
  151. children: React.ReactNode;
  152. onClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
  153. }) {
  154. const { onClick, children } = props;
  155. return (
  156. <div className={styles["sidebar-body"]} onClick={onClick}>
  157. {children}
  158. </div>
  159. );
  160. }
  161. export function SideBarTail(props: {
  162. primaryAction?: React.ReactNode;
  163. secondaryAction?: React.ReactNode;
  164. }) {
  165. const { primaryAction, secondaryAction } = props;
  166. return (
  167. <div className={styles["sidebar-tail"]}>
  168. <div className={styles["sidebar-actions"]}>{primaryAction}</div>
  169. <div className={styles["sidebar-actions"]}>{secondaryAction}</div>
  170. </div>
  171. );
  172. }
  173. export const SideBar = (props: { className?: string }) => {
  174. const { onDragStart, shouldNarrow } = useDragSideBar();
  175. const navigate = useNavigate();
  176. const location = useLocation();
  177. const chatStore = useChatStore();
  178. const globalStore = useGlobalStore();
  179. const isMobileScreen = useMobileScreen();
  180. const getType = (): 'bigModel' | 'deepSeek' => {
  181. if (['/knowledgeChat', '/newChat'].includes(location.pathname)) {
  182. return 'bigModel';
  183. } else if (['/deepseekChat', '/newDeepseekChat'].includes(location.pathname)) {
  184. return 'deepSeek';
  185. } else {
  186. return 'bigModel';
  187. }
  188. }
  189. const closeSidebar = () => {
  190. globalStore.setShowMenu(false);
  191. };
  192. return (
  193. <>
  194. {
  195. isMobileScreen && globalStore.showMenu && (
  196. <div
  197. className={styles["sidebar-overlay"]}
  198. onClick={closeSidebar}
  199. />
  200. )
  201. }
  202. {
  203. (location.search.includes('showMenu=false') ? globalStore.showMenu : true) &&
  204. <SideBarContainer
  205. onDragStart={onDragStart}
  206. shouldNarrow={shouldNarrow}
  207. {...props}
  208. >
  209. {
  210. getType() === 'deepSeek' &&
  211. <div>
  212. <img style={{ width: '100%' }} src={deepSeekSrc.src} />
  213. </div>
  214. }
  215. <SideBarHeader
  216. title={getType() === 'bigModel' ?
  217. <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
  218. <img style={{ height: 32 }} src={faviconSrc.src} />
  219. <span>建科•小智</span>
  220. </div>
  221. :
  222. ''
  223. }
  224. logo={getType() === 'bigModel' ? null : ''}
  225. >
  226. <div style={{ marginTop: 20 }}>
  227. <Button
  228. style={{ width: '100%' }}
  229. type="primary"
  230. icon={<PlusOutlined />}
  231. onClick={async () => {
  232. chatStore.clearSessions();
  233. chatStore.updateCurrentSession((value) => {
  234. value.appId = globalStore.selectedAppId;
  235. });
  236. if (isMobileScreen) {
  237. closeSidebar();
  238. }
  239. if (getType() === 'bigModel') {
  240. navigate({ pathname: '/newChat' });
  241. } else {
  242. navigate({ pathname: '/newDeepseekChat' });
  243. }
  244. }}
  245. >
  246. 新建对话
  247. </Button>
  248. </div>
  249. </SideBarHeader>
  250. </SideBarContainer>
  251. }
  252. </>
  253. );
  254. }