sidebar.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. PLUGINS,
  22. REPO_URL,
  23. } from "../constant";
  24. import { Link, useLocation, useNavigate } from "react-router-dom";
  25. import { isIOS, useMobileScreen } from "../utils";
  26. import dynamic from "next/dynamic";
  27. import { Selector, showConfirm, showToast } from "./ui-lib";
  28. import de from "@/app/locales/de";
  29. const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
  30. loading: () => null,
  31. });
  32. const SdPanel = dynamic(async () => (await import("./sd-panel")).SdPanel, {
  33. loading: () => null,
  34. });
  35. function useHotKey() {
  36. const chatStore = useChatStore();
  37. useEffect(() => {
  38. const onKeyDown = (e: KeyboardEvent) => {
  39. if (e.altKey || e.ctrlKey) {
  40. if (e.key === "ArrowUp") {
  41. chatStore.nextSession(-1);
  42. } else if (e.key === "ArrowDown") {
  43. chatStore.nextSession(1);
  44. }
  45. }
  46. };
  47. window.addEventListener("keydown", onKeyDown);
  48. return () => window.removeEventListener("keydown", onKeyDown);
  49. });
  50. }
  51. function useDragSideBar() {
  52. const limit = (x: number) => Math.min(MAX_SIDEBAR_WIDTH, x);
  53. const config = useAppConfig();
  54. const startX = useRef(0);
  55. const startDragWidth = useRef(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
  56. const lastUpdateTime = useRef(Date.now());
  57. const toggleSideBar = () => {
  58. config.update((config) => {
  59. if (config.sidebarWidth < MIN_SIDEBAR_WIDTH) {
  60. config.sidebarWidth = DEFAULT_SIDEBAR_WIDTH;
  61. } else {
  62. config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
  63. }
  64. });
  65. };
  66. const onDragStart = (e: MouseEvent) => {
  67. // Remembers the initial width each time the mouse is pressed
  68. startX.current = e.clientX;
  69. startDragWidth.current = config.sidebarWidth;
  70. const dragStartTime = Date.now();
  71. const handleDragMove = (e: MouseEvent) => {
  72. if (Date.now() < lastUpdateTime.current + 20) {
  73. return;
  74. }
  75. lastUpdateTime.current = Date.now();
  76. const d = e.clientX - startX.current;
  77. const nextWidth = limit(startDragWidth.current + d);
  78. config.update((config) => {
  79. if (nextWidth < MIN_SIDEBAR_WIDTH) {
  80. config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
  81. } else {
  82. config.sidebarWidth = nextWidth;
  83. }
  84. });
  85. };
  86. const handleDragEnd = () => {
  87. // In useRef the data is non-responsive, so `config.sidebarWidth` can't get the dynamic sidebarWidth
  88. window.removeEventListener("pointermove", handleDragMove);
  89. window.removeEventListener("pointerup", handleDragEnd);
  90. // if user click the drag icon, should toggle the sidebar
  91. const shouldFireClick = Date.now() - dragStartTime < 300;
  92. if (shouldFireClick) {
  93. toggleSideBar();
  94. }
  95. };
  96. window.addEventListener("pointermove", handleDragMove);
  97. window.addEventListener("pointerup", handleDragEnd);
  98. };
  99. const isMobileScreen = useMobileScreen();
  100. const shouldNarrow =
  101. !isMobileScreen && config.sidebarWidth < MIN_SIDEBAR_WIDTH;
  102. useEffect(() => {
  103. const barWidth = shouldNarrow
  104. ? NARROW_SIDEBAR_WIDTH
  105. : limit(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
  106. const sideBarWidth = isMobileScreen ? "100vw" : `${barWidth}px`;
  107. document.documentElement.style.setProperty("--sidebar-width", sideBarWidth);
  108. }, [config.sidebarWidth, isMobileScreen, shouldNarrow]);
  109. return {
  110. onDragStart,
  111. shouldNarrow,
  112. };
  113. }
  114. export function SideBar(props: { className?: string }) {
  115. const chatStore = useChatStore();
  116. // drag side bar
  117. const { onDragStart, shouldNarrow } = useDragSideBar();
  118. const navigate = useNavigate();
  119. const config = useAppConfig();
  120. const isMobileScreen = useMobileScreen();
  121. const isIOSMobile = useMemo(
  122. () => isIOS() && isMobileScreen,
  123. [isMobileScreen],
  124. );
  125. const [showPluginSelector, setShowPluginSelector] = useState(false);
  126. const location = useLocation();
  127. useHotKey();
  128. let bodyComponent: React.JSX.Element;
  129. let isChat: boolean = false;
  130. switch (location.pathname) {
  131. case Path.Sd:
  132. bodyComponent = <SdPanel />;
  133. break;
  134. default:
  135. isChat = true;
  136. bodyComponent = <ChatList narrow={shouldNarrow} />;
  137. }
  138. // @ts-ignore
  139. return (
  140. <div
  141. className={`${styles.sidebar} ${props.className} ${
  142. shouldNarrow && styles["narrow-sidebar"]
  143. }`}
  144. style={{
  145. // #3016 disable transition on ios mobile screen
  146. transition: isMobileScreen && isIOSMobile ? "none" : undefined,
  147. }}
  148. >
  149. <div className={styles["sidebar-header"]} data-tauri-drag-region>
  150. <div className={styles["sidebar-title"]} data-tauri-drag-region>
  151. NextChat
  152. </div>
  153. <div className={styles["sidebar-sub-title"]}>
  154. Build your own AI assistant.
  155. </div>
  156. <div className={styles["sidebar-logo"] + " no-dark"}>
  157. <ChatGptIcon />
  158. </div>
  159. </div>
  160. <div className={styles["sidebar-header-bar"]}>
  161. <IconButton
  162. icon={<MaskIcon />}
  163. text={shouldNarrow ? undefined : Locale.Mask.Name}
  164. className={styles["sidebar-bar-button"]}
  165. onClick={() => {
  166. if (config.dontShowMaskSplashScreen !== true) {
  167. navigate(Path.NewChat, { state: { fromHome: true } });
  168. } else {
  169. navigate(Path.Masks, { state: { fromHome: true } });
  170. }
  171. }}
  172. shadow
  173. />
  174. <IconButton
  175. icon={<PluginIcon />}
  176. text={shouldNarrow ? undefined : Locale.Plugin.Name}
  177. className={styles["sidebar-bar-button"]}
  178. onClick={() => setShowPluginSelector(true)}
  179. shadow
  180. />
  181. </div>
  182. <div
  183. className={styles["sidebar-body"]}
  184. onClick={(e) => {
  185. if (isChat && e.target === e.currentTarget) {
  186. navigate(Path.Home);
  187. }
  188. }}
  189. >
  190. {bodyComponent}
  191. </div>
  192. <div className={styles["sidebar-tail"]}>
  193. <div className={styles["sidebar-actions"]}>
  194. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  195. <IconButton
  196. icon={<DeleteIcon />}
  197. onClick={async () => {
  198. if (await showConfirm(Locale.Home.DeleteChat)) {
  199. chatStore.deleteSession(chatStore.currentSessionIndex);
  200. }
  201. }}
  202. />
  203. </div>
  204. <div className={styles["sidebar-action"]}>
  205. <Link to={Path.Settings}>
  206. <IconButton icon={<SettingsIcon />} shadow />
  207. </Link>
  208. </div>
  209. <div className={styles["sidebar-action"]}>
  210. <a href={REPO_URL} target="_blank" rel="noopener noreferrer">
  211. <IconButton icon={<GithubIcon />} shadow />
  212. </a>
  213. </div>
  214. </div>
  215. {isChat && (
  216. <div>
  217. <IconButton
  218. icon={<AddIcon />}
  219. text={shouldNarrow ? undefined : Locale.Home.NewChat}
  220. onClick={() => {
  221. if (config.dontShowMaskSplashScreen) {
  222. chatStore.newSession();
  223. navigate(Path.Chat);
  224. } else {
  225. navigate(Path.NewChat);
  226. }
  227. }}
  228. shadow
  229. />
  230. </div>
  231. )}
  232. </div>
  233. <div
  234. className={styles["sidebar-drag"]}
  235. onPointerDown={(e) => onDragStart(e as any)}
  236. >
  237. <DragIcon />
  238. </div>
  239. {showPluginSelector && (
  240. <Selector
  241. items={[
  242. {
  243. title: "👇 Please select the plugin you need to use",
  244. value: "-",
  245. disable: true,
  246. },
  247. ...PLUGINS.map((item) => {
  248. return {
  249. title: item.name,
  250. value: item.path,
  251. };
  252. }),
  253. ]}
  254. onClose={() => setShowPluginSelector(false)}
  255. onSelection={(s) => {
  256. navigate(s[0], { state: { fromHome: true } });
  257. }}
  258. />
  259. )}
  260. </div>
  261. );
  262. }