sidebar.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import React, { useEffect, useRef, useMemo, useState } 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 { ModelType, 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 { Selector, 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. const [showPluginSelector, setShowPluginSelector] = useState(false);
  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. NextChat
  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. <ChatGptIcon />
  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={() => setShowPluginSelector(true)}
  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. {showPluginSelector && (
  221. <Selector
  222. items={[
  223. {
  224. title: "👇 Please select the plugin you need to use",
  225. value: "-",
  226. disable: true,
  227. },
  228. { title: "Stable Diffusion", value: "sd" },
  229. ]}
  230. onClose={() => setShowPluginSelector(false)}
  231. onSelection={(s) => {
  232. console.log("go to page: ", s);
  233. }}
  234. />
  235. )}
  236. </div>
  237. );
  238. }