sidebar.tsx 4.0 KB

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