home.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use client";
  2. require("../polyfill");
  3. import { useState, useEffect } 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 { isMobileScreen } 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. import calcTextareaHeight from "../calcTextareaHeight";
  21. export function Loading(props: { noLogo?: boolean }) {
  22. return (
  23. <div className={styles["loading-content"]}>
  24. {!props.noLogo && <BotIcon />}
  25. <LoadingIcon />
  26. </div>
  27. );
  28. }
  29. const Settings = dynamic(async () => (await import("./settings")).Settings, {
  30. loading: () => <Loading noLogo />,
  31. });
  32. const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
  33. loading: () => <Loading noLogo />,
  34. });
  35. function useSwitchTheme() {
  36. const config = useChatStore((state) => state.config);
  37. useEffect(() => {
  38. document.body.classList.remove("light");
  39. document.body.classList.remove("dark");
  40. if (config.theme === "dark") {
  41. document.body.classList.add("dark");
  42. } else if (config.theme === "light") {
  43. document.body.classList.add("light");
  44. }
  45. const metaDescriptionDark = document.querySelector(
  46. 'meta[name="theme-color"][media]',
  47. );
  48. const metaDescriptionLight = document.querySelector(
  49. 'meta[name="theme-color"]:not([media])',
  50. );
  51. if (config.theme === "auto") {
  52. metaDescriptionDark?.setAttribute("content", "#151515");
  53. metaDescriptionLight?.setAttribute("content", "#fafafa");
  54. } else {
  55. const themeColor = getComputedStyle(document.body)
  56. .getPropertyValue("--theme-color")
  57. .trim();
  58. metaDescriptionDark?.setAttribute("content", themeColor);
  59. metaDescriptionLight?.setAttribute("content", themeColor);
  60. }
  61. }, [config.theme]);
  62. }
  63. const useHasHydrated = () => {
  64. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  65. useEffect(() => {
  66. setHasHydrated(true);
  67. }, []);
  68. return hasHydrated;
  69. };
  70. function _Home() {
  71. const [createNewSession, currentIndex, removeSession] = useChatStore(
  72. (state) => [
  73. state.newSession,
  74. state.currentSessionIndex,
  75. state.removeSession,
  76. ],
  77. );
  78. const chatStore = useChatStore();
  79. const loading = !useHasHydrated();
  80. const [showSideBar, setShowSideBar] = useState(true);
  81. // setting
  82. const [openSettings, setOpenSettings] = useState(false);
  83. const config = useChatStore((state) => state.config);
  84. useSwitchTheme();
  85. if (loading) {
  86. return <Loading />;
  87. }
  88. return (
  89. <div
  90. className={`${
  91. config.tightBorder && !isMobileScreen()
  92. ? styles["tight-container"]
  93. : styles.container
  94. }`}
  95. >
  96. <div
  97. className={styles.sidebar + ` ${showSideBar && styles["sidebar-show"]}`}
  98. >
  99. <div className={styles["sidebar-header"]}>
  100. <div className={styles["sidebar-title"]}>ChatGPT Next</div>
  101. <div className={styles["sidebar-sub-title"]}>
  102. Build your own AI assistant.
  103. </div>
  104. <div className={styles["sidebar-logo"]}>
  105. <ChatGptIcon />
  106. </div>
  107. </div>
  108. <div
  109. className={styles["sidebar-body"]}
  110. onClick={() => {
  111. setOpenSettings(false);
  112. setShowSideBar(false);
  113. }}
  114. >
  115. <ChatList />
  116. </div>
  117. <div className={styles["sidebar-tail"]}>
  118. <div className={styles["sidebar-actions"]}>
  119. <div className={styles["sidebar-action"] + " " + styles.mobile}>
  120. <IconButton
  121. icon={<CloseIcon />}
  122. onClick={chatStore.deleteSession}
  123. />
  124. </div>
  125. <div className={styles["sidebar-action"]}>
  126. <IconButton
  127. icon={<SettingsIcon />}
  128. onClick={() => {
  129. setOpenSettings(true);
  130. setShowSideBar(false);
  131. }}
  132. shadow
  133. />
  134. </div>
  135. <div className={styles["sidebar-action"]}>
  136. <a href={REPO_URL} target="_blank">
  137. <IconButton icon={<GithubIcon />} shadow />
  138. </a>
  139. </div>
  140. </div>
  141. <div>
  142. <IconButton
  143. icon={<AddIcon />}
  144. text={Locale.Home.NewChat}
  145. onClick={() => {
  146. createNewSession();
  147. setShowSideBar(false);
  148. }}
  149. shadow
  150. />
  151. </div>
  152. </div>
  153. </div>
  154. <div className={styles["window-content"]}>
  155. {openSettings ? (
  156. <Settings
  157. closeSettings={() => {
  158. setOpenSettings(false);
  159. setShowSideBar(true);
  160. }}
  161. />
  162. ) : (
  163. <Chat
  164. key="chat"
  165. showSideBar={() => setShowSideBar(true)}
  166. sideBarShowing={showSideBar}
  167. autoSize={{ minRows: 2, maxRows: 6 }}
  168. />
  169. )}
  170. </div>
  171. </div>
  172. );
  173. }
  174. export function Home() {
  175. return (
  176. <ErrorBoundary>
  177. <_Home></_Home>
  178. </ErrorBoundary>
  179. );
  180. }