sidebar.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { useEffect, useRef, useCallback, 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 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 { isIOS, 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. const isMobileScreen = useMobileScreen();
  115. const isIOSMobile = useMemo(
  116. () => isIOS() && isMobileScreen,
  117. [isMobileScreen],
  118. );
  119. useHotKey();
  120. return (
  121. <div
  122. className={`${styles.sidebar} ${props.className} ${
  123. shouldNarrow && styles["narrow-sidebar"]
  124. }`}
  125. style={{
  126. // #3016 disable transition on ios mobile screen
  127. transition: isMobileScreen && isIOSMobile ? "none" : undefined,
  128. }}
  129. >
  130. <div className={styles["sidebar-header"]} data-tauri-drag-region>
  131. <div className={styles["sidebar-title"]} data-tauri-drag-region>
  132. ChatGPT Next
  133. </div>
  134. <div className={styles["sidebar-sub-title"]}>
  135. Build your own AI assistant.
  136. </div>
  137. <div className={styles["sidebar-logo"] + " no-dark"}>
  138. <ChatGptIcon />
  139. </div>
  140. </div>
  141. <div className={styles["sidebar-header-bar"]}>
  142. <IconButton
  143. icon={<MaskIcon />}
  144. text={shouldNarrow ? undefined : Locale.Mask.Name}
  145. className={styles["sidebar-bar-button"]}
  146. onClick={() => {
  147. if (config.dontShowMaskSplashScreen !== true) {
  148. navigate(Path.NewChat, { state: { fromHome: true } });
  149. } else {
  150. navigate(Path.Masks, { state: { fromHome: true } });
  151. }
  152. }}
  153. shadow
  154. />
  155. <IconButton
  156. icon={<PluginIcon />}
  157. text={shouldNarrow ? undefined : Locale.Plugin.Name}
  158. className={styles["sidebar-bar-button"]}
  159. onClick={() => showToast(Locale.WIP)}
  160. shadow
  161. />
  162. </div>
  163. <div
  164. className={styles["sidebar-body"]}
  165. onClick={(e) => {
  166. if (e.target === e.currentTarget) {
  167. navigate(Path.Home);
  168. }
  169. }}
  170. >
  171. <ChatList narrow={shouldNarrow} />
  172. </div>
  173. <div className={styles["sidebar-tail"]}>
  174. <div className={styles["sidebar-actions"]}>
  175. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  176. <IconButton
  177. icon={<CloseIcon />}
  178. onClick={async () => {
  179. if (await showConfirm(Locale.Home.DeleteChat)) {
  180. chatStore.deleteSession(chatStore.currentSessionIndex);
  181. }
  182. }}
  183. />
  184. </div>
  185. <div className={styles["sidebar-action"]}>
  186. <Link to={Path.Settings}>
  187. <IconButton icon={<SettingsIcon />} shadow />
  188. </Link>
  189. </div>
  190. <div className={styles["sidebar-action"]}>
  191. <a href={REPO_URL} target="_blank" rel="noopener noreferrer">
  192. <IconButton icon={<GithubIcon />} shadow />
  193. </a>
  194. </div>
  195. </div>
  196. <div>
  197. <IconButton
  198. icon={<AddIcon />}
  199. text={shouldNarrow ? undefined : Locale.Home.NewChat}
  200. onClick={() => {
  201. if (config.dontShowMaskSplashScreen) {
  202. chatStore.newSession();
  203. navigate(Path.Chat);
  204. } else {
  205. navigate(Path.NewChat);
  206. }
  207. }}
  208. shadow
  209. />
  210. </div>
  211. </div>
  212. <div
  213. className={styles["sidebar-drag"]}
  214. onPointerDown={(e) => onDragStart(e as any)}
  215. >
  216. <DragIcon />
  217. </div>
  218. </div>
  219. );
  220. }