sidebar.tsx 7.0 KB

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