sidebar.tsx 7.4 KB

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