home.tsx 7.0 KB

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