home.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use client";
  2. require("../polyfill");
  3. import {
  4. useState,
  5. useEffect,
  6. useRef,
  7. useCallback,
  8. MouseEventHandler,
  9. } from "react";
  10. import { IconButton } from "./button";
  11. import styles from "./home.module.scss";
  12. import SettingsIcon from "../icons/settings.svg";
  13. import GithubIcon from "../icons/github.svg";
  14. import ChatGptIcon from "../icons/chatgpt.svg";
  15. import BotIcon from "../icons/bot.svg";
  16. import AddIcon from "../icons/add.svg";
  17. import LoadingIcon from "../icons/three-dots.svg";
  18. import CloseIcon from "../icons/close.svg";
  19. import { useChatStore } from "../store";
  20. import { isMobileScreen } from "../utils";
  21. import Locale from "../locales";
  22. import { Chat } from "./chat";
  23. import dynamic from "next/dynamic";
  24. import { REPO_URL } from "../constant";
  25. import { ErrorBoundary } from "./error";
  26. import { useDebounce } from "use-debounce";
  27. export function Loading(props: { noLogo?: boolean }) {
  28. return (
  29. <div className={styles["loading-content"]}>
  30. {!props.noLogo && <BotIcon />}
  31. <LoadingIcon />
  32. </div>
  33. );
  34. }
  35. const Settings = dynamic(async () => (await import("./settings")).Settings, {
  36. loading: () => <Loading noLogo />,
  37. });
  38. const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
  39. loading: () => <Loading noLogo />,
  40. });
  41. function useSwitchTheme() {
  42. const config = useChatStore((state) => state.config);
  43. useEffect(() => {
  44. document.body.classList.remove("light");
  45. document.body.classList.remove("dark");
  46. if (config.theme === "dark") {
  47. document.body.classList.add("dark");
  48. } else if (config.theme === "light") {
  49. document.body.classList.add("light");
  50. }
  51. const metaDescriptionDark = document.querySelector(
  52. 'meta[name="theme-color"][media]',
  53. );
  54. const metaDescriptionLight = document.querySelector(
  55. 'meta[name="theme-color"]:not([media])',
  56. );
  57. if (config.theme === "auto") {
  58. metaDescriptionDark?.setAttribute("content", "#151515");
  59. metaDescriptionLight?.setAttribute("content", "#fafafa");
  60. } else {
  61. const themeColor = getComputedStyle(document.body)
  62. .getPropertyValue("--theme-color")
  63. .trim();
  64. metaDescriptionDark?.setAttribute("content", themeColor);
  65. metaDescriptionLight?.setAttribute("content", themeColor);
  66. }
  67. }, [config.theme]);
  68. }
  69. function useDragSideBar() {
  70. const limit = (x: number) => Math.min(500, Math.max(220, x));
  71. const chatStore = useChatStore();
  72. const startX = useRef(0);
  73. const startDragWidth = useRef(chatStore.config.sidebarWidth ?? 300);
  74. const lastUpdateTime = useRef(Date.now());
  75. const handleMouseMove = useRef((e: MouseEvent) => {
  76. if (Date.now() < lastUpdateTime.current + 100) {
  77. return;
  78. }
  79. lastUpdateTime.current = Date.now();
  80. const d = e.clientX - startX.current;
  81. const nextWidth = limit(startDragWidth.current + d);
  82. chatStore.updateConfig((config) => (config.sidebarWidth = nextWidth));
  83. });
  84. const handleMouseUp = useRef(() => {
  85. startDragWidth.current = chatStore.config.sidebarWidth ?? 300;
  86. window.removeEventListener("mousemove", handleMouseMove.current);
  87. window.removeEventListener("mouseup", handleMouseUp.current);
  88. });
  89. const onDragMouseDown = (e: MouseEvent) => {
  90. startX.current = e.clientX;
  91. window.addEventListener("mousemove", handleMouseMove.current);
  92. window.addEventListener("mouseup", handleMouseUp.current);
  93. };
  94. useEffect(() => {
  95. document.documentElement.style.setProperty(
  96. "--sidebar-width",
  97. `${limit(chatStore.config.sidebarWidth ?? 300)}px`,
  98. );
  99. }, [chatStore.config.sidebarWidth]);
  100. return {
  101. onDragMouseDown,
  102. };
  103. }
  104. const useHasHydrated = () => {
  105. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  106. useEffect(() => {
  107. setHasHydrated(true);
  108. }, []);
  109. return hasHydrated;
  110. };
  111. function _Home() {
  112. const [createNewSession, currentIndex, removeSession] = useChatStore(
  113. (state) => [
  114. state.newSession,
  115. state.currentSessionIndex,
  116. state.removeSession,
  117. ],
  118. );
  119. const chatStore = useChatStore();
  120. const loading = !useHasHydrated();
  121. const [showSideBar, setShowSideBar] = useState(true);
  122. // setting
  123. const [openSettings, setOpenSettings] = useState(false);
  124. const config = useChatStore((state) => state.config);
  125. // drag side bar
  126. const { onDragMouseDown } = useDragSideBar();
  127. useSwitchTheme();
  128. if (loading) {
  129. return <Loading />;
  130. }
  131. return (
  132. <div
  133. className={`${
  134. config.tightBorder && !isMobileScreen()
  135. ? styles["tight-container"]
  136. : styles.container
  137. }`}
  138. >
  139. <div
  140. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  141. >
  142. <div className={styles["sidebar-header"]}>
  143. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  144. <div className={styles["sidebar-sub-title"]}>
  145. Build your own AI assistant.
  146. </div>
  147. <div className={styles["sidebar-logo"]}>
  148. <ChatGptIcon />
  149. </div>
  150. </div>
  151. <div
  152. className={styles["sidebar-body"]}
  153. onClick={() => {
  154. setOpenSettings(false);
  155. setShowSideBar(false);
  156. }}
  157. >
  158. <ChatList />
  159. </div>
  160. <div className={styles["sidebar-tail"]}>
  161. <div className={styles["sidebar-actions"]}>
  162. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  163. <IconButton
  164. icon={<CloseIcon />}
  165. onClick={chatStore.deleteSession}
  166. />
  167. </div>
  168. <div className={styles["sidebar-action"]}>
  169. <IconButton
  170. icon={<SettingsIcon />}
  171. onClick={() => {
  172. setOpenSettings(true);
  173. setShowSideBar(false);
  174. }}
  175. shadow
  176. />
  177. </div>
  178. <div className={styles["sidebar-action"]}>
  179. <a href={REPO_URL} target="_blank">
  180. <IconButton icon={<GithubIcon />} shadow />
  181. </a>
  182. </div>
  183. </div>
  184. <div>
  185. <IconButton
  186. icon={<AddIcon />}
  187. text={Locale.Home.NewChat}
  188. onClick={() => {
  189. createNewSession();
  190. setShowSideBar(false);
  191. }}
  192. shadow
  193. />
  194. </div>
  195. </div>
  196. <div
  197. className={styles["sidebar-drag"]}
  198. onMouseDown={(e) => onDragMouseDown(e as any)}
  199. ></div>
  200. </div>
  201. <div className={styles["window-content"]}>
  202. {openSettings ? (
  203. <Settings
  204. closeSettings={() => {
  205. setOpenSettings(false);
  206. setShowSideBar(true);
  207. }}
  208. />
  209. ) : (
  210. <Chat
  211. key="chat"
  212. showSideBar={() => setShowSideBar(true)}
  213. sideBarShowing={showSideBar}
  214. />
  215. )}
  216. </div>
  217. </div>
  218. );
  219. }
  220. export function Home() {
  221. return (
  222. <ErrorBoundary>
  223. <_Home></_Home>
  224. </ErrorBoundary>
  225. );
  226. }