home.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. if (isMobileScreen()) {
  96. return;
  97. }
  98. document.documentElement.style.setProperty(
  99. "--sidebar-width",
  100. `${limit(chatStore.config.sidebarWidth ?? 300)}px`,
  101. );
  102. }, [chatStore.config.sidebarWidth]);
  103. return {
  104. onDragMouseDown,
  105. };
  106. }
  107. const useHasHydrated = () => {
  108. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  109. useEffect(() => {
  110. setHasHydrated(true);
  111. }, []);
  112. return hasHydrated;
  113. };
  114. function _Home() {
  115. const [createNewSession, currentIndex, removeSession] = useChatStore(
  116. (state) => [
  117. state.newSession,
  118. state.currentSessionIndex,
  119. state.removeSession,
  120. ],
  121. );
  122. const chatStore = useChatStore();
  123. const loading = !useHasHydrated();
  124. const [showSideBar, setShowSideBar] = useState(true);
  125. // setting
  126. const [openSettings, setOpenSettings] = useState(false);
  127. const config = useChatStore((state) => state.config);
  128. // drag side bar
  129. const { onDragMouseDown } = useDragSideBar();
  130. useSwitchTheme();
  131. if (loading) {
  132. return <Loading />;
  133. }
  134. return (
  135. <div
  136. className={`${
  137. config.tightBorder && !isMobileScreen()
  138. ? styles["tight-container"]
  139. : styles.container
  140. }`}
  141. >
  142. <div
  143. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  144. >
  145. <div className={styles["sidebar-header"]}>
  146. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  147. <div className={styles["sidebar-sub-title"]}>
  148. Build your own AI assistant.
  149. </div>
  150. <div className={styles["sidebar-logo"]}>
  151. <ChatGptIcon />
  152. </div>
  153. </div>
  154. <div
  155. className={styles["sidebar-body"]}
  156. onClick={() => {
  157. setOpenSettings(false);
  158. setShowSideBar(false);
  159. }}
  160. >
  161. <ChatList />
  162. </div>
  163. <div className={styles["sidebar-tail"]}>
  164. <div className={styles["sidebar-actions"]}>
  165. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  166. <IconButton
  167. icon={<CloseIcon />}
  168. onClick={chatStore.deleteSession}
  169. />
  170. </div>
  171. <div className={styles["sidebar-action"]}>
  172. <IconButton
  173. icon={<SettingsIcon />}
  174. onClick={() => {
  175. setOpenSettings(true);
  176. setShowSideBar(false);
  177. }}
  178. shadow
  179. />
  180. </div>
  181. <div className={styles["sidebar-action"]}>
  182. <a href={REPO_URL} target="_blank">
  183. <IconButton icon={<GithubIcon />} shadow />
  184. </a>
  185. </div>
  186. </div>
  187. <div>
  188. <IconButton
  189. icon={<AddIcon />}
  190. text={Locale.Home.NewChat}
  191. onClick={() => {
  192. createNewSession();
  193. setShowSideBar(false);
  194. }}
  195. shadow
  196. />
  197. </div>
  198. </div>
  199. <div
  200. className={styles["sidebar-drag"]}
  201. onMouseDown={(e) => onDragMouseDown(e as any)}
  202. ></div>
  203. </div>
  204. <div className={styles["window-content"]}>
  205. {openSettings ? (
  206. <Settings
  207. closeSettings={() => {
  208. setOpenSettings(false);
  209. setShowSideBar(true);
  210. }}
  211. />
  212. ) : (
  213. <Chat
  214. key="chat"
  215. showSideBar={() => setShowSideBar(true)}
  216. sideBarShowing={showSideBar}
  217. />
  218. )}
  219. </div>
  220. </div>
  221. );
  222. }
  223. export function Home() {
  224. return (
  225. <ErrorBoundary>
  226. <_Home></_Home>
  227. </ErrorBoundary>
  228. );
  229. }