sidebar.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { useState, useEffect, useRef } 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 Locale from "../locales";
  10. import { useChatStore } from "../store";
  11. import {
  12. MAX_SIDEBAR_WIDTH,
  13. MIN_SIDEBAR_WIDTH,
  14. NARROW_SIDEBAR_WIDTH,
  15. Path,
  16. REPO_URL,
  17. } from "../constant";
  18. import { HashRouter as Router, Link, useNavigate } from "react-router-dom";
  19. import { useMobileScreen } from "../utils";
  20. import { ChatList } from "./chat-list";
  21. function useDragSideBar() {
  22. const limit = (x: number) => Math.min(MAX_SIDEBAR_WIDTH, x);
  23. const chatStore = useChatStore();
  24. const startX = useRef(0);
  25. const startDragWidth = useRef(chatStore.config.sidebarWidth ?? 300);
  26. const lastUpdateTime = useRef(Date.now());
  27. const handleMouseMove = useRef((e: MouseEvent) => {
  28. if (Date.now() < lastUpdateTime.current + 50) {
  29. return;
  30. }
  31. lastUpdateTime.current = Date.now();
  32. const d = e.clientX - startX.current;
  33. const nextWidth = limit(startDragWidth.current + d);
  34. chatStore.updateConfig((config) => (config.sidebarWidth = nextWidth));
  35. });
  36. const handleMouseUp = useRef(() => {
  37. startDragWidth.current = chatStore.config.sidebarWidth ?? 300;
  38. window.removeEventListener("mousemove", handleMouseMove.current);
  39. window.removeEventListener("mouseup", handleMouseUp.current);
  40. });
  41. const onDragMouseDown = (e: MouseEvent) => {
  42. startX.current = e.clientX;
  43. window.addEventListener("mousemove", handleMouseMove.current);
  44. window.addEventListener("mouseup", handleMouseUp.current);
  45. };
  46. const isMobileScreen = useMobileScreen();
  47. const shouldNarrow =
  48. !isMobileScreen && chatStore.config.sidebarWidth < MIN_SIDEBAR_WIDTH;
  49. useEffect(() => {
  50. const barWidth = shouldNarrow
  51. ? NARROW_SIDEBAR_WIDTH
  52. : limit(chatStore.config.sidebarWidth ?? 300);
  53. const sideBarWidth = isMobileScreen ? "100vw" : `${barWidth}px`;
  54. document.documentElement.style.setProperty("--sidebar-width", sideBarWidth);
  55. }, [chatStore.config.sidebarWidth, isMobileScreen, shouldNarrow]);
  56. return {
  57. onDragMouseDown,
  58. shouldNarrow,
  59. };
  60. }
  61. export function SideBar(props: { className?: string }) {
  62. const chatStore = useChatStore();
  63. // drag side bar
  64. const { onDragMouseDown, shouldNarrow } = useDragSideBar();
  65. const navigate = useNavigate();
  66. return (
  67. <div
  68. className={`${styles.sidebar} ${props.className} ${
  69. shouldNarrow && styles["narrow-sidebar"]
  70. }`}
  71. >
  72. <div className={styles["sidebar-header"]}>
  73. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  74. <div className={styles["sidebar-sub-title"]}>
  75. Build your own AI assistant.
  76. </div>
  77. <div className={styles["sidebar-logo"]}>
  78. <ChatGptIcon />
  79. </div>
  80. </div>
  81. <div
  82. className={styles["sidebar-body"]}
  83. onClick={(e) => {
  84. if (e.target === e.currentTarget) {
  85. navigate(Path.Home);
  86. }
  87. }}
  88. >
  89. <ChatList narrow={shouldNarrow} />
  90. </div>
  91. <div className={styles["sidebar-tail"]}>
  92. <div className={styles["sidebar-actions"]}>
  93. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  94. <IconButton
  95. icon={<CloseIcon />}
  96. onClick={chatStore.deleteSession}
  97. />
  98. </div>
  99. <div className={styles["sidebar-action"]}>
  100. <Link to={Path.Settings}>
  101. <IconButton icon={<SettingsIcon />} shadow />
  102. </Link>
  103. </div>
  104. <div className={styles["sidebar-action"]}>
  105. <a href={REPO_URL} target="_blank">
  106. <IconButton icon={<GithubIcon />} shadow />
  107. </a>
  108. </div>
  109. </div>
  110. <div>
  111. <IconButton
  112. icon={<AddIcon />}
  113. text={shouldNarrow ? undefined : Locale.Home.NewChat}
  114. onClick={() => {
  115. chatStore.newSession();
  116. }}
  117. shadow
  118. />
  119. </div>
  120. </div>
  121. <div
  122. className={styles["sidebar-drag"]}
  123. onMouseDown={(e) => onDragMouseDown(e as any)}
  124. ></div>
  125. </div>
  126. );
  127. }