sidebar.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { useEffect, useRef, useMemo } from "react";
  2. import styles from "./home.module.scss";
  3. import { IconButton } from "./button";
  4. import SettingsIcon from "../icons/settings.svg";
  5. import GithubIcon from "../icons/github.svg";
  6. import ChatGptIcon from "../icons/chatgpt.svg";
  7. import DiitIcon from "../icons/diit.svg";
  8. import AddIcon from "../icons/add.svg";
  9. import CloseIcon from "../icons/close.svg";
  10. import DeleteIcon from "../icons/delete.svg";
  11. import MaskIcon from "../icons/mask.svg";
  12. import PluginIcon from "../icons/plugin.svg";
  13. import DragIcon from "../icons/drag.svg";
  14. import Locale from "../locales";
  15. import { useAppConfig, useChatStore } from "../store";
  16. import {
  17. DEFAULT_SIDEBAR_WIDTH,
  18. MAX_SIDEBAR_WIDTH,
  19. MIN_SIDEBAR_WIDTH,
  20. NARROW_SIDEBAR_WIDTH,
  21. Path,
  22. REPO_URL,
  23. } from "../constant";
  24. import { Link, useNavigate } from "react-router-dom";
  25. import { isIOS, useMobileScreen } from "../utils";
  26. import dynamic from "next/dynamic";
  27. import { showConfirm, showToast } from "./ui-lib";
  28. const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
  29. loading: () => null,
  30. });
  31. function useHotKey() {
  32. const chatStore = useChatStore();
  33. useEffect(() => {
  34. const onKeyDown = (e: KeyboardEvent) => {
  35. if (e.altKey || e.ctrlKey) {
  36. if (e.key === "ArrowUp") {
  37. chatStore.nextSession(-1);
  38. } else if (e.key === "ArrowDown") {
  39. chatStore.nextSession(1);
  40. }
  41. }
  42. };
  43. window.addEventListener("keydown", onKeyDown);
  44. return () => window.removeEventListener("keydown", onKeyDown);
  45. });
  46. }
  47. function useDragSideBar() {
  48. const limit = (x: number) => Math.min(MAX_SIDEBAR_WIDTH, x);
  49. const config = useAppConfig();
  50. const startX = useRef(0);
  51. const startDragWidth = useRef(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
  52. const lastUpdateTime = useRef(Date.now());
  53. const toggleSideBar = () => {
  54. config.update((config) => {
  55. if (config.sidebarWidth < MIN_SIDEBAR_WIDTH) {
  56. config.sidebarWidth = DEFAULT_SIDEBAR_WIDTH;
  57. } else {
  58. config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
  59. }
  60. });
  61. };
  62. const onDragStart = (e: MouseEvent) => {
  63. // Remembers the initial width each time the mouse is pressed
  64. startX.current = e.clientX;
  65. startDragWidth.current = config.sidebarWidth;
  66. const dragStartTime = Date.now();
  67. const handleDragMove = (e: MouseEvent) => {
  68. if (Date.now() < lastUpdateTime.current + 20) {
  69. return;
  70. }
  71. lastUpdateTime.current = Date.now();
  72. const d = e.clientX - startX.current;
  73. const nextWidth = limit(startDragWidth.current + d);
  74. config.update((config) => {
  75. if (nextWidth < MIN_SIDEBAR_WIDTH) {
  76. config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
  77. } else {
  78. config.sidebarWidth = nextWidth;
  79. }
  80. });
  81. };
  82. const handleDragEnd = () => {
  83. // In useRef the data is non-responsive, so `config.sidebarWidth` can't get the dynamic sidebarWidth
  84. window.removeEventListener("pointermove", handleDragMove);
  85. window.removeEventListener("pointerup", handleDragEnd);
  86. // if user click the drag icon, should toggle the sidebar
  87. const shouldFireClick = Date.now() - dragStartTime < 300;
  88. if (shouldFireClick) {
  89. toggleSideBar();
  90. }
  91. };
  92. window.addEventListener("pointermove", handleDragMove);
  93. window.addEventListener("pointerup", handleDragEnd);
  94. };
  95. const isMobileScreen = useMobileScreen();
  96. const shouldNarrow =
  97. !isMobileScreen && config.sidebarWidth < MIN_SIDEBAR_WIDTH;
  98. useEffect(() => {
  99. const barWidth = shouldNarrow
  100. ? NARROW_SIDEBAR_WIDTH
  101. : limit(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
  102. const sideBarWidth = isMobileScreen ? "100vw" : `${barWidth}px`;
  103. document.documentElement.style.setProperty("--sidebar-width", sideBarWidth);
  104. }, [config.sidebarWidth, isMobileScreen, shouldNarrow]);
  105. return {
  106. onDragStart,
  107. shouldNarrow,
  108. };
  109. }
  110. export function SideBar(props: { className?: string }) {
  111. const chatStore = useChatStore();
  112. // drag side bar
  113. const { onDragStart, shouldNarrow } = useDragSideBar();
  114. const navigate = useNavigate();
  115. const config = useAppConfig();
  116. const isMobileScreen = useMobileScreen();
  117. const isIOSMobile = useMemo(
  118. () => isIOS() && isMobileScreen,
  119. [isMobileScreen],
  120. );
  121. useHotKey();
  122. return (
  123. <div
  124. className={`${styles.sidebar} ${props.className} ${
  125. shouldNarrow && styles["narrow-sidebar"]
  126. }`}
  127. style={{
  128. // #3016 disable transition on ios mobile screen
  129. transition: isMobileScreen && isIOSMobile ? "none" : undefined,
  130. }}
  131. >
  132. <div className={styles["sidebar-header"]} data-tauri-drag-region>
  133. <div className={styles["sidebar-title"]} data-tauri-drag-region>
  134. DiitChat
  135. </div>
  136. {/*<div className={styles["sidebar-sub-title"]}>*/}
  137. {/* Build your own AI assistant.*/}
  138. {/*</div>*/}
  139. <div className={styles["sidebar-logo"] + " no-dark"}>
  140. <DiitIcon />
  141. </div>
  142. </div>
  143. {/*<div className={styles["sidebar-header-bar"]}>*/}
  144. {/* <IconButton*/}
  145. {/* icon={<MaskIcon />}*/}
  146. {/* text={shouldNarrow ? undefined : Locale.Mask.Name}*/}
  147. {/* className={styles["sidebar-bar-button"]}*/}
  148. {/* onClick={() => {*/}
  149. {/* if (config.dontShowMaskSplashScreen !== true) {*/}
  150. {/* navigate(Path.NewChat, { state: { fromHome: true } });*/}
  151. {/* } else {*/}
  152. {/* navigate(Path.Masks, { state: { fromHome: true } });*/}
  153. {/* }*/}
  154. {/* }}*/}
  155. {/* shadow*/}
  156. {/* />*/}
  157. {/* <IconButton*/}
  158. {/* icon={<PluginIcon />}*/}
  159. {/* text={shouldNarrow ? undefined : Locale.Plugin.Name}*/}
  160. {/* className={styles["sidebar-bar-button"]}*/}
  161. {/* onClick={() => showToast(Locale.WIP)}*/}
  162. {/* shadow*/}
  163. {/* />*/}
  164. {/*</div>*/}
  165. <div
  166. className={styles["sidebar-body"]}
  167. onClick={(e) => {
  168. if (e.target === e.currentTarget) {
  169. navigate(Path.Home);
  170. }
  171. }}
  172. >
  173. <ChatList narrow={shouldNarrow} />
  174. </div>
  175. <div className={styles["sidebar-tail"]}>
  176. <div className={styles["sidebar-actions"]}>
  177. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  178. <IconButton
  179. icon={<DeleteIcon />}
  180. onClick={async () => {
  181. if (await showConfirm(Locale.Home.DeleteChat)) {
  182. chatStore.deleteSession(chatStore.currentSessionIndex);
  183. }
  184. }}
  185. />
  186. </div>
  187. <div className={styles["sidebar-action"]}>
  188. <Link to={Path.Settings}>
  189. <IconButton icon={<SettingsIcon />} shadow />
  190. </Link>
  191. </div>
  192. {/*<div className={styles["sidebar-action"]}>*/}
  193. {/* <a href={REPO_URL} target="_blank" rel="noopener noreferrer">*/}
  194. {/* <IconButton icon={<GithubIcon />} shadow />*/}
  195. {/* </a>*/}
  196. {/*</div>*/}
  197. </div>
  198. <div>
  199. <IconButton
  200. icon={<AddIcon />}
  201. text={shouldNarrow ? undefined : Locale.Home.NewChat}
  202. onClick={() => {
  203. if (config.dontShowMaskSplashScreen) {
  204. chatStore.newSession();
  205. navigate(Path.Chat);
  206. } else {
  207. navigate(Path.NewChat);
  208. }
  209. }}
  210. shadow
  211. />
  212. </div>
  213. </div>
  214. <div
  215. className={styles["sidebar-drag"]}
  216. onPointerDown={(e) => onDragStart(e as any)}
  217. >
  218. <DragIcon />
  219. </div>
  220. </div>
  221. );
  222. }